sftpcommon.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * sftpcommon.c: SFTP code shared between client and server.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <limits.h>
  9. #include "misc.h"
  10. #include "sftp.h"
  11. static void sftp_pkt_BinarySink_write(
  12. BinarySink *bs, const void *data, size_t length)
  13. {
  14. struct sftp_packet *pkt = BinarySink_DOWNCAST(bs, struct sftp_packet);
  15. assert(length <= 0xFFFFFFFFU - pkt->length);
  16. sgrowarrayn_nm(pkt->data, pkt->maxlen, pkt->length, length);
  17. memcpy(pkt->data + pkt->length, data, length);
  18. pkt->length += length;
  19. }
  20. struct sftp_packet *sftp_pkt_init(int type)
  21. {
  22. struct sftp_packet *pkt;
  23. pkt = snew(struct sftp_packet);
  24. pkt->data = NULL;
  25. pkt->savedpos = -1;
  26. pkt->length = 0;
  27. pkt->maxlen = 0;
  28. pkt->type = type;
  29. BinarySink_INIT(pkt, sftp_pkt_BinarySink_write);
  30. put_uint32(pkt, 0); /* length field will be filled in later */
  31. put_byte(pkt, 0); /* so will the type field */
  32. return pkt;
  33. }
  34. void BinarySink_put_fxp_attrs(BinarySink *bs, struct fxp_attrs attrs)
  35. {
  36. put_uint32(bs, attrs.flags);
  37. if (attrs.flags & SSH_FILEXFER_ATTR_SIZE)
  38. put_uint64(bs, attrs.size);
  39. if (attrs.flags & SSH_FILEXFER_ATTR_UIDGID) {
  40. put_uint32(bs, attrs.uid);
  41. put_uint32(bs, attrs.gid);
  42. }
  43. if (attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) {
  44. put_uint32(bs, attrs.permissions);
  45. }
  46. if (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME) {
  47. put_uint32(bs, attrs.atime);
  48. put_uint32(bs, attrs.mtime);
  49. }
  50. if (attrs.flags & SSH_FILEXFER_ATTR_EXTENDED) {
  51. /*
  52. * We currently don't support sending any extended
  53. * attributes.
  54. */
  55. }
  56. }
  57. const struct fxp_attrs no_attrs = { 0 };
  58. #define put_fxp_attrs(bs, attrs) \
  59. BinarySink_put_fxp_attrs(BinarySink_UPCAST(bs), attrs)
  60. bool BinarySource_get_fxp_attrs(BinarySource *src, struct fxp_attrs *attrs)
  61. {
  62. attrs->flags = get_uint32(src);
  63. if (attrs->flags & SSH_FILEXFER_ATTR_SIZE)
  64. attrs->size = get_uint64(src);
  65. if (attrs->flags & SSH_FILEXFER_ATTR_UIDGID) {
  66. attrs->uid = get_uint32(src);
  67. attrs->gid = get_uint32(src);
  68. }
  69. if (attrs->flags & SSH_FILEXFER_ATTR_PERMISSIONS)
  70. attrs->permissions = get_uint32(src);
  71. if (attrs->flags & SSH_FILEXFER_ATTR_ACMODTIME) {
  72. attrs->atime = get_uint32(src);
  73. attrs->mtime = get_uint32(src);
  74. }
  75. if (attrs->flags & SSH_FILEXFER_ATTR_EXTENDED) {
  76. unsigned long count = get_uint32(src);
  77. while (count--) {
  78. if (get_err(src)) {
  79. /* Truncated packet. Don't waste time looking for
  80. * attributes that aren't there. Caller should spot
  81. * the truncation. */
  82. break;
  83. }
  84. /*
  85. * We should try to analyse these, if we ever find one
  86. * we recognise.
  87. */
  88. get_string(src);
  89. get_string(src);
  90. }
  91. }
  92. return true;
  93. }
  94. void sftp_pkt_free(struct sftp_packet *pkt)
  95. {
  96. if (pkt->data)
  97. sfree(pkt->data);
  98. sfree(pkt);
  99. }
  100. void sftp_send_prepare(struct sftp_packet *pkt)
  101. {
  102. PUT_32BIT_MSB_FIRST(pkt->data, pkt->length - 4);
  103. if (pkt->length >= 5) {
  104. /* Rewrite the type code, in case the caller changed its mind
  105. * about pkt->type since calling sftp_pkt_init */
  106. pkt->data[4] = pkt->type;
  107. }
  108. }
  109. struct sftp_packet *sftp_recv_prepare(unsigned length)
  110. {
  111. struct sftp_packet *pkt;
  112. pkt = snew(struct sftp_packet);
  113. pkt->savedpos = 0;
  114. pkt->length = pkt->maxlen = length;
  115. pkt->data = snewn(pkt->length, char);
  116. return pkt;
  117. }
  118. bool sftp_recv_finish(struct sftp_packet *pkt)
  119. {
  120. BinarySource_INIT(pkt, pkt->data, pkt->length);
  121. pkt->type = get_byte(pkt);
  122. return !get_err(pkt);
  123. }