packet.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. package sftp
  2. import (
  3. "bytes"
  4. "encoding"
  5. "encoding/binary"
  6. "fmt"
  7. "io"
  8. "os"
  9. "reflect"
  10. "github.com/pkg/errors"
  11. )
  12. var (
  13. errShortPacket = errors.New("packet too short")
  14. errUnknownExtendedPacket = errors.New("unknown extended packet")
  15. )
  16. const (
  17. debugDumpTxPacket = false
  18. debugDumpRxPacket = false
  19. debugDumpTxPacketBytes = false
  20. debugDumpRxPacketBytes = false
  21. )
  22. func marshalUint32(b []byte, v uint32) []byte {
  23. return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
  24. }
  25. func marshalUint64(b []byte, v uint64) []byte {
  26. return marshalUint32(marshalUint32(b, uint32(v>>32)), uint32(v))
  27. }
  28. func marshalString(b []byte, v string) []byte {
  29. return append(marshalUint32(b, uint32(len(v))), v...)
  30. }
  31. func marshal(b []byte, v interface{}) []byte {
  32. if v == nil {
  33. return b
  34. }
  35. switch v := v.(type) {
  36. case uint8:
  37. return append(b, v)
  38. case uint32:
  39. return marshalUint32(b, v)
  40. case uint64:
  41. return marshalUint64(b, v)
  42. case string:
  43. return marshalString(b, v)
  44. case os.FileInfo:
  45. return marshalFileInfo(b, v)
  46. default:
  47. switch d := reflect.ValueOf(v); d.Kind() {
  48. case reflect.Struct:
  49. for i, n := 0, d.NumField(); i < n; i++ {
  50. b = append(marshal(b, d.Field(i).Interface()))
  51. }
  52. return b
  53. case reflect.Slice:
  54. for i, n := 0, d.Len(); i < n; i++ {
  55. b = append(marshal(b, d.Index(i).Interface()))
  56. }
  57. return b
  58. default:
  59. panic(fmt.Sprintf("marshal(%#v): cannot handle type %T", v, v))
  60. }
  61. }
  62. }
  63. func unmarshalUint32(b []byte) (uint32, []byte) {
  64. v := uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
  65. return v, b[4:]
  66. }
  67. func unmarshalUint32Safe(b []byte) (uint32, []byte, error) {
  68. var v uint32
  69. if len(b) < 4 {
  70. return 0, nil, errShortPacket
  71. }
  72. v, b = unmarshalUint32(b)
  73. return v, b, nil
  74. }
  75. func unmarshalUint64(b []byte) (uint64, []byte) {
  76. h, b := unmarshalUint32(b)
  77. l, b := unmarshalUint32(b)
  78. return uint64(h)<<32 | uint64(l), b
  79. }
  80. func unmarshalUint64Safe(b []byte) (uint64, []byte, error) {
  81. var v uint64
  82. if len(b) < 8 {
  83. return 0, nil, errShortPacket
  84. }
  85. v, b = unmarshalUint64(b)
  86. return v, b, nil
  87. }
  88. func unmarshalString(b []byte) (string, []byte) {
  89. n, b := unmarshalUint32(b)
  90. return string(b[:n]), b[n:]
  91. }
  92. func unmarshalStringSafe(b []byte) (string, []byte, error) {
  93. n, b, err := unmarshalUint32Safe(b)
  94. if err != nil {
  95. return "", nil, err
  96. }
  97. if int64(n) > int64(len(b)) {
  98. return "", nil, errShortPacket
  99. }
  100. return string(b[:n]), b[n:], nil
  101. }
  102. // sendPacket marshals p according to RFC 4234.
  103. func sendPacket(w io.Writer, m encoding.BinaryMarshaler) error {
  104. bb, err := m.MarshalBinary()
  105. if err != nil {
  106. return errors.Errorf("binary marshaller failed: %v", err)
  107. }
  108. if debugDumpTxPacketBytes {
  109. debug("send packet: %s %d bytes %x", fxp(bb[0]), len(bb), bb[1:])
  110. } else if debugDumpTxPacket {
  111. debug("send packet: %s %d bytes", fxp(bb[0]), len(bb))
  112. }
  113. l := uint32(len(bb))
  114. hdr := []byte{byte(l >> 24), byte(l >> 16), byte(l >> 8), byte(l)}
  115. _, err = w.Write(hdr)
  116. if err != nil {
  117. return errors.Errorf("failed to send packet header: %v", err)
  118. }
  119. _, err = w.Write(bb)
  120. if err != nil {
  121. return errors.Errorf("failed to send packet body: %v", err)
  122. }
  123. return nil
  124. }
  125. func recvPacket(r io.Reader) (uint8, []byte, error) {
  126. var b = []byte{0, 0, 0, 0}
  127. if _, err := io.ReadFull(r, b); err != nil {
  128. return 0, nil, err
  129. }
  130. l, _ := unmarshalUint32(b)
  131. b = make([]byte, l)
  132. if _, err := io.ReadFull(r, b); err != nil {
  133. debug("recv packet %d bytes: err %v", l, err)
  134. return 0, nil, err
  135. }
  136. if debugDumpRxPacketBytes {
  137. debug("recv packet: %s %d bytes %x", fxp(b[0]), l, b[1:])
  138. } else if debugDumpRxPacket {
  139. debug("recv packet: %s %d bytes", fxp(b[0]), l)
  140. }
  141. return b[0], b[1:], nil
  142. }
  143. type extensionPair struct {
  144. Name string
  145. Data string
  146. }
  147. func unmarshalExtensionPair(b []byte) (extensionPair, []byte, error) {
  148. var ep extensionPair
  149. var err error
  150. ep.Name, b, err = unmarshalStringSafe(b)
  151. if err != nil {
  152. return ep, b, err
  153. }
  154. ep.Data, b, err = unmarshalStringSafe(b)
  155. if err != nil {
  156. return ep, b, err
  157. }
  158. return ep, b, err
  159. }
  160. // Here starts the definition of packets along with their MarshalBinary
  161. // implementations.
  162. // Manually writing the marshalling logic wins us a lot of time and
  163. // allocation.
  164. type sshFxInitPacket struct {
  165. Version uint32
  166. Extensions []extensionPair
  167. }
  168. func (p sshFxInitPacket) MarshalBinary() ([]byte, error) {
  169. l := 1 + 4 // byte + uint32
  170. for _, e := range p.Extensions {
  171. l += 4 + len(e.Name) + 4 + len(e.Data)
  172. }
  173. b := make([]byte, 0, l)
  174. b = append(b, ssh_FXP_INIT)
  175. b = marshalUint32(b, p.Version)
  176. for _, e := range p.Extensions {
  177. b = marshalString(b, e.Name)
  178. b = marshalString(b, e.Data)
  179. }
  180. return b, nil
  181. }
  182. func (p *sshFxInitPacket) UnmarshalBinary(b []byte) error {
  183. var err error
  184. if p.Version, b, err = unmarshalUint32Safe(b); err != nil {
  185. return err
  186. }
  187. for len(b) > 0 {
  188. var ep extensionPair
  189. ep, b, err = unmarshalExtensionPair(b)
  190. if err != nil {
  191. return err
  192. }
  193. p.Extensions = append(p.Extensions, ep)
  194. }
  195. return nil
  196. }
  197. type sshFxVersionPacket struct {
  198. Version uint32
  199. Extensions []struct {
  200. Name, Data string
  201. }
  202. }
  203. func (p sshFxVersionPacket) MarshalBinary() ([]byte, error) {
  204. l := 1 + 4 // byte + uint32
  205. for _, e := range p.Extensions {
  206. l += 4 + len(e.Name) + 4 + len(e.Data)
  207. }
  208. b := make([]byte, 0, l)
  209. b = append(b, ssh_FXP_VERSION)
  210. b = marshalUint32(b, p.Version)
  211. for _, e := range p.Extensions {
  212. b = marshalString(b, e.Name)
  213. b = marshalString(b, e.Data)
  214. }
  215. return b, nil
  216. }
  217. func marshalIDString(packetType byte, id uint32, str string) ([]byte, error) {
  218. l := 1 + 4 + // type(byte) + uint32
  219. 4 + len(str)
  220. b := make([]byte, 0, l)
  221. b = append(b, packetType)
  222. b = marshalUint32(b, id)
  223. b = marshalString(b, str)
  224. return b, nil
  225. }
  226. func unmarshalIDString(b []byte, id *uint32, str *string) error {
  227. var err error
  228. *id, b, err = unmarshalUint32Safe(b)
  229. if err != nil {
  230. return err
  231. }
  232. *str, b, err = unmarshalStringSafe(b)
  233. return err
  234. }
  235. type sshFxpReaddirPacket struct {
  236. ID uint32
  237. Handle string
  238. }
  239. func (p sshFxpReaddirPacket) id() uint32 { return p.ID }
  240. func (p sshFxpReaddirPacket) MarshalBinary() ([]byte, error) {
  241. return marshalIDString(ssh_FXP_READDIR, p.ID, p.Handle)
  242. }
  243. func (p *sshFxpReaddirPacket) UnmarshalBinary(b []byte) error {
  244. return unmarshalIDString(b, &p.ID, &p.Handle)
  245. }
  246. type sshFxpOpendirPacket struct {
  247. ID uint32
  248. Path string
  249. }
  250. func (p sshFxpOpendirPacket) id() uint32 { return p.ID }
  251. func (p sshFxpOpendirPacket) MarshalBinary() ([]byte, error) {
  252. return marshalIDString(ssh_FXP_OPENDIR, p.ID, p.Path)
  253. }
  254. func (p *sshFxpOpendirPacket) UnmarshalBinary(b []byte) error {
  255. return unmarshalIDString(b, &p.ID, &p.Path)
  256. }
  257. type sshFxpLstatPacket struct {
  258. ID uint32
  259. Path string
  260. }
  261. func (p sshFxpLstatPacket) id() uint32 { return p.ID }
  262. func (p sshFxpLstatPacket) MarshalBinary() ([]byte, error) {
  263. return marshalIDString(ssh_FXP_LSTAT, p.ID, p.Path)
  264. }
  265. func (p *sshFxpLstatPacket) UnmarshalBinary(b []byte) error {
  266. return unmarshalIDString(b, &p.ID, &p.Path)
  267. }
  268. type sshFxpStatPacket struct {
  269. ID uint32
  270. Path string
  271. }
  272. func (p sshFxpStatPacket) id() uint32 { return p.ID }
  273. func (p sshFxpStatPacket) MarshalBinary() ([]byte, error) {
  274. return marshalIDString(ssh_FXP_STAT, p.ID, p.Path)
  275. }
  276. func (p *sshFxpStatPacket) UnmarshalBinary(b []byte) error {
  277. return unmarshalIDString(b, &p.ID, &p.Path)
  278. }
  279. type sshFxpFstatPacket struct {
  280. ID uint32
  281. Handle string
  282. }
  283. func (p sshFxpFstatPacket) id() uint32 { return p.ID }
  284. func (p sshFxpFstatPacket) MarshalBinary() ([]byte, error) {
  285. return marshalIDString(ssh_FXP_FSTAT, p.ID, p.Handle)
  286. }
  287. func (p *sshFxpFstatPacket) UnmarshalBinary(b []byte) error {
  288. return unmarshalIDString(b, &p.ID, &p.Handle)
  289. }
  290. type sshFxpClosePacket struct {
  291. ID uint32
  292. Handle string
  293. }
  294. func (p sshFxpClosePacket) id() uint32 { return p.ID }
  295. func (p sshFxpClosePacket) MarshalBinary() ([]byte, error) {
  296. return marshalIDString(ssh_FXP_CLOSE, p.ID, p.Handle)
  297. }
  298. func (p *sshFxpClosePacket) UnmarshalBinary(b []byte) error {
  299. return unmarshalIDString(b, &p.ID, &p.Handle)
  300. }
  301. type sshFxpRemovePacket struct {
  302. ID uint32
  303. Filename string
  304. }
  305. func (p sshFxpRemovePacket) id() uint32 { return p.ID }
  306. func (p sshFxpRemovePacket) MarshalBinary() ([]byte, error) {
  307. return marshalIDString(ssh_FXP_REMOVE, p.ID, p.Filename)
  308. }
  309. func (p *sshFxpRemovePacket) UnmarshalBinary(b []byte) error {
  310. return unmarshalIDString(b, &p.ID, &p.Filename)
  311. }
  312. type sshFxpRmdirPacket struct {
  313. ID uint32
  314. Path string
  315. }
  316. func (p sshFxpRmdirPacket) id() uint32 { return p.ID }
  317. func (p sshFxpRmdirPacket) MarshalBinary() ([]byte, error) {
  318. return marshalIDString(ssh_FXP_RMDIR, p.ID, p.Path)
  319. }
  320. func (p *sshFxpRmdirPacket) UnmarshalBinary(b []byte) error {
  321. return unmarshalIDString(b, &p.ID, &p.Path)
  322. }
  323. type sshFxpSymlinkPacket struct {
  324. ID uint32
  325. Targetpath string
  326. Linkpath string
  327. }
  328. func (p sshFxpSymlinkPacket) id() uint32 { return p.ID }
  329. func (p sshFxpSymlinkPacket) MarshalBinary() ([]byte, error) {
  330. l := 1 + 4 + // type(byte) + uint32
  331. 4 + len(p.Targetpath) +
  332. 4 + len(p.Linkpath)
  333. b := make([]byte, 0, l)
  334. b = append(b, ssh_FXP_SYMLINK)
  335. b = marshalUint32(b, p.ID)
  336. b = marshalString(b, p.Targetpath)
  337. b = marshalString(b, p.Linkpath)
  338. return b, nil
  339. }
  340. func (p *sshFxpSymlinkPacket) UnmarshalBinary(b []byte) error {
  341. var err error
  342. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  343. return err
  344. } else if p.Targetpath, b, err = unmarshalStringSafe(b); err != nil {
  345. return err
  346. } else if p.Linkpath, b, err = unmarshalStringSafe(b); err != nil {
  347. return err
  348. }
  349. return nil
  350. }
  351. type sshFxpReadlinkPacket struct {
  352. ID uint32
  353. Path string
  354. }
  355. func (p sshFxpReadlinkPacket) id() uint32 { return p.ID }
  356. func (p sshFxpReadlinkPacket) MarshalBinary() ([]byte, error) {
  357. return marshalIDString(ssh_FXP_READLINK, p.ID, p.Path)
  358. }
  359. func (p *sshFxpReadlinkPacket) UnmarshalBinary(b []byte) error {
  360. return unmarshalIDString(b, &p.ID, &p.Path)
  361. }
  362. type sshFxpRealpathPacket struct {
  363. ID uint32
  364. Path string
  365. }
  366. func (p sshFxpRealpathPacket) id() uint32 { return p.ID }
  367. func (p sshFxpRealpathPacket) MarshalBinary() ([]byte, error) {
  368. return marshalIDString(ssh_FXP_REALPATH, p.ID, p.Path)
  369. }
  370. func (p *sshFxpRealpathPacket) UnmarshalBinary(b []byte) error {
  371. return unmarshalIDString(b, &p.ID, &p.Path)
  372. }
  373. type sshFxpNameAttr struct {
  374. Name string
  375. LongName string
  376. Attrs []interface{}
  377. }
  378. func (p sshFxpNameAttr) MarshalBinary() ([]byte, error) {
  379. b := []byte{}
  380. b = marshalString(b, p.Name)
  381. b = marshalString(b, p.LongName)
  382. for _, attr := range p.Attrs {
  383. b = marshal(b, attr)
  384. }
  385. return b, nil
  386. }
  387. type sshFxpNamePacket struct {
  388. ID uint32
  389. NameAttrs []sshFxpNameAttr
  390. }
  391. func (p sshFxpNamePacket) MarshalBinary() ([]byte, error) {
  392. b := []byte{}
  393. b = append(b, ssh_FXP_NAME)
  394. b = marshalUint32(b, p.ID)
  395. b = marshalUint32(b, uint32(len(p.NameAttrs)))
  396. for _, na := range p.NameAttrs {
  397. ab, err := na.MarshalBinary()
  398. if err != nil {
  399. return nil, err
  400. }
  401. b = append(b, ab...)
  402. }
  403. return b, nil
  404. }
  405. type sshFxpOpenPacket struct {
  406. ID uint32
  407. Path string
  408. Pflags uint32
  409. Flags uint32 // ignored
  410. }
  411. func (p sshFxpOpenPacket) id() uint32 { return p.ID }
  412. func (p sshFxpOpenPacket) MarshalBinary() ([]byte, error) {
  413. l := 1 + 4 +
  414. 4 + len(p.Path) +
  415. 4 + 4
  416. b := make([]byte, 0, l)
  417. b = append(b, ssh_FXP_OPEN)
  418. b = marshalUint32(b, p.ID)
  419. b = marshalString(b, p.Path)
  420. b = marshalUint32(b, p.Pflags)
  421. b = marshalUint32(b, p.Flags)
  422. return b, nil
  423. }
  424. func (p *sshFxpOpenPacket) UnmarshalBinary(b []byte) error {
  425. var err error
  426. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  427. return err
  428. } else if p.Path, b, err = unmarshalStringSafe(b); err != nil {
  429. return err
  430. } else if p.Pflags, b, err = unmarshalUint32Safe(b); err != nil {
  431. return err
  432. } else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil {
  433. return err
  434. }
  435. return nil
  436. }
  437. type sshFxpReadPacket struct {
  438. ID uint32
  439. Handle string
  440. Offset uint64
  441. Len uint32
  442. }
  443. func (p sshFxpReadPacket) id() uint32 { return p.ID }
  444. func (p sshFxpReadPacket) MarshalBinary() ([]byte, error) {
  445. l := 1 + 4 + // type(byte) + uint32
  446. 4 + len(p.Handle) +
  447. 8 + 4 // uint64 + uint32
  448. b := make([]byte, 0, l)
  449. b = append(b, ssh_FXP_READ)
  450. b = marshalUint32(b, p.ID)
  451. b = marshalString(b, p.Handle)
  452. b = marshalUint64(b, p.Offset)
  453. b = marshalUint32(b, p.Len)
  454. return b, nil
  455. }
  456. func (p *sshFxpReadPacket) UnmarshalBinary(b []byte) error {
  457. var err error
  458. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  459. return err
  460. } else if p.Handle, b, err = unmarshalStringSafe(b); err != nil {
  461. return err
  462. } else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil {
  463. return err
  464. } else if p.Len, b, err = unmarshalUint32Safe(b); err != nil {
  465. return err
  466. }
  467. return nil
  468. }
  469. type sshFxpRenamePacket struct {
  470. ID uint32
  471. Oldpath string
  472. Newpath string
  473. }
  474. func (p sshFxpRenamePacket) id() uint32 { return p.ID }
  475. func (p sshFxpRenamePacket) MarshalBinary() ([]byte, error) {
  476. l := 1 + 4 + // type(byte) + uint32
  477. 4 + len(p.Oldpath) +
  478. 4 + len(p.Newpath)
  479. b := make([]byte, 0, l)
  480. b = append(b, ssh_FXP_RENAME)
  481. b = marshalUint32(b, p.ID)
  482. b = marshalString(b, p.Oldpath)
  483. b = marshalString(b, p.Newpath)
  484. return b, nil
  485. }
  486. func (p *sshFxpRenamePacket) UnmarshalBinary(b []byte) error {
  487. var err error
  488. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  489. return err
  490. } else if p.Oldpath, b, err = unmarshalStringSafe(b); err != nil {
  491. return err
  492. } else if p.Newpath, b, err = unmarshalStringSafe(b); err != nil {
  493. return err
  494. }
  495. return nil
  496. }
  497. type sshFxpWritePacket struct {
  498. ID uint32
  499. Handle string
  500. Offset uint64
  501. Length uint32
  502. Data []byte
  503. }
  504. func (p sshFxpWritePacket) id() uint32 { return p.ID }
  505. func (p sshFxpWritePacket) MarshalBinary() ([]byte, error) {
  506. l := 1 + 4 + // type(byte) + uint32
  507. 4 + len(p.Handle) +
  508. 8 + 4 + // uint64 + uint32
  509. len(p.Data)
  510. b := make([]byte, 0, l)
  511. b = append(b, ssh_FXP_WRITE)
  512. b = marshalUint32(b, p.ID)
  513. b = marshalString(b, p.Handle)
  514. b = marshalUint64(b, p.Offset)
  515. b = marshalUint32(b, p.Length)
  516. b = append(b, p.Data...)
  517. return b, nil
  518. }
  519. func (p *sshFxpWritePacket) UnmarshalBinary(b []byte) error {
  520. var err error
  521. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  522. return err
  523. } else if p.Handle, b, err = unmarshalStringSafe(b); err != nil {
  524. return err
  525. } else if p.Offset, b, err = unmarshalUint64Safe(b); err != nil {
  526. return err
  527. } else if p.Length, b, err = unmarshalUint32Safe(b); err != nil {
  528. return err
  529. } else if uint32(len(b)) < p.Length {
  530. return errShortPacket
  531. }
  532. p.Data = append([]byte{}, b[:p.Length]...)
  533. return nil
  534. }
  535. type sshFxpMkdirPacket struct {
  536. ID uint32
  537. Path string
  538. Flags uint32 // ignored
  539. }
  540. func (p sshFxpMkdirPacket) id() uint32 { return p.ID }
  541. func (p sshFxpMkdirPacket) MarshalBinary() ([]byte, error) {
  542. l := 1 + 4 + // type(byte) + uint32
  543. 4 + len(p.Path) +
  544. 4 // uint32
  545. b := make([]byte, 0, l)
  546. b = append(b, ssh_FXP_MKDIR)
  547. b = marshalUint32(b, p.ID)
  548. b = marshalString(b, p.Path)
  549. b = marshalUint32(b, p.Flags)
  550. return b, nil
  551. }
  552. func (p *sshFxpMkdirPacket) UnmarshalBinary(b []byte) error {
  553. var err error
  554. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  555. return err
  556. } else if p.Path, b, err = unmarshalStringSafe(b); err != nil {
  557. return err
  558. } else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil {
  559. return err
  560. }
  561. return nil
  562. }
  563. type sshFxpSetstatPacket struct {
  564. ID uint32
  565. Path string
  566. Flags uint32
  567. Attrs interface{}
  568. }
  569. type sshFxpFsetstatPacket struct {
  570. ID uint32
  571. Handle string
  572. Flags uint32
  573. Attrs interface{}
  574. }
  575. func (p sshFxpSetstatPacket) id() uint32 { return p.ID }
  576. func (p sshFxpFsetstatPacket) id() uint32 { return p.ID }
  577. func (p sshFxpSetstatPacket) MarshalBinary() ([]byte, error) {
  578. l := 1 + 4 + // type(byte) + uint32
  579. 4 + len(p.Path) +
  580. 4 // uint32 + uint64
  581. b := make([]byte, 0, l)
  582. b = append(b, ssh_FXP_SETSTAT)
  583. b = marshalUint32(b, p.ID)
  584. b = marshalString(b, p.Path)
  585. b = marshalUint32(b, p.Flags)
  586. b = marshal(b, p.Attrs)
  587. return b, nil
  588. }
  589. func (p sshFxpFsetstatPacket) MarshalBinary() ([]byte, error) {
  590. l := 1 + 4 + // type(byte) + uint32
  591. 4 + len(p.Handle) +
  592. 4 // uint32 + uint64
  593. b := make([]byte, 0, l)
  594. b = append(b, ssh_FXP_FSETSTAT)
  595. b = marshalUint32(b, p.ID)
  596. b = marshalString(b, p.Handle)
  597. b = marshalUint32(b, p.Flags)
  598. b = marshal(b, p.Attrs)
  599. return b, nil
  600. }
  601. func (p *sshFxpSetstatPacket) UnmarshalBinary(b []byte) error {
  602. var err error
  603. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  604. return err
  605. } else if p.Path, b, err = unmarshalStringSafe(b); err != nil {
  606. return err
  607. } else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil {
  608. return err
  609. }
  610. p.Attrs = b
  611. return nil
  612. }
  613. func (p *sshFxpFsetstatPacket) UnmarshalBinary(b []byte) error {
  614. var err error
  615. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  616. return err
  617. } else if p.Handle, b, err = unmarshalStringSafe(b); err != nil {
  618. return err
  619. } else if p.Flags, b, err = unmarshalUint32Safe(b); err != nil {
  620. return err
  621. }
  622. p.Attrs = b
  623. return nil
  624. }
  625. type sshFxpHandlePacket struct {
  626. ID uint32
  627. Handle string
  628. }
  629. func (p sshFxpHandlePacket) MarshalBinary() ([]byte, error) {
  630. b := []byte{ssh_FXP_HANDLE}
  631. b = marshalUint32(b, p.ID)
  632. b = marshalString(b, p.Handle)
  633. return b, nil
  634. }
  635. type sshFxpStatusPacket struct {
  636. ID uint32
  637. StatusError
  638. }
  639. func (p sshFxpStatusPacket) MarshalBinary() ([]byte, error) {
  640. b := []byte{ssh_FXP_STATUS}
  641. b = marshalUint32(b, p.ID)
  642. b = marshalStatus(b, p.StatusError)
  643. return b, nil
  644. }
  645. type sshFxpDataPacket struct {
  646. ID uint32
  647. Length uint32
  648. Data []byte
  649. }
  650. func (p sshFxpDataPacket) MarshalBinary() ([]byte, error) {
  651. b := []byte{ssh_FXP_DATA}
  652. b = marshalUint32(b, p.ID)
  653. b = marshalUint32(b, p.Length)
  654. b = append(b, p.Data[:p.Length]...)
  655. return b, nil
  656. }
  657. func (p *sshFxpDataPacket) UnmarshalBinary(b []byte) error {
  658. var err error
  659. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  660. return err
  661. } else if p.Length, b, err = unmarshalUint32Safe(b); err != nil {
  662. return err
  663. } else if uint32(len(b)) < p.Length {
  664. return errors.New("truncated packet")
  665. }
  666. p.Data = make([]byte, p.Length)
  667. copy(p.Data, b)
  668. return nil
  669. }
  670. type sshFxpStatvfsPacket struct {
  671. ID uint32
  672. Path string
  673. }
  674. func (p sshFxpStatvfsPacket) id() uint32 { return p.ID }
  675. func (p sshFxpStatvfsPacket) MarshalBinary() ([]byte, error) {
  676. l := 1 + 4 + // type(byte) + uint32
  677. len(p.Path) +
  678. len("statvfs@openssh.com")
  679. b := make([]byte, 0, l)
  680. b = append(b, ssh_FXP_EXTENDED)
  681. b = marshalUint32(b, p.ID)
  682. b = marshalString(b, "statvfs@openssh.com")
  683. b = marshalString(b, p.Path)
  684. return b, nil
  685. }
  686. // A StatVFS contains statistics about a filesystem.
  687. type StatVFS struct {
  688. ID uint32
  689. Bsize uint64 /* file system block size */
  690. Frsize uint64 /* fundamental fs block size */
  691. Blocks uint64 /* number of blocks (unit f_frsize) */
  692. Bfree uint64 /* free blocks in file system */
  693. Bavail uint64 /* free blocks for non-root */
  694. Files uint64 /* total file inodes */
  695. Ffree uint64 /* free file inodes */
  696. Favail uint64 /* free file inodes for to non-root */
  697. Fsid uint64 /* file system id */
  698. Flag uint64 /* bit mask of f_flag values */
  699. Namemax uint64 /* maximum filename length */
  700. }
  701. // TotalSpace calculates the amount of total space in a filesystem.
  702. func (p *StatVFS) TotalSpace() uint64 {
  703. return p.Frsize * p.Blocks
  704. }
  705. // FreeSpace calculates the amount of free space in a filesystem.
  706. func (p *StatVFS) FreeSpace() uint64 {
  707. return p.Frsize * p.Bfree
  708. }
  709. // Convert to ssh_FXP_EXTENDED_REPLY packet binary format
  710. func (p *StatVFS) MarshalBinary() ([]byte, error) {
  711. var buf bytes.Buffer
  712. buf.Write([]byte{ssh_FXP_EXTENDED_REPLY})
  713. err := binary.Write(&buf, binary.BigEndian, p)
  714. return buf.Bytes(), err
  715. }
  716. type sshFxpExtendedPacket struct {
  717. ID uint32
  718. ExtendedRequest string
  719. SpecificPacket interface {
  720. serverRespondablePacket
  721. readonly() bool
  722. }
  723. }
  724. func (p sshFxpExtendedPacket) id() uint32 { return p.ID }
  725. func (p sshFxpExtendedPacket) readonly() bool { return p.SpecificPacket.readonly() }
  726. func (p sshFxpExtendedPacket) respond(svr *Server) error {
  727. return p.SpecificPacket.respond(svr)
  728. }
  729. func (p *sshFxpExtendedPacket) UnmarshalBinary(b []byte) error {
  730. var err error
  731. bOrig := b
  732. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  733. return err
  734. } else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil {
  735. return err
  736. }
  737. // specific unmarshalling
  738. switch p.ExtendedRequest {
  739. case "statvfs@openssh.com":
  740. p.SpecificPacket = &sshFxpExtendedPacketStatVFS{}
  741. default:
  742. return errUnknownExtendedPacket
  743. }
  744. return p.SpecificPacket.UnmarshalBinary(bOrig)
  745. }
  746. type sshFxpExtendedPacketStatVFS struct {
  747. ID uint32
  748. ExtendedRequest string
  749. Path string
  750. }
  751. func (p sshFxpExtendedPacketStatVFS) id() uint32 { return p.ID }
  752. func (p sshFxpExtendedPacketStatVFS) readonly() bool { return true }
  753. func (p *sshFxpExtendedPacketStatVFS) UnmarshalBinary(b []byte) error {
  754. var err error
  755. if p.ID, b, err = unmarshalUint32Safe(b); err != nil {
  756. return err
  757. } else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil {
  758. return err
  759. } else if p.Path, b, err = unmarshalStringSafe(b); err != nil {
  760. return err
  761. }
  762. return nil
  763. }