geom_gate userland utility improvements
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

3753 righe
126 KiB

  1. /* Copyright (c) 2004-2008, Sara Golemon <sarag@libssh2.org>
  2. * Copyright (c) 2007 Eli Fant <elifantu@mail.ru>
  3. * Copyright (c) 2009-2019 by Daniel Stenberg
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms,
  7. * with or without modification, are permitted provided
  8. * that the following conditions are met:
  9. *
  10. * Redistributions of source code must retain the above
  11. * copyright notice, this list of conditions and the
  12. * following disclaimer.
  13. *
  14. * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials
  17. * provided with the distribution.
  18. *
  19. * Neither the name of the copyright holder nor the names
  20. * of any other contributors may be used to endorse or
  21. * promote products derived from this software without
  22. * specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  25. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  26. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  37. * OF SUCH DAMAGE.
  38. */
  39. #include <assert.h>
  40. #include "libssh2_priv.h"
  41. #include "libssh2_sftp.h"
  42. #include "channel.h"
  43. #include "session.h"
  44. #include "sftp.h"
  45. /* Note: Version 6 was documented at the time of writing
  46. * However it was marked as "DO NOT IMPLEMENT" due to pending changes
  47. *
  48. * This release of libssh2 implements Version 5 with automatic downgrade
  49. * based on server's declaration
  50. */
  51. /* SFTP packet types */
  52. #define SSH_FXP_INIT 1
  53. #define SSH_FXP_VERSION 2
  54. #define SSH_FXP_OPEN 3
  55. #define SSH_FXP_CLOSE 4
  56. #define SSH_FXP_READ 5
  57. #define SSH_FXP_WRITE 6
  58. #define SSH_FXP_LSTAT 7
  59. #define SSH_FXP_FSTAT 8
  60. #define SSH_FXP_SETSTAT 9
  61. #define SSH_FXP_FSETSTAT 10
  62. #define SSH_FXP_OPENDIR 11
  63. #define SSH_FXP_READDIR 12
  64. #define SSH_FXP_REMOVE 13
  65. #define SSH_FXP_MKDIR 14
  66. #define SSH_FXP_RMDIR 15
  67. #define SSH_FXP_REALPATH 16
  68. #define SSH_FXP_STAT 17
  69. #define SSH_FXP_RENAME 18
  70. #define SSH_FXP_READLINK 19
  71. #define SSH_FXP_SYMLINK 20
  72. #define SSH_FXP_STATUS 101
  73. #define SSH_FXP_HANDLE 102
  74. #define SSH_FXP_DATA 103
  75. #define SSH_FXP_NAME 104
  76. #define SSH_FXP_ATTRS 105
  77. #define SSH_FXP_EXTENDED 200
  78. #define SSH_FXP_EXTENDED_REPLY 201
  79. /* S_IFREG */
  80. #define LIBSSH2_SFTP_ATTR_PFILETYPE_FILE 0100000
  81. /* S_IFDIR */
  82. #define LIBSSH2_SFTP_ATTR_PFILETYPE_DIR 0040000
  83. #define SSH_FXE_STATVFS_ST_RDONLY 0x00000001
  84. #define SSH_FXE_STATVFS_ST_NOSUID 0x00000002
  85. /* This is the maximum packet length to accept, as larger than this indicate
  86. some kind of server problem. */
  87. #define LIBSSH2_SFTP_PACKET_MAXLEN (256 * 1024)
  88. static int sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type,
  89. uint32_t request_id, unsigned char **data,
  90. size_t *data_len);
  91. static void sftp_packet_flush(LIBSSH2_SFTP *sftp);
  92. /* sftp_attrsize
  93. * Size that attr with this flagset will occupy when turned into a bin struct
  94. */
  95. static int sftp_attrsize(unsigned long flags)
  96. {
  97. return (4 + /* flags(4) */
  98. ((flags & LIBSSH2_SFTP_ATTR_SIZE) ? 8 : 0) +
  99. ((flags & LIBSSH2_SFTP_ATTR_UIDGID) ? 8 : 0) +
  100. ((flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) ? 4 : 0) +
  101. ((flags & LIBSSH2_SFTP_ATTR_ACMODTIME) ? 8 : 0));
  102. /* atime + mtime as u32 */
  103. }
  104. /* _libssh2_store_u64
  105. */
  106. static void _libssh2_store_u64(unsigned char **ptr, libssh2_uint64_t value)
  107. {
  108. uint32_t msl = (uint32_t)(value >> 32);
  109. unsigned char *buf = *ptr;
  110. buf[0] = (unsigned char)((msl >> 24) & 0xFF);
  111. buf[1] = (unsigned char)((msl >> 16) & 0xFF);
  112. buf[2] = (unsigned char)((msl >> 8) & 0xFF);
  113. buf[3] = (unsigned char)( msl & 0xFF);
  114. buf[4] = (unsigned char)((value >> 24) & 0xFF);
  115. buf[5] = (unsigned char)((value >> 16) & 0xFF);
  116. buf[6] = (unsigned char)((value >> 8) & 0xFF);
  117. buf[7] = (unsigned char)( value & 0xFF);
  118. *ptr += 8;
  119. }
  120. /*
  121. * Search list of zombied FXP_READ request IDs.
  122. *
  123. * Returns NULL if ID not in list.
  124. */
  125. static struct sftp_zombie_requests *
  126. find_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
  127. {
  128. struct sftp_zombie_requests *zombie =
  129. _libssh2_list_first(&sftp->zombie_requests);
  130. while(zombie) {
  131. if(zombie->request_id == request_id)
  132. break;
  133. else
  134. zombie = _libssh2_list_next(&zombie->node);
  135. }
  136. return zombie;
  137. }
  138. static void
  139. remove_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
  140. {
  141. LIBSSH2_SESSION *session = sftp->channel->session;
  142. struct sftp_zombie_requests *zombie = find_zombie_request(sftp,
  143. request_id);
  144. if(zombie) {
  145. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  146. "Removing request ID %ld from the list of "
  147. "zombie requests",
  148. request_id);
  149. _libssh2_list_remove(&zombie->node);
  150. LIBSSH2_FREE(session, zombie);
  151. }
  152. }
  153. static int
  154. add_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id)
  155. {
  156. LIBSSH2_SESSION *session = sftp->channel->session;
  157. struct sftp_zombie_requests *zombie;
  158. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  159. "Marking request ID %ld as a zombie request", request_id);
  160. zombie = LIBSSH2_ALLOC(sftp->channel->session,
  161. sizeof(struct sftp_zombie_requests));
  162. if(!zombie)
  163. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  164. "malloc fail for zombie request ID");
  165. else {
  166. zombie->request_id = request_id;
  167. _libssh2_list_add(&sftp->zombie_requests, &zombie->node);
  168. return LIBSSH2_ERROR_NONE;
  169. }
  170. }
  171. /*
  172. * sftp_packet_add
  173. *
  174. * Add a packet to the SFTP packet brigade
  175. */
  176. static int
  177. sftp_packet_add(LIBSSH2_SFTP *sftp, unsigned char *data,
  178. size_t data_len)
  179. {
  180. LIBSSH2_SESSION *session = sftp->channel->session;
  181. LIBSSH2_SFTP_PACKET *packet;
  182. uint32_t request_id;
  183. if(data_len < 5) {
  184. return LIBSSH2_ERROR_OUT_OF_BOUNDARY;
  185. }
  186. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  187. "Received packet type %d (len %d)",
  188. (int) data[0], data_len);
  189. /*
  190. * Experience shows that if we mess up EAGAIN handling somewhere or
  191. * otherwise get out of sync with the channel, this is where we first get
  192. * a wrong byte and if so we need to bail out at once to aid tracking the
  193. * problem better.
  194. */
  195. switch(data[0]) {
  196. case SSH_FXP_INIT:
  197. case SSH_FXP_VERSION:
  198. case SSH_FXP_OPEN:
  199. case SSH_FXP_CLOSE:
  200. case SSH_FXP_READ:
  201. case SSH_FXP_WRITE:
  202. case SSH_FXP_LSTAT:
  203. case SSH_FXP_FSTAT:
  204. case SSH_FXP_SETSTAT:
  205. case SSH_FXP_FSETSTAT:
  206. case SSH_FXP_OPENDIR:
  207. case SSH_FXP_READDIR:
  208. case SSH_FXP_REMOVE:
  209. case SSH_FXP_MKDIR:
  210. case SSH_FXP_RMDIR:
  211. case SSH_FXP_REALPATH:
  212. case SSH_FXP_STAT:
  213. case SSH_FXP_RENAME:
  214. case SSH_FXP_READLINK:
  215. case SSH_FXP_SYMLINK:
  216. case SSH_FXP_STATUS:
  217. case SSH_FXP_HANDLE:
  218. case SSH_FXP_DATA:
  219. case SSH_FXP_NAME:
  220. case SSH_FXP_ATTRS:
  221. case SSH_FXP_EXTENDED:
  222. case SSH_FXP_EXTENDED_REPLY:
  223. break;
  224. default:
  225. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  226. "Out of sync with the world");
  227. }
  228. request_id = _libssh2_ntohu32(&data[1]);
  229. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Received packet id %d",
  230. request_id);
  231. /* Don't add the packet if it answers a request we've given up on. */
  232. if((data[0] == SSH_FXP_STATUS || data[0] == SSH_FXP_DATA)
  233. && find_zombie_request(sftp, request_id)) {
  234. /* If we get here, the file ended before the response arrived. We
  235. are no longer interested in the request so we discard it */
  236. LIBSSH2_FREE(session, data);
  237. remove_zombie_request(sftp, request_id);
  238. return LIBSSH2_ERROR_NONE;
  239. }
  240. packet = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_PACKET));
  241. if(!packet) {
  242. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  243. "Unable to allocate datablock for SFTP packet");
  244. }
  245. packet->data = data;
  246. packet->data_len = data_len;
  247. packet->request_id = request_id;
  248. _libssh2_list_add(&sftp->packets, &packet->node);
  249. return LIBSSH2_ERROR_NONE;
  250. }
  251. /*
  252. * sftp_packet_read
  253. *
  254. * Frame an SFTP packet off the channel
  255. */
  256. static int
  257. sftp_packet_read(LIBSSH2_SFTP *sftp)
  258. {
  259. LIBSSH2_CHANNEL *channel = sftp->channel;
  260. LIBSSH2_SESSION *session = channel->session;
  261. unsigned char *packet = NULL;
  262. ssize_t rc;
  263. unsigned long recv_window;
  264. int packet_type;
  265. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "recv packet");
  266. switch(sftp->packet_state) {
  267. case libssh2_NB_state_sent: /* EAGAIN from window adjusting */
  268. sftp->packet_state = libssh2_NB_state_idle;
  269. packet = sftp->partial_packet;
  270. goto window_adjust;
  271. case libssh2_NB_state_sent1: /* EAGAIN from channel read */
  272. sftp->packet_state = libssh2_NB_state_idle;
  273. packet = sftp->partial_packet;
  274. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  275. "partial read cont, len: %lu", sftp->partial_len);
  276. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  277. "partial read cont, already recvd: %lu",
  278. sftp->partial_received);
  279. /* fall-through */
  280. default:
  281. if(!packet) {
  282. /* only do this if there's not already a packet buffer allocated
  283. to use */
  284. /* each packet starts with a 32 bit length field */
  285. rc = _libssh2_channel_read(channel, 0,
  286. (char *)&sftp->partial_size[
  287. sftp->partial_size_len],
  288. 4 - sftp->partial_size_len);
  289. if(rc == LIBSSH2_ERROR_EAGAIN)
  290. return rc;
  291. else if(rc < 0)
  292. return _libssh2_error(session, rc, "channel read");
  293. sftp->partial_size_len += rc;
  294. if(4 != sftp->partial_size_len)
  295. /* we got a short read for the length part */
  296. return LIBSSH2_ERROR_EAGAIN;
  297. sftp->partial_len = _libssh2_ntohu32(sftp->partial_size);
  298. /* make sure we don't proceed if the packet size is unreasonably
  299. large */
  300. if(sftp->partial_len > LIBSSH2_SFTP_PACKET_MAXLEN) {
  301. libssh2_channel_flush(channel);
  302. sftp->partial_size_len = 0;
  303. return _libssh2_error(session,
  304. LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED,
  305. "SFTP packet too large");
  306. }
  307. if(sftp->partial_len == 0)
  308. return _libssh2_error(session,
  309. LIBSSH2_ERROR_ALLOC,
  310. "Unable to allocate empty SFTP packet");
  311. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  312. "Data begin - Packet Length: %lu",
  313. sftp->partial_len);
  314. packet = LIBSSH2_ALLOC(session, sftp->partial_len);
  315. if(!packet)
  316. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  317. "Unable to allocate SFTP packet");
  318. sftp->partial_size_len = 0;
  319. sftp->partial_received = 0; /* how much of the packet already
  320. received */
  321. sftp->partial_packet = packet;
  322. window_adjust:
  323. recv_window = libssh2_channel_window_read_ex(channel, NULL, NULL);
  324. if(sftp->partial_len > recv_window) {
  325. /* ask for twice the data amount we need at once */
  326. rc = _libssh2_channel_receive_window_adjust(channel,
  327. sftp->partial_len
  328. * 2,
  329. 1, NULL);
  330. /* store the state so that we continue with the correct
  331. operation at next invoke */
  332. sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN)?
  333. libssh2_NB_state_sent:
  334. libssh2_NB_state_idle;
  335. if(rc == LIBSSH2_ERROR_EAGAIN)
  336. return rc;
  337. }
  338. }
  339. /* Read as much of the packet as we can */
  340. while(sftp->partial_len > sftp->partial_received) {
  341. rc = _libssh2_channel_read(channel, 0,
  342. (char *)&packet[sftp->partial_received],
  343. sftp->partial_len -
  344. sftp->partial_received);
  345. if(rc == LIBSSH2_ERROR_EAGAIN) {
  346. /*
  347. * We received EAGAIN, save what we have and return EAGAIN to
  348. * the caller. Set 'partial_packet' so that this function
  349. * knows how to continue on the next invoke.
  350. */
  351. sftp->packet_state = libssh2_NB_state_sent1;
  352. return rc;
  353. }
  354. else if(rc < 0) {
  355. LIBSSH2_FREE(session, packet);
  356. sftp->partial_packet = NULL;
  357. return _libssh2_error(session, rc,
  358. "Error waiting for SFTP packet");
  359. }
  360. sftp->partial_received += rc;
  361. }
  362. sftp->partial_packet = NULL;
  363. /* sftp_packet_add takes ownership of the packet and might free it
  364. so we take a copy of the packet type before we call it. */
  365. packet_type = packet[0];
  366. rc = sftp_packet_add(sftp, packet, sftp->partial_len);
  367. if(rc) {
  368. LIBSSH2_FREE(session, packet);
  369. return rc;
  370. }
  371. else {
  372. return packet_type;
  373. }
  374. }
  375. /* WON'T REACH */
  376. }
  377. /*
  378. * sftp_packetlist_flush
  379. *
  380. * Remove all pending packets in the packet_list and the corresponding one(s)
  381. * in the SFTP packet brigade.
  382. */
  383. static void sftp_packetlist_flush(LIBSSH2_SFTP_HANDLE *handle)
  384. {
  385. struct sftp_pipeline_chunk *chunk;
  386. LIBSSH2_SFTP *sftp = handle->sftp;
  387. LIBSSH2_SESSION *session = sftp->channel->session;
  388. /* remove pending packets, if any */
  389. chunk = _libssh2_list_first(&handle->packet_list);
  390. while(chunk) {
  391. unsigned char *data;
  392. size_t data_len;
  393. int rc;
  394. struct sftp_pipeline_chunk *next = _libssh2_list_next(&chunk->node);
  395. rc = sftp_packet_ask(sftp, SSH_FXP_STATUS,
  396. chunk->request_id, &data, &data_len);
  397. if(rc)
  398. rc = sftp_packet_ask(sftp, SSH_FXP_DATA,
  399. chunk->request_id, &data, &data_len);
  400. if(!rc)
  401. /* we found a packet, free it */
  402. LIBSSH2_FREE(session, data);
  403. else if(chunk->sent)
  404. /* there was no incoming packet for this request, mark this
  405. request as a zombie if it ever sent the request */
  406. add_zombie_request(sftp, chunk->request_id);
  407. _libssh2_list_remove(&chunk->node);
  408. LIBSSH2_FREE(session, chunk);
  409. chunk = next;
  410. }
  411. }
  412. /*
  413. * sftp_packet_ask()
  414. *
  415. * Checks if there's a matching SFTP packet available.
  416. */
  417. static int
  418. sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type,
  419. uint32_t request_id, unsigned char **data,
  420. size_t *data_len)
  421. {
  422. LIBSSH2_SESSION *session = sftp->channel->session;
  423. LIBSSH2_SFTP_PACKET *packet = _libssh2_list_first(&sftp->packets);
  424. if(!packet)
  425. return -1;
  426. /* Special consideration when getting VERSION packet */
  427. while(packet) {
  428. if((packet->data[0] == packet_type) &&
  429. ((packet_type == SSH_FXP_VERSION) ||
  430. (packet->request_id == request_id))) {
  431. /* Match! Fetch the data */
  432. *data = packet->data;
  433. *data_len = packet->data_len;
  434. /* unlink and free this struct */
  435. _libssh2_list_remove(&packet->node);
  436. LIBSSH2_FREE(session, packet);
  437. return 0;
  438. }
  439. /* check next struct in the list */
  440. packet = _libssh2_list_next(&packet->node);
  441. }
  442. return -1;
  443. }
  444. /* sftp_packet_require
  445. * A la libssh2_packet_require
  446. */
  447. static int
  448. sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type,
  449. uint32_t request_id, unsigned char **data,
  450. size_t *data_len, size_t required_size)
  451. {
  452. LIBSSH2_SESSION *session = sftp->channel->session;
  453. int rc;
  454. if(data == NULL || data_len == NULL || required_size == 0) {
  455. return LIBSSH2_ERROR_BAD_USE;
  456. }
  457. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Requiring packet %d id %ld",
  458. (int) packet_type, request_id);
  459. if(sftp_packet_ask(sftp, packet_type, request_id, data, data_len) == 0) {
  460. /* The right packet was available in the packet brigade */
  461. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Got %d",
  462. (int) packet_type);
  463. if (*data_len < required_size) {
  464. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  465. }
  466. return LIBSSH2_ERROR_NONE;
  467. }
  468. while(session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
  469. rc = sftp_packet_read(sftp);
  470. if(rc < 0)
  471. return rc;
  472. /* data was read, check the queue again */
  473. if(!sftp_packet_ask(sftp, packet_type, request_id, data, data_len)) {
  474. /* The right packet was available in the packet brigade */
  475. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Got %d",
  476. (int) packet_type);
  477. if (*data_len < required_size) {
  478. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  479. }
  480. return LIBSSH2_ERROR_NONE;
  481. }
  482. }
  483. /* Only reached if the socket died */
  484. return LIBSSH2_ERROR_SOCKET_DISCONNECT;
  485. }
  486. /* sftp_packet_requirev
  487. * Require one of N possible responses
  488. */
  489. static int
  490. sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses,
  491. const unsigned char *valid_responses,
  492. uint32_t request_id, unsigned char **data,
  493. size_t *data_len, size_t required_size)
  494. {
  495. int i;
  496. int rc;
  497. if(data == NULL || data_len == NULL || required_size == 0) {
  498. return LIBSSH2_ERROR_BAD_USE;
  499. }
  500. /* If no timeout is active, start a new one */
  501. if(sftp->requirev_start == 0)
  502. sftp->requirev_start = time(NULL);
  503. while(sftp->channel->session->socket_state == LIBSSH2_SOCKET_CONNECTED) {
  504. for(i = 0; i < num_valid_responses; i++) {
  505. if(sftp_packet_ask(sftp, valid_responses[i], request_id,
  506. data, data_len) == 0) {
  507. /*
  508. * Set to zero before all returns to say
  509. * the timeout is not active
  510. */
  511. sftp->requirev_start = 0;
  512. if (*data_len < required_size) {
  513. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  514. }
  515. return LIBSSH2_ERROR_NONE;
  516. }
  517. }
  518. rc = sftp_packet_read(sftp);
  519. if((rc < 0) && (rc != LIBSSH2_ERROR_EAGAIN)) {
  520. sftp->requirev_start = 0;
  521. return rc;
  522. }
  523. else if(rc <= 0) {
  524. /* prevent busy-looping */
  525. long left =
  526. LIBSSH2_READ_TIMEOUT -
  527. (long)(time(NULL) - sftp->requirev_start);
  528. if(left <= 0) {
  529. sftp->requirev_start = 0;
  530. return LIBSSH2_ERROR_TIMEOUT;
  531. }
  532. else if(rc == LIBSSH2_ERROR_EAGAIN) {
  533. return rc;
  534. }
  535. }
  536. }
  537. sftp->requirev_start = 0;
  538. /* Only reached if the socket died */
  539. return LIBSSH2_ERROR_SOCKET_DISCONNECT;
  540. }
  541. /* sftp_attr2bin
  542. * Populate attributes into an SFTP block
  543. */
  544. static ssize_t
  545. sftp_attr2bin(unsigned char *p, const LIBSSH2_SFTP_ATTRIBUTES * attrs)
  546. {
  547. unsigned char *s = p;
  548. uint32_t flag_mask =
  549. LIBSSH2_SFTP_ATTR_SIZE | LIBSSH2_SFTP_ATTR_UIDGID |
  550. LIBSSH2_SFTP_ATTR_PERMISSIONS | LIBSSH2_SFTP_ATTR_ACMODTIME;
  551. /* TODO: When we add SFTP4+ functionality flag_mask can get additional
  552. bits */
  553. if(!attrs) {
  554. _libssh2_htonu32(s, 0);
  555. return 4;
  556. }
  557. _libssh2_store_u32(&s, attrs->flags & flag_mask);
  558. if(attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
  559. _libssh2_store_u64(&s, attrs->filesize);
  560. }
  561. if(attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
  562. _libssh2_store_u32(&s, attrs->uid);
  563. _libssh2_store_u32(&s, attrs->gid);
  564. }
  565. if(attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
  566. _libssh2_store_u32(&s, attrs->permissions);
  567. }
  568. if(attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
  569. _libssh2_store_u32(&s, attrs->atime);
  570. _libssh2_store_u32(&s, attrs->mtime);
  571. }
  572. return (s - p);
  573. }
  574. /* sftp_bin2attr
  575. */
  576. static int
  577. sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, const unsigned char *p,
  578. size_t data_len)
  579. {
  580. struct string_buf buf;
  581. uint32_t flags = 0;
  582. buf.data = (unsigned char *)p;
  583. buf.dataptr = buf.data;
  584. buf.len = data_len;
  585. if(_libssh2_get_u32(&buf, &flags) != 0) {
  586. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  587. }
  588. attrs->flags = flags;
  589. if(attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) {
  590. if(_libssh2_get_u64(&buf, &(attrs->filesize)) != 0) {
  591. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  592. }
  593. }
  594. if(attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) {
  595. uint32_t uid = 0;
  596. uint32_t gid = 0;
  597. if(_libssh2_get_u32(&buf, &uid) != 0 ||
  598. _libssh2_get_u32(&buf, &gid) != 0) {
  599. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  600. }
  601. attrs->uid = uid;
  602. attrs->gid = gid;
  603. }
  604. if(attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
  605. uint32_t permissions;
  606. if(_libssh2_get_u32(&buf, &permissions) != 0) {
  607. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  608. }
  609. attrs->permissions = permissions;
  610. }
  611. if(attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
  612. uint32_t atime;
  613. uint32_t mtime;
  614. if(_libssh2_get_u32(&buf, &atime) != 0 ||
  615. _libssh2_get_u32(&buf, &mtime) != 0) {
  616. return LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  617. }
  618. attrs->atime = atime;
  619. attrs->mtime = mtime;
  620. }
  621. return (buf.dataptr - buf.data);
  622. }
  623. /* ************
  624. * SFTP API *
  625. ************ */
  626. LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor);
  627. /* libssh2_sftp_dtor
  628. * Shutdown an SFTP stream when the channel closes
  629. */
  630. LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor)
  631. {
  632. LIBSSH2_SFTP *sftp = (LIBSSH2_SFTP *) (*channel_abstract);
  633. (void) session_abstract;
  634. (void) channel;
  635. /* Free the partial packet storage for sftp_packet_read */
  636. if(sftp->partial_packet) {
  637. LIBSSH2_FREE(session, sftp->partial_packet);
  638. }
  639. /* Free the packet storage for _libssh2_sftp_packet_readdir */
  640. if(sftp->readdir_packet) {
  641. LIBSSH2_FREE(session, sftp->readdir_packet);
  642. }
  643. LIBSSH2_FREE(session, sftp);
  644. }
  645. /*
  646. * sftp_init
  647. *
  648. * Startup an SFTP session
  649. */
  650. static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
  651. {
  652. unsigned char *data;
  653. size_t data_len;
  654. ssize_t rc;
  655. LIBSSH2_SFTP *sftp_handle;
  656. struct string_buf buf;
  657. unsigned char *endp;
  658. if(session->sftpInit_state == libssh2_NB_state_idle) {
  659. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  660. "Initializing SFTP subsystem");
  661. /*
  662. * The 'sftpInit_sftp' and 'sftpInit_channel' struct fields within the
  663. * session struct are only to be used during the setup phase. As soon
  664. * as the SFTP session is created they are cleared and can thus be
  665. * re-used again to allow any amount of SFTP handles per sessions.
  666. *
  667. * Note that you MUST NOT try to call libssh2_sftp_init() again to get
  668. * another handle until the previous call has finished and either
  669. * successfully made a handle or failed and returned error (not
  670. * including *EAGAIN).
  671. */
  672. assert(session->sftpInit_sftp == NULL);
  673. session->sftpInit_sftp = NULL;
  674. session->sftpInit_state = libssh2_NB_state_created;
  675. }
  676. sftp_handle = session->sftpInit_sftp;
  677. if(session->sftpInit_state == libssh2_NB_state_created) {
  678. session->sftpInit_channel =
  679. _libssh2_channel_open(session, "session", sizeof("session") - 1,
  680. LIBSSH2_CHANNEL_WINDOW_DEFAULT,
  681. LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0);
  682. if(!session->sftpInit_channel) {
  683. if(libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) {
  684. _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
  685. "Would block starting up channel");
  686. }
  687. else {
  688. _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
  689. "Unable to startup channel");
  690. session->sftpInit_state = libssh2_NB_state_idle;
  691. }
  692. return NULL;
  693. }
  694. session->sftpInit_state = libssh2_NB_state_sent;
  695. }
  696. if(session->sftpInit_state == libssh2_NB_state_sent) {
  697. int ret = _libssh2_channel_process_startup(session->sftpInit_channel,
  698. "subsystem",
  699. sizeof("subsystem") - 1,
  700. "sftp",
  701. strlen("sftp"));
  702. if(ret == LIBSSH2_ERROR_EAGAIN) {
  703. _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
  704. "Would block to request SFTP subsystem");
  705. return NULL;
  706. }
  707. else if(ret) {
  708. _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE,
  709. "Unable to request SFTP subsystem");
  710. goto sftp_init_error;
  711. }
  712. session->sftpInit_state = libssh2_NB_state_sent1;
  713. }
  714. if(session->sftpInit_state == libssh2_NB_state_sent1) {
  715. rc = _libssh2_channel_extended_data(session->sftpInit_channel,
  716. LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE);
  717. if(rc == LIBSSH2_ERROR_EAGAIN) {
  718. _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
  719. "Would block requesting handle extended data");
  720. return NULL;
  721. }
  722. sftp_handle =
  723. session->sftpInit_sftp =
  724. LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP));
  725. if(!sftp_handle) {
  726. _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  727. "Unable to allocate a new SFTP structure");
  728. goto sftp_init_error;
  729. }
  730. sftp_handle->channel = session->sftpInit_channel;
  731. sftp_handle->request_id = 0;
  732. _libssh2_htonu32(session->sftpInit_buffer, 5);
  733. session->sftpInit_buffer[4] = SSH_FXP_INIT;
  734. _libssh2_htonu32(session->sftpInit_buffer + 5, LIBSSH2_SFTP_VERSION);
  735. session->sftpInit_sent = 0; /* nothing's sent yet */
  736. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  737. "Sending FXP_INIT packet advertising "
  738. "version %d support",
  739. (int) LIBSSH2_SFTP_VERSION);
  740. session->sftpInit_state = libssh2_NB_state_sent2;
  741. }
  742. if(session->sftpInit_state == libssh2_NB_state_sent2) {
  743. /* sent off what's left of the init buffer to send */
  744. rc = _libssh2_channel_write(session->sftpInit_channel, 0,
  745. session->sftpInit_buffer +
  746. session->sftpInit_sent,
  747. 9 - session->sftpInit_sent);
  748. if(rc == LIBSSH2_ERROR_EAGAIN) {
  749. _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
  750. "Would block sending SSH_FXP_INIT");
  751. return NULL;
  752. }
  753. else if(rc < 0) {
  754. _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  755. "Unable to send SSH_FXP_INIT");
  756. goto sftp_init_error;
  757. }
  758. else {
  759. /* add up the number of bytes sent */
  760. session->sftpInit_sent += rc;
  761. if(session->sftpInit_sent == 9)
  762. /* move on */
  763. session->sftpInit_state = libssh2_NB_state_sent3;
  764. /* if less than 9, we remain in this state to send more later on */
  765. }
  766. }
  767. rc = sftp_packet_require(sftp_handle, SSH_FXP_VERSION,
  768. 0, &data, &data_len, 5);
  769. if(rc == LIBSSH2_ERROR_EAGAIN) {
  770. _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
  771. "Would block receiving SSH_FXP_VERSION");
  772. return NULL;
  773. }
  774. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  775. if(data_len > 0) {
  776. LIBSSH2_FREE(session, data);
  777. }
  778. _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  779. "Invalid SSH_FXP_VERSION response");
  780. goto sftp_init_error;
  781. }
  782. else if(rc) {
  783. _libssh2_error(session, rc,
  784. "Timeout waiting for response from SFTP subsystem");
  785. goto sftp_init_error;
  786. }
  787. buf.data = data;
  788. buf.dataptr = buf.data + 1;
  789. buf.len = data_len;
  790. endp = &buf.data[data_len];
  791. if(_libssh2_get_u32(&buf, &(sftp_handle->version)) != 0) {
  792. LIBSSH2_FREE(session, data);
  793. rc = LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  794. goto sftp_init_error;
  795. }
  796. if(sftp_handle->version > LIBSSH2_SFTP_VERSION) {
  797. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  798. "Truncating remote SFTP version from %lu",
  799. sftp_handle->version);
  800. sftp_handle->version = LIBSSH2_SFTP_VERSION;
  801. }
  802. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  803. "Enabling SFTP version %lu compatibility",
  804. sftp_handle->version);
  805. while(buf.dataptr < endp) {
  806. unsigned char *extname, *extdata;
  807. if(_libssh2_get_string(&buf, &extname, NULL)) {
  808. LIBSSH2_FREE(session, data);
  809. _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
  810. "Data too short when extracting extname");
  811. goto sftp_init_error;
  812. }
  813. if(_libssh2_get_string(&buf, &extdata, NULL)) {
  814. LIBSSH2_FREE(session, data);
  815. _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
  816. "Data too short when extracting extdata");
  817. goto sftp_init_error;
  818. }
  819. }
  820. LIBSSH2_FREE(session, data);
  821. /* Make sure that when the channel gets closed, the SFTP service is shut
  822. down too */
  823. sftp_handle->channel->abstract = sftp_handle;
  824. sftp_handle->channel->close_cb = libssh2_sftp_dtor;
  825. session->sftpInit_state = libssh2_NB_state_idle;
  826. /* clear the sftp and channel pointers in this session struct now */
  827. session->sftpInit_sftp = NULL;
  828. session->sftpInit_channel = NULL;
  829. _libssh2_list_init(&sftp_handle->sftp_handles);
  830. return sftp_handle;
  831. sftp_init_error:
  832. while(_libssh2_channel_free(session->sftpInit_channel) ==
  833. LIBSSH2_ERROR_EAGAIN);
  834. session->sftpInit_channel = NULL;
  835. if(session->sftpInit_sftp) {
  836. LIBSSH2_FREE(session, session->sftpInit_sftp);
  837. session->sftpInit_sftp = NULL;
  838. }
  839. session->sftpInit_state = libssh2_NB_state_idle;
  840. return NULL;
  841. }
  842. /*
  843. * libssh2_sftp_init
  844. *
  845. * Startup an SFTP session
  846. */
  847. LIBSSH2_API LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session)
  848. {
  849. LIBSSH2_SFTP *ptr;
  850. if(!session)
  851. return NULL;
  852. if(!(session->state & LIBSSH2_STATE_AUTHENTICATED)) {
  853. _libssh2_error(session, LIBSSH2_ERROR_INVAL,
  854. "session not authenticated yet");
  855. return NULL;
  856. }
  857. BLOCK_ADJUST_ERRNO(ptr, session, sftp_init(session));
  858. return ptr;
  859. }
  860. /*
  861. * sftp_shutdown
  862. *
  863. * Shuts down the SFTP subsystem
  864. */
  865. static int
  866. sftp_shutdown(LIBSSH2_SFTP *sftp)
  867. {
  868. int rc;
  869. LIBSSH2_SESSION *session = sftp->channel->session;
  870. /*
  871. * Make sure all memory used in the state variables are free
  872. */
  873. if(sftp->partial_packet) {
  874. LIBSSH2_FREE(session, sftp->partial_packet);
  875. sftp->partial_packet = NULL;
  876. }
  877. if(sftp->open_packet) {
  878. LIBSSH2_FREE(session, sftp->open_packet);
  879. sftp->open_packet = NULL;
  880. }
  881. if(sftp->readdir_packet) {
  882. LIBSSH2_FREE(session, sftp->readdir_packet);
  883. sftp->readdir_packet = NULL;
  884. }
  885. if(sftp->fstat_packet) {
  886. LIBSSH2_FREE(session, sftp->fstat_packet);
  887. sftp->fstat_packet = NULL;
  888. }
  889. if(sftp->unlink_packet) {
  890. LIBSSH2_FREE(session, sftp->unlink_packet);
  891. sftp->unlink_packet = NULL;
  892. }
  893. if(sftp->rename_packet) {
  894. LIBSSH2_FREE(session, sftp->rename_packet);
  895. sftp->rename_packet = NULL;
  896. }
  897. if(sftp->fstatvfs_packet) {
  898. LIBSSH2_FREE(session, sftp->fstatvfs_packet);
  899. sftp->fstatvfs_packet = NULL;
  900. }
  901. if(sftp->statvfs_packet) {
  902. LIBSSH2_FREE(session, sftp->statvfs_packet);
  903. sftp->statvfs_packet = NULL;
  904. }
  905. if(sftp->mkdir_packet) {
  906. LIBSSH2_FREE(session, sftp->mkdir_packet);
  907. sftp->mkdir_packet = NULL;
  908. }
  909. if(sftp->rmdir_packet) {
  910. LIBSSH2_FREE(session, sftp->rmdir_packet);
  911. sftp->rmdir_packet = NULL;
  912. }
  913. if(sftp->stat_packet) {
  914. LIBSSH2_FREE(session, sftp->stat_packet);
  915. sftp->stat_packet = NULL;
  916. }
  917. if(sftp->symlink_packet) {
  918. LIBSSH2_FREE(session, sftp->symlink_packet);
  919. sftp->symlink_packet = NULL;
  920. }
  921. if(sftp->fsync_packet) {
  922. LIBSSH2_FREE(session, sftp->fsync_packet);
  923. sftp->fsync_packet = NULL;
  924. }
  925. sftp_packet_flush(sftp);
  926. /* TODO: We should consider walking over the sftp_handles list and kill
  927. * any remaining sftp handles ... */
  928. rc = _libssh2_channel_free(sftp->channel);
  929. return rc;
  930. }
  931. /* libssh2_sftp_shutdown
  932. * Shutsdown the SFTP subsystem
  933. */
  934. LIBSSH2_API int
  935. libssh2_sftp_shutdown(LIBSSH2_SFTP *sftp)
  936. {
  937. int rc;
  938. if(!sftp)
  939. return LIBSSH2_ERROR_BAD_USE;
  940. BLOCK_ADJUST(rc, sftp->channel->session, sftp_shutdown(sftp));
  941. return rc;
  942. }
  943. /* *******************************
  944. * SFTP File and Directory Ops *
  945. ******************************* */
  946. /* sftp_open
  947. */
  948. static LIBSSH2_SFTP_HANDLE *
  949. sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
  950. size_t filename_len, uint32_t flags, long mode,
  951. int open_type)
  952. {
  953. LIBSSH2_CHANNEL *channel = sftp->channel;
  954. LIBSSH2_SESSION *session = channel->session;
  955. LIBSSH2_SFTP_HANDLE *fp;
  956. LIBSSH2_SFTP_ATTRIBUTES attrs = {
  957. LIBSSH2_SFTP_ATTR_PERMISSIONS, 0, 0, 0, 0, 0, 0
  958. };
  959. unsigned char *s;
  960. ssize_t rc;
  961. int open_file = (open_type == LIBSSH2_SFTP_OPENFILE)?1:0;
  962. if(sftp->open_state == libssh2_NB_state_idle) {
  963. /* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) +
  964. flags(4) */
  965. sftp->open_packet_len = filename_len + 13 +
  966. (open_file? (4 +
  967. sftp_attrsize(LIBSSH2_SFTP_ATTR_PERMISSIONS)) : 0);
  968. /* surprise! this starts out with nothing sent */
  969. sftp->open_packet_sent = 0;
  970. s = sftp->open_packet = LIBSSH2_ALLOC(session, sftp->open_packet_len);
  971. if(!sftp->open_packet) {
  972. _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  973. "Unable to allocate memory for FXP_OPEN or "
  974. "FXP_OPENDIR packet");
  975. return NULL;
  976. }
  977. /* Filetype in SFTP 3 and earlier */
  978. attrs.permissions = mode |
  979. (open_file ? LIBSSH2_SFTP_ATTR_PFILETYPE_FILE :
  980. LIBSSH2_SFTP_ATTR_PFILETYPE_DIR);
  981. _libssh2_store_u32(&s, sftp->open_packet_len - 4);
  982. *(s++) = open_file? SSH_FXP_OPEN : SSH_FXP_OPENDIR;
  983. sftp->open_request_id = sftp->request_id++;
  984. _libssh2_store_u32(&s, sftp->open_request_id);
  985. _libssh2_store_str(&s, filename, filename_len);
  986. if(open_file) {
  987. _libssh2_store_u32(&s, flags);
  988. s += sftp_attr2bin(s, &attrs);
  989. }
  990. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Sending %s open request",
  991. open_file? "file" : "directory");
  992. sftp->open_state = libssh2_NB_state_created;
  993. }
  994. if(sftp->open_state == libssh2_NB_state_created) {
  995. rc = _libssh2_channel_write(channel, 0, sftp->open_packet+
  996. sftp->open_packet_sent,
  997. sftp->open_packet_len -
  998. sftp->open_packet_sent);
  999. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1000. _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
  1001. "Would block sending FXP_OPEN or "
  1002. "FXP_OPENDIR command");
  1003. return NULL;
  1004. }
  1005. else if(rc < 0) {
  1006. _libssh2_error(session, rc, "Unable to send FXP_OPEN*");
  1007. LIBSSH2_FREE(session, sftp->open_packet);
  1008. sftp->open_packet = NULL;
  1009. sftp->open_state = libssh2_NB_state_idle;
  1010. return NULL;
  1011. }
  1012. /* bump the sent counter and remain in this state until the whole
  1013. data is off */
  1014. sftp->open_packet_sent += rc;
  1015. if(sftp->open_packet_len == sftp->open_packet_sent) {
  1016. LIBSSH2_FREE(session, sftp->open_packet);
  1017. sftp->open_packet = NULL;
  1018. sftp->open_state = libssh2_NB_state_sent;
  1019. }
  1020. }
  1021. if(sftp->open_state == libssh2_NB_state_sent) {
  1022. size_t data_len;
  1023. unsigned char *data;
  1024. static const unsigned char fopen_responses[2] =
  1025. { SSH_FXP_HANDLE, SSH_FXP_STATUS };
  1026. rc = sftp_packet_requirev(sftp, 2, fopen_responses,
  1027. sftp->open_request_id, &data,
  1028. &data_len, 1);
  1029. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1030. _libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
  1031. "Would block waiting for status message");
  1032. return NULL;
  1033. }
  1034. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  1035. if(data_len > 0) {
  1036. LIBSSH2_FREE(session, data);
  1037. }
  1038. _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1039. "Response too small");
  1040. return NULL;
  1041. }
  1042. sftp->open_state = libssh2_NB_state_idle;
  1043. if(rc) {
  1044. _libssh2_error(session, rc, "Timeout waiting for status message");
  1045. return NULL;
  1046. }
  1047. /* OPEN can basically get STATUS or HANDLE back, where HANDLE implies
  1048. a fine response while STATUS means error. It seems though that at
  1049. times we get an SSH_FX_OK back in a STATUS, followed the "real"
  1050. HANDLE so we need to properly deal with that. */
  1051. if(data[0] == SSH_FXP_STATUS) {
  1052. int badness = 1;
  1053. if(data_len < 9) {
  1054. _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1055. "Too small FXP_STATUS");
  1056. LIBSSH2_FREE(session, data);
  1057. return NULL;
  1058. }
  1059. sftp->last_errno = _libssh2_ntohu32(data + 5);
  1060. if(LIBSSH2_FX_OK == sftp->last_errno) {
  1061. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  1062. "got HANDLE FXOK!");
  1063. LIBSSH2_FREE(session, data);
  1064. /* silly situation, but check for a HANDLE */
  1065. rc = sftp_packet_require(sftp, SSH_FXP_HANDLE,
  1066. sftp->open_request_id, &data,
  1067. &data_len, 10);
  1068. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1069. /* go back to sent state and wait for something else */
  1070. sftp->open_state = libssh2_NB_state_sent;
  1071. return NULL;
  1072. }
  1073. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  1074. if(data_len > 0) {
  1075. LIBSSH2_FREE(session, data);
  1076. }
  1077. _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1078. "Too small FXP_HANDLE");
  1079. return NULL;
  1080. }
  1081. else if(!rc)
  1082. /* we got the handle so this is not a bad situation */
  1083. badness = 0;
  1084. }
  1085. if(badness) {
  1086. _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1087. "Failed opening remote file");
  1088. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  1089. "got FXP_STATUS %d",
  1090. sftp->last_errno);
  1091. LIBSSH2_FREE(session, data);
  1092. return NULL;
  1093. }
  1094. }
  1095. if(data_len < 10) {
  1096. _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1097. "Too small FXP_HANDLE");
  1098. LIBSSH2_FREE(session, data);
  1099. return NULL;
  1100. }
  1101. fp = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE));
  1102. if(!fp) {
  1103. _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  1104. "Unable to allocate new SFTP handle structure");
  1105. LIBSSH2_FREE(session, data);
  1106. return NULL;
  1107. }
  1108. fp->handle_type = open_file ? LIBSSH2_SFTP_HANDLE_FILE :
  1109. LIBSSH2_SFTP_HANDLE_DIR;
  1110. fp->handle_len = _libssh2_ntohu32(data + 5);
  1111. if(fp->handle_len > SFTP_HANDLE_MAXLEN)
  1112. /* SFTP doesn't allow handles longer than 256 characters */
  1113. fp->handle_len = SFTP_HANDLE_MAXLEN;
  1114. if(fp->handle_len > (data_len - 9))
  1115. /* do not reach beyond the end of the data we got */
  1116. fp->handle_len = data_len - 9;
  1117. memcpy(fp->handle, data + 9, fp->handle_len);
  1118. LIBSSH2_FREE(session, data);
  1119. /* add this file handle to the list kept in the sftp session */
  1120. _libssh2_list_add(&sftp->sftp_handles, &fp->node);
  1121. fp->sftp = sftp; /* point to the parent struct */
  1122. fp->u.file.offset = 0;
  1123. fp->u.file.offset_sent = 0;
  1124. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Open command successful");
  1125. return fp;
  1126. }
  1127. return NULL;
  1128. }
  1129. /* libssh2_sftp_open_ex
  1130. */
  1131. LIBSSH2_API LIBSSH2_SFTP_HANDLE *
  1132. libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp, const char *filename,
  1133. unsigned int filename_len, unsigned long flags, long mode,
  1134. int open_type)
  1135. {
  1136. LIBSSH2_SFTP_HANDLE *hnd;
  1137. if(!sftp)
  1138. return NULL;
  1139. BLOCK_ADJUST_ERRNO(hnd, sftp->channel->session,
  1140. sftp_open(sftp, filename, filename_len, flags, mode,
  1141. open_type));
  1142. return hnd;
  1143. }
  1144. /*
  1145. * sftp_read
  1146. *
  1147. * Read from an SFTP file handle
  1148. *
  1149. */
  1150. static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
  1151. size_t buffer_size)
  1152. {
  1153. LIBSSH2_SFTP *sftp = handle->sftp;
  1154. LIBSSH2_CHANNEL *channel = sftp->channel;
  1155. LIBSSH2_SESSION *session = channel->session;
  1156. size_t count = 0;
  1157. struct sftp_pipeline_chunk *chunk;
  1158. struct sftp_pipeline_chunk *next;
  1159. ssize_t rc;
  1160. struct _libssh2_sftp_handle_file_data *filep =
  1161. &handle->u.file;
  1162. size_t bytes_in_buffer = 0;
  1163. char *sliding_bufferp = buffer;
  1164. /* This function can be interrupted in three different places where it
  1165. might need to wait for data from the network. It returns EAGAIN to
  1166. allow non-blocking clients to do other work but these client are
  1167. expected to call this function again (possibly many times) to finish
  1168. the operation.
  1169. The tricky part is that if we previously aborted a sftp_read due to
  1170. EAGAIN, we must continue at the same spot to continue the previously
  1171. interrupted operation. This is done using a state machine to record
  1172. what phase of execution we were at. The state is stored in
  1173. sftp->read_state.
  1174. libssh2_NB_state_idle: The first phase is where we prepare multiple
  1175. FXP_READ packets to do optimistic read-ahead. We send off as many as
  1176. possible in the second phase without waiting for a response to each
  1177. one; this is the key to fast reads. But we may have to adjust the
  1178. channel window size to do this which may interrupt this function while
  1179. waiting. The state machine saves the phase as libssh2_NB_state_idle so
  1180. it returns here on the next call.
  1181. libssh2_NB_state_sent: The second phase is where we send the FXP_READ
  1182. packets. Writing them to the channel can be interrupted with EAGAIN
  1183. but the state machine ensures we skip the first phase on the next call
  1184. and resume sending.
  1185. libssh2_NB_state_sent2: In the third phase (indicated by ) we read the
  1186. data from the responses that have arrived so far. Reading can be
  1187. interrupted with EAGAIN but the state machine ensures we skip the first
  1188. and second phases on the next call and resume sending.
  1189. */
  1190. switch(sftp->read_state) {
  1191. case libssh2_NB_state_idle:
  1192. /* Some data may already have been read from the server in the
  1193. previous call but didn't fit in the buffer at the time. If so, we
  1194. return that now as we can't risk being interrupted later with data
  1195. partially written to the buffer. */
  1196. if(filep->data_left) {
  1197. size_t copy = MIN(buffer_size, filep->data_left);
  1198. memcpy(buffer, &filep->data[ filep->data_len - filep->data_left],
  1199. copy);
  1200. filep->data_left -= copy;
  1201. filep->offset += copy;
  1202. if(!filep->data_left) {
  1203. LIBSSH2_FREE(session, filep->data);
  1204. filep->data = NULL;
  1205. }
  1206. return copy;
  1207. }
  1208. if(filep->eof) {
  1209. return 0;
  1210. }
  1211. else {
  1212. /* We allow a number of bytes being requested at any given time
  1213. without having been acked - until we reach EOF. */
  1214. /* Number of bytes asked for that haven't been acked yet */
  1215. size_t already = (size_t)(filep->offset_sent - filep->offset);
  1216. size_t max_read_ahead = buffer_size;
  1217. unsigned long recv_window;
  1218. /* if the buffer_size passed in now is smaller than what has
  1219. already been sent, we risk getting count become a very large
  1220. number */
  1221. if(max_read_ahead > already)
  1222. count = max_read_ahead - already;
  1223. /* 'count' is how much more data to ask for, and 'already' is how
  1224. much data that already has been asked for but not yet returned.
  1225. Specifically, 'count' means how much data that have or will be
  1226. asked for by the nodes that are already added to the linked
  1227. list. Some of those read requests may not actually have been
  1228. sent off successfully yet.
  1229. If 'already' is very large it should be perfectly fine to have
  1230. count set to 0 as then we don't have to ask for more data
  1231. (right now).
  1232. buffer_size*4 is just picked more or less out of the air. The
  1233. idea is that when reading SFTP from a remote server, we send
  1234. away multiple read requests guessing that the client will read
  1235. more than only this 'buffer_size' amount of memory. So we ask
  1236. for maximum buffer_size*4 amount of data so that we can return
  1237. them very fast in subsequent calls.
  1238. */
  1239. recv_window = libssh2_channel_window_read_ex(sftp->channel,
  1240. NULL, NULL);
  1241. if(max_read_ahead > recv_window) {
  1242. /* more data will be asked for than what the window currently
  1243. allows, expand it! */
  1244. rc = _libssh2_channel_receive_window_adjust(sftp->channel,
  1245. max_read_ahead*8,
  1246. 1, NULL);
  1247. /* if this returns EAGAIN, we will get back to this function
  1248. at next call */
  1249. assert(rc != LIBSSH2_ERROR_EAGAIN || !filep->data_left);
  1250. assert(rc != LIBSSH2_ERROR_EAGAIN || !filep->eof);
  1251. if(rc)
  1252. return rc;
  1253. }
  1254. }
  1255. while(count > 0) {
  1256. unsigned char *s;
  1257. /* 25 = packet_len(4) + packet_type(1) + request_id(4) +
  1258. handle_len(4) + offset(8) + count(4) */
  1259. uint32_t packet_len = (uint32_t)handle->handle_len + 25;
  1260. uint32_t request_id;
  1261. uint32_t size = count;
  1262. //if(size < buffer_size)
  1263. // size = buffer_size;
  1264. if(size > MAX_SFTP_READ_SIZE)
  1265. size = MAX_SFTP_READ_SIZE;
  1266. chunk = LIBSSH2_ALLOC(session, packet_len +
  1267. sizeof(struct sftp_pipeline_chunk));
  1268. if(!chunk)
  1269. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  1270. "malloc fail for FXP_WRITE");
  1271. chunk->offset = filep->offset_sent;
  1272. chunk->len = size;
  1273. chunk->lefttosend = packet_len;
  1274. chunk->sent = 0;
  1275. s = chunk->packet;
  1276. _libssh2_store_u32(&s, packet_len - 4);
  1277. *s++ = SSH_FXP_READ;
  1278. request_id = sftp->request_id++;
  1279. chunk->request_id = request_id;
  1280. _libssh2_store_u32(&s, request_id);
  1281. _libssh2_store_str(&s, handle->handle, handle->handle_len);
  1282. _libssh2_store_u64(&s, filep->offset_sent);
  1283. filep->offset_sent += size; /* advance offset at once */
  1284. _libssh2_store_u32(&s, size);
  1285. /* add this new entry LAST in the list */
  1286. _libssh2_list_add(&handle->packet_list, &chunk->node);
  1287. count -= MIN(size, count); /* deduct the size we used, as we might
  1288. * have to create more packets */
  1289. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  1290. "read request id %d sent (offset: %d, size: %d)",
  1291. request_id, (int)chunk->offset, (int)chunk->len);
  1292. }
  1293. /* FALL-THROUGH */
  1294. case libssh2_NB_state_sent:
  1295. sftp->read_state = libssh2_NB_state_idle;
  1296. /* move through the READ packets that haven't been sent and send as
  1297. many as possible - remember that we don't block */
  1298. chunk = _libssh2_list_first(&handle->packet_list);
  1299. while(chunk) {
  1300. if(chunk->lefttosend) {
  1301. rc = _libssh2_channel_write(channel, 0,
  1302. &chunk->packet[chunk->sent],
  1303. chunk->lefttosend);
  1304. if(rc < 0) {
  1305. sftp->read_state = libssh2_NB_state_sent;
  1306. return rc;
  1307. }
  1308. /* remember where to continue sending the next time */
  1309. chunk->lefttosend -= rc;
  1310. chunk->sent += rc;
  1311. if(chunk->lefttosend) {
  1312. /* We still have data left to send for this chunk.
  1313. * If there is at least one completely sent chunk,
  1314. * we can get out of this loop and start reading. */
  1315. if(chunk != _libssh2_list_first(&handle->packet_list)) {
  1316. break;
  1317. }
  1318. else {
  1319. continue;
  1320. }
  1321. }
  1322. }
  1323. /* move on to the next chunk with data to send */
  1324. chunk = _libssh2_list_next(&chunk->node);
  1325. }
  1326. /* FALL-THROUGH */
  1327. case libssh2_NB_state_sent2:
  1328. sftp->read_state = libssh2_NB_state_idle;
  1329. /*
  1330. * Count all ACKed packets and act on the contents of them.
  1331. */
  1332. chunk = _libssh2_list_first(&handle->packet_list);
  1333. while(chunk) {
  1334. unsigned char *data;
  1335. size_t data_len;
  1336. uint32_t rc32;
  1337. static const unsigned char read_responses[2] = {
  1338. SSH_FXP_DATA, SSH_FXP_STATUS
  1339. };
  1340. if(chunk->lefttosend) {
  1341. /* if the chunk still has data left to send, we shouldn't wait
  1342. for an ACK for it just yet */
  1343. if(bytes_in_buffer > 0) {
  1344. return bytes_in_buffer;
  1345. }
  1346. else {
  1347. /* we should never reach this point */
  1348. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1349. "sftp_read() internal error a");
  1350. }
  1351. }
  1352. rc = sftp_packet_requirev(sftp, 2, read_responses,
  1353. chunk->request_id, &data, &data_len, 9);
  1354. if(rc == LIBSSH2_ERROR_EAGAIN && bytes_in_buffer != 0) {
  1355. /* do not return EAGAIN if we have already
  1356. * written data into the buffer */
  1357. return bytes_in_buffer;
  1358. }
  1359. if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  1360. if(data_len > 0) {
  1361. LIBSSH2_FREE(session, data);
  1362. }
  1363. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1364. "Response too small");
  1365. }
  1366. else if(rc < 0) {
  1367. sftp->read_state = libssh2_NB_state_sent2;
  1368. return rc;
  1369. }
  1370. /*
  1371. * We get DATA or STATUS back. STATUS can be error, or it is
  1372. * FX_EOF when we reach the end of the file.
  1373. */
  1374. switch(data[0]) {
  1375. case SSH_FXP_STATUS:
  1376. /* remove the chunk we just processed */
  1377. _libssh2_list_remove(&chunk->node);
  1378. LIBSSH2_FREE(session, chunk);
  1379. /* we must remove all outstanding READ requests, as either we
  1380. got an error or we're at end of file */
  1381. sftp_packetlist_flush(handle);
  1382. rc32 = _libssh2_ntohu32(data + 5);
  1383. LIBSSH2_FREE(session, data);
  1384. if(rc32 == LIBSSH2_FX_EOF) {
  1385. filep->eof = TRUE;
  1386. return bytes_in_buffer;
  1387. }
  1388. else {
  1389. sftp->last_errno = rc32;
  1390. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1391. "SFTP READ error");
  1392. }
  1393. break;
  1394. case SSH_FXP_DATA:
  1395. if(chunk->offset != filep->offset) {
  1396. /* This could happen if the server returns less bytes than
  1397. requested, which shouldn't happen for normal files. See:
  1398. https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
  1399. #section-6.4
  1400. */
  1401. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1402. "Read Packet At Unexpected Offset");
  1403. }
  1404. rc32 = _libssh2_ntohu32(data + 5);
  1405. if(rc32 > (data_len - 9))
  1406. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1407. "SFTP Protocol badness");
  1408. if(rc32 > chunk->len) {
  1409. /* A chunk larger than we requested was returned to us.
  1410. This is a protocol violation and we don't know how to
  1411. deal with it. Bail out! */
  1412. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1413. "FXP_READ response too big");
  1414. }
  1415. if(rc32 != chunk->len) {
  1416. /* a short read does not imply end of file, but we must
  1417. adjust the offset_sent since it was advanced with a
  1418. full chunk->len before */
  1419. filep->offset_sent -= (chunk->len - rc32);
  1420. }
  1421. if((bytes_in_buffer + rc32) > buffer_size) {
  1422. /* figure out the overlap amount */
  1423. filep->data_left = (bytes_in_buffer + rc32) - buffer_size;
  1424. /* getting the full packet would overflow the buffer, so
  1425. only get the correct amount and keep the remainder */
  1426. rc32 = (uint32_t)buffer_size - bytes_in_buffer;
  1427. /* store data to keep for next call */
  1428. filep->data = data;
  1429. filep->data_len = data_len;
  1430. }
  1431. else
  1432. filep->data_len = 0;
  1433. /* copy the received data from the received FXP_DATA packet to
  1434. the buffer at the correct index */
  1435. memcpy(sliding_bufferp, data + 9, rc32);
  1436. filep->offset += rc32;
  1437. bytes_in_buffer += rc32;
  1438. sliding_bufferp += rc32;
  1439. if(filep->data_len == 0)
  1440. /* free the allocated data if not stored to keep */
  1441. LIBSSH2_FREE(session, data);
  1442. /* remove the chunk we just processed keeping track of the
  1443. * next one in case we need it */
  1444. next = _libssh2_list_next(&chunk->node);
  1445. _libssh2_list_remove(&chunk->node);
  1446. LIBSSH2_FREE(session, chunk);
  1447. /* check if we have space left in the buffer
  1448. * and either continue to the next chunk or stop
  1449. */
  1450. if(bytes_in_buffer < buffer_size) {
  1451. chunk = next;
  1452. }
  1453. else {
  1454. chunk = NULL;
  1455. }
  1456. break;
  1457. default:
  1458. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1459. "SFTP Protocol badness: unrecognised "
  1460. "read request response");
  1461. }
  1462. }
  1463. if(bytes_in_buffer > 0)
  1464. return bytes_in_buffer;
  1465. break;
  1466. default:
  1467. assert(!"State machine error; unrecognised read state");
  1468. }
  1469. /* we should never reach this point */
  1470. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1471. "sftp_read() internal error b");
  1472. }
  1473. /* libssh2_sftp_read
  1474. * Read from an SFTP file handle
  1475. */
  1476. LIBSSH2_API ssize_t
  1477. libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *hnd, char *buffer,
  1478. size_t buffer_maxlen)
  1479. {
  1480. ssize_t rc;
  1481. if(!hnd)
  1482. return LIBSSH2_ERROR_BAD_USE;
  1483. BLOCK_ADJUST(rc, hnd->sftp->channel->session,
  1484. sftp_read(hnd, buffer, buffer_maxlen));
  1485. return rc;
  1486. }
  1487. /* sftp_readdir
  1488. * Read from an SFTP directory handle
  1489. */
  1490. static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer,
  1491. size_t buffer_maxlen, char *longentry,
  1492. size_t longentry_maxlen,
  1493. LIBSSH2_SFTP_ATTRIBUTES *attrs)
  1494. {
  1495. LIBSSH2_SFTP *sftp = handle->sftp;
  1496. LIBSSH2_CHANNEL *channel = sftp->channel;
  1497. LIBSSH2_SESSION *session = channel->session;
  1498. size_t data_len;
  1499. uint32_t num_names;
  1500. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
  1501. uint32_t packet_len = handle->handle_len + 13;
  1502. unsigned char *s, *data;
  1503. static const unsigned char read_responses[2] = {
  1504. SSH_FXP_NAME, SSH_FXP_STATUS };
  1505. ssize_t retcode;
  1506. if(sftp->readdir_state == libssh2_NB_state_idle) {
  1507. if(handle->u.dir.names_left) {
  1508. /*
  1509. * A prior request returned more than one directory entry,
  1510. * feed it back from the buffer
  1511. */
  1512. LIBSSH2_SFTP_ATTRIBUTES attrs_dummy;
  1513. size_t real_longentry_len;
  1514. size_t real_filename_len;
  1515. size_t filename_len;
  1516. size_t longentry_len;
  1517. size_t names_packet_len = handle->u.dir.names_packet_len;
  1518. int attr_len = 0;
  1519. if(names_packet_len >= 4) {
  1520. s = (unsigned char *) handle->u.dir.next_name;
  1521. real_filename_len = _libssh2_ntohu32(s);
  1522. s += 4;
  1523. names_packet_len -= 4;
  1524. }
  1525. else {
  1526. filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  1527. goto end;
  1528. }
  1529. filename_len = real_filename_len;
  1530. if(filename_len >= buffer_maxlen) {
  1531. filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  1532. goto end;
  1533. }
  1534. if(buffer_maxlen >= filename_len && names_packet_len >=
  1535. filename_len) {
  1536. memcpy(buffer, s, filename_len);
  1537. buffer[filename_len] = '\0'; /* zero terminate */
  1538. s += real_filename_len;
  1539. names_packet_len -= real_filename_len;
  1540. }
  1541. else {
  1542. filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  1543. goto end;
  1544. }
  1545. if(names_packet_len >= 4) {
  1546. real_longentry_len = _libssh2_ntohu32(s);
  1547. s += 4;
  1548. names_packet_len -= 4;
  1549. }
  1550. else {
  1551. filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  1552. goto end;
  1553. }
  1554. if(longentry && (longentry_maxlen>1)) {
  1555. longentry_len = real_longentry_len;
  1556. if(longentry_len >= longentry_maxlen ||
  1557. longentry_len > names_packet_len) {
  1558. filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  1559. goto end;
  1560. }
  1561. memcpy(longentry, s, longentry_len);
  1562. longentry[longentry_len] = '\0'; /* zero terminate */
  1563. }
  1564. if(real_longentry_len <= names_packet_len) {
  1565. s += real_longentry_len;
  1566. names_packet_len -= real_longentry_len;
  1567. }
  1568. else {
  1569. filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  1570. goto end;
  1571. }
  1572. if(attrs)
  1573. memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
  1574. attr_len = sftp_bin2attr(attrs ? attrs : &attrs_dummy, s,
  1575. names_packet_len);
  1576. if(attr_len >= 0) {
  1577. s += attr_len;
  1578. names_packet_len -= attr_len;
  1579. }
  1580. else {
  1581. filename_len = (size_t)LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  1582. goto end;
  1583. }
  1584. handle->u.dir.next_name = (char *) s;
  1585. handle->u.dir.names_packet_len = names_packet_len;
  1586. end:
  1587. if((--handle->u.dir.names_left) == 0)
  1588. LIBSSH2_FREE(session, handle->u.dir.names_packet);
  1589. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  1590. "libssh2_sftp_readdir_ex() return %d",
  1591. filename_len);
  1592. return (ssize_t)filename_len;
  1593. }
  1594. /* Request another entry(entries?) */
  1595. s = sftp->readdir_packet = LIBSSH2_ALLOC(session, packet_len);
  1596. if(!sftp->readdir_packet)
  1597. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  1598. "Unable to allocate memory for "
  1599. "FXP_READDIR packet");
  1600. _libssh2_store_u32(&s, packet_len - 4);
  1601. *(s++) = SSH_FXP_READDIR;
  1602. sftp->readdir_request_id = sftp->request_id++;
  1603. _libssh2_store_u32(&s, sftp->readdir_request_id);
  1604. _libssh2_store_str(&s, handle->handle, handle->handle_len);
  1605. sftp->readdir_state = libssh2_NB_state_created;
  1606. }
  1607. if(sftp->readdir_state == libssh2_NB_state_created) {
  1608. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  1609. "Reading entries from directory handle");
  1610. retcode = _libssh2_channel_write(channel, 0, sftp->readdir_packet,
  1611. packet_len);
  1612. if(retcode == LIBSSH2_ERROR_EAGAIN) {
  1613. return retcode;
  1614. }
  1615. else if((ssize_t)packet_len != retcode) {
  1616. LIBSSH2_FREE(session, sftp->readdir_packet);
  1617. sftp->readdir_packet = NULL;
  1618. sftp->readdir_state = libssh2_NB_state_idle;
  1619. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  1620. "_libssh2_channel_write() failed");
  1621. }
  1622. LIBSSH2_FREE(session, sftp->readdir_packet);
  1623. sftp->readdir_packet = NULL;
  1624. sftp->readdir_state = libssh2_NB_state_sent;
  1625. }
  1626. retcode = sftp_packet_requirev(sftp, 2, read_responses,
  1627. sftp->readdir_request_id, &data,
  1628. &data_len, 9);
  1629. if(retcode == LIBSSH2_ERROR_EAGAIN)
  1630. return retcode;
  1631. else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  1632. if(data_len > 0) {
  1633. LIBSSH2_FREE(session, data);
  1634. }
  1635. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1636. "Status message too short");
  1637. }
  1638. else if(retcode) {
  1639. sftp->readdir_state = libssh2_NB_state_idle;
  1640. return _libssh2_error(session, retcode,
  1641. "Timeout waiting for status message");
  1642. }
  1643. if(data[0] == SSH_FXP_STATUS) {
  1644. retcode = _libssh2_ntohu32(data + 5);
  1645. LIBSSH2_FREE(session, data);
  1646. if(retcode == LIBSSH2_FX_EOF) {
  1647. sftp->readdir_state = libssh2_NB_state_idle;
  1648. return 0;
  1649. }
  1650. else {
  1651. sftp->last_errno = retcode;
  1652. sftp->readdir_state = libssh2_NB_state_idle;
  1653. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1654. "SFTP Protocol Error");
  1655. }
  1656. }
  1657. sftp->readdir_state = libssh2_NB_state_idle;
  1658. num_names = _libssh2_ntohu32(data + 5);
  1659. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%lu entries returned",
  1660. num_names);
  1661. if(!num_names) {
  1662. LIBSSH2_FREE(session, data);
  1663. return 0;
  1664. }
  1665. handle->u.dir.names_left = num_names;
  1666. handle->u.dir.names_packet = data;
  1667. handle->u.dir.next_name = (char *) data + 9;
  1668. handle->u.dir.names_packet_len = data_len - 9;
  1669. /* use the name popping mechanism from the start of the function */
  1670. return sftp_readdir(handle, buffer, buffer_maxlen, longentry,
  1671. longentry_maxlen, attrs);
  1672. }
  1673. /* libssh2_sftp_readdir_ex
  1674. * Read from an SFTP directory handle
  1675. */
  1676. LIBSSH2_API int
  1677. libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *hnd, char *buffer,
  1678. size_t buffer_maxlen, char *longentry,
  1679. size_t longentry_maxlen,
  1680. LIBSSH2_SFTP_ATTRIBUTES *attrs)
  1681. {
  1682. int rc;
  1683. if(!hnd)
  1684. return LIBSSH2_ERROR_BAD_USE;
  1685. BLOCK_ADJUST(rc, hnd->sftp->channel->session,
  1686. sftp_readdir(hnd, buffer, buffer_maxlen, longentry,
  1687. longentry_maxlen, attrs));
  1688. return rc;
  1689. }
  1690. /*
  1691. * sftp_write
  1692. *
  1693. * Write data to an SFTP handle. Returns the number of bytes written, or
  1694. * a negative error code.
  1695. *
  1696. * We recommend sending very large data buffers to this function!
  1697. *
  1698. * Concept:
  1699. *
  1700. * - Detect how much of the given buffer that was already sent in a previous
  1701. * call by inspecting the linked list of outgoing chunks. Make sure to skip
  1702. * passed the data that has already been taken care of.
  1703. *
  1704. * - Split all (new) outgoing data in chunks no larger than N.
  1705. *
  1706. * - Each N bytes chunk gets created as a separate SFTP packet.
  1707. *
  1708. * - Add all created outgoing packets to the linked list.
  1709. *
  1710. * - Walk through the list and send the chunks that haven't been sent,
  1711. * as many as possible until EAGAIN. Some of the chunks may have been put
  1712. * in the list in a previous invoke.
  1713. *
  1714. * - For all the chunks in the list that have been completely sent off, check
  1715. * for ACKs. If a chunk has been ACKed, it is removed from the linked
  1716. * list and the "acked" counter gets increased with that data amount.
  1717. *
  1718. * - Return TOTAL bytes acked so far.
  1719. *
  1720. * Caveats:
  1721. * - be careful: we must not return a higher number than what was given!
  1722. *
  1723. * TODO:
  1724. * Introduce an option that disables this sort of "speculative" ahead writing
  1725. * as there's a risk that it will do harm to some app.
  1726. */
  1727. static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
  1728. size_t count)
  1729. {
  1730. LIBSSH2_SFTP *sftp = handle->sftp;
  1731. LIBSSH2_CHANNEL *channel = sftp->channel;
  1732. LIBSSH2_SESSION *session = channel->session;
  1733. size_t data_len;
  1734. uint32_t retcode;
  1735. uint32_t packet_len;
  1736. unsigned char *s, *data;
  1737. ssize_t rc;
  1738. struct sftp_pipeline_chunk *chunk;
  1739. struct sftp_pipeline_chunk *next;
  1740. size_t acked = 0;
  1741. size_t org_count = count;
  1742. size_t already;
  1743. switch(sftp->write_state) {
  1744. default:
  1745. case libssh2_NB_state_idle:
  1746. /* Number of bytes sent off that haven't been acked and therefore we
  1747. will get passed in here again.
  1748. Also, add up the number of bytes that actually already have been
  1749. acked but we haven't been able to return as such yet, so we will
  1750. get that data as well passed in here again.
  1751. */
  1752. already = (size_t) (handle->u.file.offset_sent -
  1753. handle->u.file.offset)+
  1754. handle->u.file.acked;
  1755. if(count >= already) {
  1756. /* skip the part already made into packets */
  1757. buffer += already;
  1758. count -= already;
  1759. }
  1760. else
  1761. /* there is more data already fine than what we got in this call */
  1762. count = 0;
  1763. sftp->write_state = libssh2_NB_state_idle;
  1764. while(count) {
  1765. /* TODO: Possibly this should have some logic to prevent a very
  1766. very small fraction to be left but lets ignore that for now */
  1767. uint32_t size = MIN(MAX_SFTP_OUTGOING_SIZE, count);
  1768. uint32_t request_id;
  1769. /* 25 = packet_len(4) + packet_type(1) + request_id(4) +
  1770. handle_len(4) + offset(8) + count(4) */
  1771. packet_len = handle->handle_len + size + 25;
  1772. chunk = LIBSSH2_ALLOC(session, packet_len +
  1773. sizeof(struct sftp_pipeline_chunk));
  1774. if(!chunk)
  1775. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  1776. "malloc fail for FXP_WRITE");
  1777. chunk->len = size;
  1778. chunk->sent = 0;
  1779. chunk->lefttosend = packet_len;
  1780. s = chunk->packet;
  1781. _libssh2_store_u32(&s, packet_len - 4);
  1782. *(s++) = SSH_FXP_WRITE;
  1783. request_id = sftp->request_id++;
  1784. chunk->request_id = request_id;
  1785. _libssh2_store_u32(&s, request_id);
  1786. _libssh2_store_str(&s, handle->handle, handle->handle_len);
  1787. _libssh2_store_u64(&s, handle->u.file.offset_sent);
  1788. handle->u.file.offset_sent += size; /* advance offset at once */
  1789. _libssh2_store_str(&s, buffer, size);
  1790. /* add this new entry LAST in the list */
  1791. _libssh2_list_add(&handle->packet_list, &chunk->node);
  1792. buffer += size;
  1793. count -= size; /* deduct the size we used, as we might have
  1794. to create more packets */
  1795. }
  1796. /* move through the WRITE packets that haven't been sent and send as
  1797. many as possible - remember that we don't block */
  1798. chunk = _libssh2_list_first(&handle->packet_list);
  1799. while(chunk) {
  1800. if(chunk->lefttosend) {
  1801. rc = _libssh2_channel_write(channel, 0,
  1802. &chunk->packet[chunk->sent],
  1803. chunk->lefttosend);
  1804. if(rc < 0)
  1805. /* remain in idle state */
  1806. return rc;
  1807. /* remember where to continue sending the next time */
  1808. chunk->lefttosend -= rc;
  1809. chunk->sent += rc;
  1810. if(chunk->lefttosend)
  1811. /* data left to send, get out of loop */
  1812. break;
  1813. }
  1814. /* move on to the next chunk with data to send */
  1815. chunk = _libssh2_list_next(&chunk->node);
  1816. }
  1817. /* fall-through */
  1818. case libssh2_NB_state_sent:
  1819. sftp->write_state = libssh2_NB_state_idle;
  1820. /*
  1821. * Count all ACKed packets
  1822. */
  1823. chunk = _libssh2_list_first(&handle->packet_list);
  1824. while(chunk) {
  1825. if(chunk->lefttosend)
  1826. /* if the chunk still has data left to send, we shouldn't wait
  1827. for an ACK for it just yet */
  1828. break;
  1829. else if(acked)
  1830. /* if we have sent data that is acked, we must return that
  1831. info before we call a function that might return EAGAIN */
  1832. break;
  1833. /* we check the packets in order */
  1834. rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
  1835. chunk->request_id, &data, &data_len, 9);
  1836. if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  1837. if(data_len > 0) {
  1838. LIBSSH2_FREE(session, data);
  1839. }
  1840. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1841. "FXP write packet too short");
  1842. }
  1843. else if(rc < 0) {
  1844. if(rc == LIBSSH2_ERROR_EAGAIN)
  1845. sftp->write_state = libssh2_NB_state_sent;
  1846. return rc;
  1847. }
  1848. retcode = _libssh2_ntohu32(data + 5);
  1849. LIBSSH2_FREE(session, data);
  1850. sftp->last_errno = retcode;
  1851. if(retcode == LIBSSH2_FX_OK) {
  1852. acked += chunk->len; /* number of payload data that was acked
  1853. here */
  1854. /* we increase the offset value for all acks */
  1855. handle->u.file.offset += chunk->len;
  1856. next = _libssh2_list_next(&chunk->node);
  1857. _libssh2_list_remove(&chunk->node); /* remove from list */
  1858. LIBSSH2_FREE(session, chunk); /* free memory */
  1859. chunk = next;
  1860. }
  1861. else {
  1862. /* flush all pending packets from the outgoing list */
  1863. sftp_packetlist_flush(handle);
  1864. /* since we return error now, the application will not get any
  1865. outstanding data acked, so we need to rewind the offset to
  1866. where the application knows it has reached with acked
  1867. data */
  1868. handle->u.file.offset -= handle->u.file.acked;
  1869. /* then reset the offset_sent to be the same as the offset */
  1870. handle->u.file.offset_sent = handle->u.file.offset;
  1871. /* clear the acked counter since we can have no pending data to
  1872. ack after an error */
  1873. handle->u.file.acked = 0;
  1874. /* the server returned an error for that written chunk,
  1875. propagate this back to our parent function */
  1876. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1877. "FXP write failed");
  1878. }
  1879. }
  1880. break;
  1881. }
  1882. /* if there were acked data in a previous call that wasn't returned then,
  1883. add that up and try to return it all now. This can happen if the app
  1884. first sends a huge buffer of data, and then in a second call it sends a
  1885. smaller one. */
  1886. acked += handle->u.file.acked;
  1887. if(acked) {
  1888. ssize_t ret = MIN(acked, org_count);
  1889. /* we got data acked so return that amount, but no more than what
  1890. was asked to get sent! */
  1891. /* store the remainder. 'ret' is always equal to or less than 'acked'
  1892. here */
  1893. handle->u.file.acked = acked - ret;
  1894. return ret;
  1895. }
  1896. else
  1897. return 0; /* nothing was acked, and no EAGAIN was received! */
  1898. }
  1899. /* libssh2_sftp_write
  1900. * Write data to a file handle
  1901. */
  1902. LIBSSH2_API ssize_t
  1903. libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *hnd, const char *buffer,
  1904. size_t count)
  1905. {
  1906. ssize_t rc;
  1907. if(!hnd)
  1908. return LIBSSH2_ERROR_BAD_USE;
  1909. BLOCK_ADJUST(rc, hnd->sftp->channel->session,
  1910. sftp_write(hnd, buffer, count));
  1911. return rc;
  1912. }
  1913. static int sftp_fsync(LIBSSH2_SFTP_HANDLE *handle)
  1914. {
  1915. LIBSSH2_SFTP *sftp = handle->sftp;
  1916. LIBSSH2_CHANNEL *channel = sftp->channel;
  1917. LIBSSH2_SESSION *session = channel->session;
  1918. /* 34 = packet_len(4) + packet_type(1) + request_id(4) +
  1919. string_len(4) + strlen("fsync@openssh.com")(17) + handle_len(4) */
  1920. uint32_t packet_len = handle->handle_len + 34;
  1921. size_t data_len;
  1922. unsigned char *packet, *s, *data;
  1923. ssize_t rc;
  1924. uint32_t retcode;
  1925. if(sftp->fsync_state == libssh2_NB_state_idle) {
  1926. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  1927. "Issuing fsync command");
  1928. s = packet = LIBSSH2_ALLOC(session, packet_len);
  1929. if(!packet) {
  1930. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  1931. "Unable to allocate memory for FXP_EXTENDED "
  1932. "packet");
  1933. }
  1934. _libssh2_store_u32(&s, packet_len - 4);
  1935. *(s++) = SSH_FXP_EXTENDED;
  1936. sftp->fsync_request_id = sftp->request_id++;
  1937. _libssh2_store_u32(&s, sftp->fsync_request_id);
  1938. _libssh2_store_str(&s, "fsync@openssh.com", 17);
  1939. _libssh2_store_str(&s, handle->handle, handle->handle_len);
  1940. sftp->fsync_state = libssh2_NB_state_created;
  1941. }
  1942. else {
  1943. packet = sftp->fsync_packet;
  1944. }
  1945. if(sftp->fsync_state == libssh2_NB_state_created) {
  1946. rc = _libssh2_channel_write(channel, 0, packet, packet_len);
  1947. if(rc == LIBSSH2_ERROR_EAGAIN ||
  1948. (0 <= rc && rc < (ssize_t)packet_len)) {
  1949. sftp->fsync_packet = packet;
  1950. return LIBSSH2_ERROR_EAGAIN;
  1951. }
  1952. LIBSSH2_FREE(session, packet);
  1953. sftp->fsync_packet = NULL;
  1954. if(rc < 0) {
  1955. sftp->fsync_state = libssh2_NB_state_idle;
  1956. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  1957. "_libssh2_channel_write() failed");
  1958. }
  1959. sftp->fsync_state = libssh2_NB_state_sent;
  1960. }
  1961. rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
  1962. sftp->fsync_request_id, &data, &data_len, 9);
  1963. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1964. return rc;
  1965. }
  1966. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  1967. if(data_len > 0) {
  1968. LIBSSH2_FREE(session, data);
  1969. }
  1970. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1971. "SFTP fsync packet too short");
  1972. }
  1973. else if(rc) {
  1974. sftp->fsync_state = libssh2_NB_state_idle;
  1975. return _libssh2_error(session, rc,
  1976. "Error waiting for FXP EXTENDED REPLY");
  1977. }
  1978. sftp->fsync_state = libssh2_NB_state_idle;
  1979. retcode = _libssh2_ntohu32(data + 5);
  1980. LIBSSH2_FREE(session, data);
  1981. if(retcode != LIBSSH2_FX_OK) {
  1982. sftp->last_errno = retcode;
  1983. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  1984. "fsync failed");
  1985. }
  1986. return 0;
  1987. }
  1988. /* libssh2_sftp_fsync
  1989. * Commit data on the handle to disk.
  1990. */
  1991. LIBSSH2_API int
  1992. libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE *hnd)
  1993. {
  1994. int rc;
  1995. if(!hnd)
  1996. return LIBSSH2_ERROR_BAD_USE;
  1997. BLOCK_ADJUST(rc, hnd->sftp->channel->session,
  1998. sftp_fsync(hnd));
  1999. return rc;
  2000. }
  2001. /*
  2002. * sftp_fstat
  2003. *
  2004. * Get or Set stat on a file
  2005. */
  2006. static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle,
  2007. LIBSSH2_SFTP_ATTRIBUTES *attrs, int setstat)
  2008. {
  2009. LIBSSH2_SFTP *sftp = handle->sftp;
  2010. LIBSSH2_CHANNEL *channel = sftp->channel;
  2011. LIBSSH2_SESSION *session = channel->session;
  2012. size_t data_len;
  2013. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
  2014. uint32_t packet_len =
  2015. handle->handle_len + 13 + (setstat ? sftp_attrsize(attrs->flags) : 0);
  2016. unsigned char *s, *data;
  2017. static const unsigned char fstat_responses[2] =
  2018. { SSH_FXP_ATTRS, SSH_FXP_STATUS };
  2019. ssize_t rc;
  2020. if(sftp->fstat_state == libssh2_NB_state_idle) {
  2021. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Issuing %s command",
  2022. setstat ? "set-stat" : "stat");
  2023. s = sftp->fstat_packet = LIBSSH2_ALLOC(session, packet_len);
  2024. if(!sftp->fstat_packet) {
  2025. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2026. "Unable to allocate memory for "
  2027. "FSTAT/FSETSTAT packet");
  2028. }
  2029. _libssh2_store_u32(&s, packet_len - 4);
  2030. *(s++) = setstat ? SSH_FXP_FSETSTAT : SSH_FXP_FSTAT;
  2031. sftp->fstat_request_id = sftp->request_id++;
  2032. _libssh2_store_u32(&s, sftp->fstat_request_id);
  2033. _libssh2_store_str(&s, handle->handle, handle->handle_len);
  2034. if(setstat) {
  2035. s += sftp_attr2bin(s, attrs);
  2036. }
  2037. sftp->fstat_state = libssh2_NB_state_created;
  2038. }
  2039. if(sftp->fstat_state == libssh2_NB_state_created) {
  2040. rc = _libssh2_channel_write(channel, 0, sftp->fstat_packet,
  2041. packet_len);
  2042. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2043. return rc;
  2044. }
  2045. else if((ssize_t)packet_len != rc) {
  2046. LIBSSH2_FREE(session, sftp->fstat_packet);
  2047. sftp->fstat_packet = NULL;
  2048. sftp->fstat_state = libssh2_NB_state_idle;
  2049. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2050. (setstat ? "Unable to send FXP_FSETSTAT"
  2051. : "Unable to send FXP_FSTAT command"));
  2052. }
  2053. LIBSSH2_FREE(session, sftp->fstat_packet);
  2054. sftp->fstat_packet = NULL;
  2055. sftp->fstat_state = libssh2_NB_state_sent;
  2056. }
  2057. rc = sftp_packet_requirev(sftp, 2, fstat_responses,
  2058. sftp->fstat_request_id, &data,
  2059. &data_len, 9);
  2060. if(rc == LIBSSH2_ERROR_EAGAIN)
  2061. return rc;
  2062. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2063. if(data_len > 0) {
  2064. LIBSSH2_FREE(session, data);
  2065. }
  2066. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2067. "SFTP fstat packet too short");
  2068. }
  2069. else if(rc) {
  2070. sftp->fstat_state = libssh2_NB_state_idle;
  2071. return _libssh2_error(session, rc,
  2072. "Timeout waiting for status message");
  2073. }
  2074. sftp->fstat_state = libssh2_NB_state_idle;
  2075. if(data[0] == SSH_FXP_STATUS) {
  2076. uint32_t retcode;
  2077. retcode = _libssh2_ntohu32(data + 5);
  2078. LIBSSH2_FREE(session, data);
  2079. if(retcode == LIBSSH2_FX_OK) {
  2080. return 0;
  2081. }
  2082. else {
  2083. sftp->last_errno = retcode;
  2084. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2085. "SFTP Protocol Error");
  2086. }
  2087. }
  2088. if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
  2089. LIBSSH2_FREE(session, data);
  2090. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2091. "Attributes too short in SFTP fstat");
  2092. }
  2093. LIBSSH2_FREE(session, data);
  2094. return 0;
  2095. }
  2096. /* libssh2_sftp_fstat_ex
  2097. * Get or Set stat on a file
  2098. */
  2099. LIBSSH2_API int
  2100. libssh2_sftp_fstat_ex(LIBSSH2_SFTP_HANDLE *hnd,
  2101. LIBSSH2_SFTP_ATTRIBUTES *attrs, int setstat)
  2102. {
  2103. int rc;
  2104. if(!hnd || !attrs)
  2105. return LIBSSH2_ERROR_BAD_USE;
  2106. BLOCK_ADJUST(rc, hnd->sftp->channel->session,
  2107. sftp_fstat(hnd, attrs, setstat));
  2108. return rc;
  2109. }
  2110. /* libssh2_sftp_seek64
  2111. * Set the read/write pointer to an arbitrary position within the file
  2112. */
  2113. LIBSSH2_API void
  2114. libssh2_sftp_seek64(LIBSSH2_SFTP_HANDLE *handle, libssh2_uint64_t offset)
  2115. {
  2116. if(!handle)
  2117. return;
  2118. if(handle->u.file.offset == offset && handle->u.file.offset_sent == offset)
  2119. return;
  2120. handle->u.file.offset = handle->u.file.offset_sent = offset;
  2121. /* discard all pending requests and currently read data */
  2122. sftp_packetlist_flush(handle);
  2123. /* free the left received buffered data */
  2124. if(handle->u.file.data_left) {
  2125. LIBSSH2_FREE(handle->sftp->channel->session, handle->u.file.data);
  2126. handle->u.file.data_left = handle->u.file.data_len = 0;
  2127. handle->u.file.data = NULL;
  2128. }
  2129. /* reset EOF to False */
  2130. handle->u.file.eof = FALSE;
  2131. }
  2132. /* libssh2_sftp_seek
  2133. * Set the read/write pointer to an arbitrary position within the file
  2134. */
  2135. LIBSSH2_API void
  2136. libssh2_sftp_seek(LIBSSH2_SFTP_HANDLE *handle, size_t offset)
  2137. {
  2138. libssh2_sftp_seek64(handle, (libssh2_uint64_t)offset);
  2139. }
  2140. /* libssh2_sftp_tell
  2141. * Return the current read/write pointer's offset
  2142. */
  2143. LIBSSH2_API size_t
  2144. libssh2_sftp_tell(LIBSSH2_SFTP_HANDLE *handle)
  2145. {
  2146. if(!handle)
  2147. return 0; /* no handle, no size */
  2148. /* NOTE: this may very well truncate the size if it is larger than what
  2149. size_t can hold, so libssh2_sftp_tell64() is really the function you
  2150. should use */
  2151. return (size_t)(handle->u.file.offset);
  2152. }
  2153. /* libssh2_sftp_tell64
  2154. * Return the current read/write pointer's offset
  2155. */
  2156. LIBSSH2_API libssh2_uint64_t
  2157. libssh2_sftp_tell64(LIBSSH2_SFTP_HANDLE *handle)
  2158. {
  2159. if(!handle)
  2160. return 0; /* no handle, no size */
  2161. return handle->u.file.offset;
  2162. }
  2163. /*
  2164. * Flush all remaining incoming SFTP packets and zombies.
  2165. */
  2166. static void sftp_packet_flush(LIBSSH2_SFTP *sftp)
  2167. {
  2168. LIBSSH2_CHANNEL *channel = sftp->channel;
  2169. LIBSSH2_SESSION *session = channel->session;
  2170. LIBSSH2_SFTP_PACKET *packet = _libssh2_list_first(&sftp->packets);
  2171. struct sftp_zombie_requests *zombie =
  2172. _libssh2_list_first(&sftp->zombie_requests);
  2173. while(packet) {
  2174. LIBSSH2_SFTP_PACKET *next;
  2175. /* check next struct in the list */
  2176. next = _libssh2_list_next(&packet->node);
  2177. _libssh2_list_remove(&packet->node);
  2178. LIBSSH2_FREE(session, packet->data);
  2179. LIBSSH2_FREE(session, packet);
  2180. packet = next;
  2181. }
  2182. while(zombie) {
  2183. /* figure out the next node */
  2184. struct sftp_zombie_requests *next = _libssh2_list_next(&zombie->node);
  2185. /* unlink the current one */
  2186. _libssh2_list_remove(&zombie->node);
  2187. /* free the memory */
  2188. LIBSSH2_FREE(session, zombie);
  2189. zombie = next;
  2190. }
  2191. }
  2192. /* sftp_close_handle
  2193. *
  2194. * Close a file or directory handle.
  2195. * Also frees handle resource and unlinks it from the SFTP structure.
  2196. * The handle is no longer usable after return of this function, unless
  2197. * the return value is LIBSSH2_ERROR_EAGAIN in which case this function
  2198. * should be called again.
  2199. */
  2200. static int
  2201. sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle)
  2202. {
  2203. LIBSSH2_SFTP *sftp = handle->sftp;
  2204. LIBSSH2_CHANNEL *channel = sftp->channel;
  2205. LIBSSH2_SESSION *session = channel->session;
  2206. size_t data_len;
  2207. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) */
  2208. uint32_t packet_len = handle->handle_len + 13;
  2209. unsigned char *s, *data = NULL;
  2210. int rc = 0;
  2211. if(handle->close_state == libssh2_NB_state_idle) {
  2212. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Closing handle");
  2213. s = handle->close_packet = LIBSSH2_ALLOC(session, packet_len);
  2214. if(!handle->close_packet) {
  2215. handle->close_state = libssh2_NB_state_idle;
  2216. rc = _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2217. "Unable to allocate memory for FXP_CLOSE "
  2218. "packet");
  2219. }
  2220. else {
  2221. _libssh2_store_u32(&s, packet_len - 4);
  2222. *(s++) = SSH_FXP_CLOSE;
  2223. handle->close_request_id = sftp->request_id++;
  2224. _libssh2_store_u32(&s, handle->close_request_id);
  2225. _libssh2_store_str(&s, handle->handle, handle->handle_len);
  2226. handle->close_state = libssh2_NB_state_created;
  2227. }
  2228. }
  2229. if(handle->close_state == libssh2_NB_state_created) {
  2230. rc = _libssh2_channel_write(channel, 0, handle->close_packet,
  2231. packet_len);
  2232. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2233. return rc;
  2234. }
  2235. else if((ssize_t)packet_len != rc) {
  2236. handle->close_state = libssh2_NB_state_idle;
  2237. rc = _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2238. "Unable to send FXP_CLOSE command");
  2239. }
  2240. else
  2241. handle->close_state = libssh2_NB_state_sent;
  2242. LIBSSH2_FREE(session, handle->close_packet);
  2243. handle->close_packet = NULL;
  2244. }
  2245. if(handle->close_state == libssh2_NB_state_sent) {
  2246. rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
  2247. handle->close_request_id, &data,
  2248. &data_len, 9);
  2249. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2250. return rc;
  2251. }
  2252. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2253. if(data_len > 0) {
  2254. LIBSSH2_FREE(session, data);
  2255. }
  2256. data = NULL;
  2257. _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2258. "Packet too short in FXP_CLOSE command");
  2259. }
  2260. else if(rc) {
  2261. _libssh2_error(session, rc,
  2262. "Error waiting for status message");
  2263. }
  2264. handle->close_state = libssh2_NB_state_sent1;
  2265. }
  2266. if(!data) {
  2267. /* if it reaches this point with data unset, something unwanted
  2268. happened for which we should have set an error code */
  2269. assert(rc);
  2270. }
  2271. else {
  2272. int retcode = _libssh2_ntohu32(data + 5);
  2273. LIBSSH2_FREE(session, data);
  2274. if(retcode != LIBSSH2_FX_OK) {
  2275. sftp->last_errno = retcode;
  2276. handle->close_state = libssh2_NB_state_idle;
  2277. rc = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2278. "SFTP Protocol Error");
  2279. }
  2280. }
  2281. /* remove this handle from the parent's list */
  2282. _libssh2_list_remove(&handle->node);
  2283. if(handle->handle_type == LIBSSH2_SFTP_HANDLE_DIR) {
  2284. if(handle->u.dir.names_left)
  2285. LIBSSH2_FREE(session, handle->u.dir.names_packet);
  2286. }
  2287. else if(handle->handle_type == LIBSSH2_SFTP_HANDLE_FILE) {
  2288. if(handle->u.file.data)
  2289. LIBSSH2_FREE(session, handle->u.file.data);
  2290. }
  2291. sftp_packetlist_flush(handle);
  2292. sftp->read_state = libssh2_NB_state_idle;
  2293. handle->close_state = libssh2_NB_state_idle;
  2294. LIBSSH2_FREE(session, handle);
  2295. return rc;
  2296. }
  2297. /* libssh2_sftp_close_handle
  2298. *
  2299. * Close a file or directory handle
  2300. * Also frees handle resource and unlinks it from the SFTP structure
  2301. */
  2302. LIBSSH2_API int
  2303. libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE *hnd)
  2304. {
  2305. int rc;
  2306. if(!hnd)
  2307. return LIBSSH2_ERROR_BAD_USE;
  2308. BLOCK_ADJUST(rc, hnd->sftp->channel->session, sftp_close_handle(hnd));
  2309. return rc;
  2310. }
  2311. /* sftp_unlink
  2312. * Delete a file from the remote server
  2313. */
  2314. static int sftp_unlink(LIBSSH2_SFTP *sftp, const char *filename,
  2315. size_t filename_len)
  2316. {
  2317. LIBSSH2_CHANNEL *channel = sftp->channel;
  2318. LIBSSH2_SESSION *session = channel->session;
  2319. size_t data_len;
  2320. int retcode;
  2321. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) */
  2322. uint32_t packet_len = filename_len + 13;
  2323. unsigned char *s, *data;
  2324. int rc;
  2325. if(sftp->unlink_state == libssh2_NB_state_idle) {
  2326. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Unlinking %s", filename);
  2327. s = sftp->unlink_packet = LIBSSH2_ALLOC(session, packet_len);
  2328. if(!sftp->unlink_packet) {
  2329. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2330. "Unable to allocate memory for FXP_REMOVE "
  2331. "packet");
  2332. }
  2333. _libssh2_store_u32(&s, packet_len - 4);
  2334. *(s++) = SSH_FXP_REMOVE;
  2335. sftp->unlink_request_id = sftp->request_id++;
  2336. _libssh2_store_u32(&s, sftp->unlink_request_id);
  2337. _libssh2_store_str(&s, filename, filename_len);
  2338. sftp->unlink_state = libssh2_NB_state_created;
  2339. }
  2340. if(sftp->unlink_state == libssh2_NB_state_created) {
  2341. rc = _libssh2_channel_write(channel, 0, sftp->unlink_packet,
  2342. packet_len);
  2343. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2344. return rc;
  2345. }
  2346. else if((ssize_t)packet_len != rc) {
  2347. LIBSSH2_FREE(session, sftp->unlink_packet);
  2348. sftp->unlink_packet = NULL;
  2349. sftp->unlink_state = libssh2_NB_state_idle;
  2350. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2351. "Unable to send FXP_REMOVE command");
  2352. }
  2353. LIBSSH2_FREE(session, sftp->unlink_packet);
  2354. sftp->unlink_packet = NULL;
  2355. sftp->unlink_state = libssh2_NB_state_sent;
  2356. }
  2357. rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
  2358. sftp->unlink_request_id, &data,
  2359. &data_len, 9);
  2360. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2361. return rc;
  2362. }
  2363. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2364. if(data_len > 0) {
  2365. LIBSSH2_FREE(session, data);
  2366. }
  2367. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2368. "SFTP unlink packet too short");
  2369. }
  2370. else if(rc) {
  2371. sftp->unlink_state = libssh2_NB_state_idle;
  2372. return _libssh2_error(session, rc,
  2373. "Error waiting for FXP STATUS");
  2374. }
  2375. sftp->unlink_state = libssh2_NB_state_idle;
  2376. retcode = _libssh2_ntohu32(data + 5);
  2377. LIBSSH2_FREE(session, data);
  2378. if(retcode == LIBSSH2_FX_OK) {
  2379. return 0;
  2380. }
  2381. else {
  2382. sftp->last_errno = retcode;
  2383. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2384. "SFTP Protocol Error");
  2385. }
  2386. }
  2387. /* libssh2_sftp_unlink_ex
  2388. * Delete a file from the remote server
  2389. */
  2390. LIBSSH2_API int
  2391. libssh2_sftp_unlink_ex(LIBSSH2_SFTP *sftp, const char *filename,
  2392. unsigned int filename_len)
  2393. {
  2394. int rc;
  2395. if(!sftp)
  2396. return LIBSSH2_ERROR_BAD_USE;
  2397. BLOCK_ADJUST(rc, sftp->channel->session,
  2398. sftp_unlink(sftp, filename, filename_len));
  2399. return rc;
  2400. }
  2401. /*
  2402. * sftp_rename
  2403. *
  2404. * Rename a file on the remote server
  2405. */
  2406. static int sftp_rename(LIBSSH2_SFTP *sftp, const char *source_filename,
  2407. unsigned int source_filename_len,
  2408. const char *dest_filename,
  2409. unsigned int dest_filename_len, long flags)
  2410. {
  2411. LIBSSH2_CHANNEL *channel = sftp->channel;
  2412. LIBSSH2_SESSION *session = channel->session;
  2413. size_t data_len;
  2414. int retcode;
  2415. uint32_t packet_len =
  2416. source_filename_len + dest_filename_len + 17 + (sftp->version >=
  2417. 5 ? 4 : 0);
  2418. /* packet_len(4) + packet_type(1) + request_id(4) +
  2419. source_filename_len(4) + dest_filename_len(4) + flags(4){SFTP5+) */
  2420. unsigned char *data;
  2421. ssize_t rc;
  2422. if(sftp->version < 2) {
  2423. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2424. "Server does not support RENAME");
  2425. }
  2426. if(sftp->rename_state == libssh2_NB_state_idle) {
  2427. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Renaming %s to %s",
  2428. source_filename, dest_filename);
  2429. sftp->rename_s = sftp->rename_packet =
  2430. LIBSSH2_ALLOC(session, packet_len);
  2431. if(!sftp->rename_packet) {
  2432. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2433. "Unable to allocate memory for FXP_RENAME "
  2434. "packet");
  2435. }
  2436. _libssh2_store_u32(&sftp->rename_s, packet_len - 4);
  2437. *(sftp->rename_s++) = SSH_FXP_RENAME;
  2438. sftp->rename_request_id = sftp->request_id++;
  2439. _libssh2_store_u32(&sftp->rename_s, sftp->rename_request_id);
  2440. _libssh2_store_str(&sftp->rename_s, source_filename,
  2441. source_filename_len);
  2442. _libssh2_store_str(&sftp->rename_s, dest_filename, dest_filename_len);
  2443. if(sftp->version >= 5)
  2444. _libssh2_store_u32(&sftp->rename_s, flags);
  2445. sftp->rename_state = libssh2_NB_state_created;
  2446. }
  2447. if(sftp->rename_state == libssh2_NB_state_created) {
  2448. rc = _libssh2_channel_write(channel, 0, sftp->rename_packet,
  2449. sftp->rename_s - sftp->rename_packet);
  2450. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2451. return rc;
  2452. }
  2453. else if((ssize_t)packet_len != rc) {
  2454. LIBSSH2_FREE(session, sftp->rename_packet);
  2455. sftp->rename_packet = NULL;
  2456. sftp->rename_state = libssh2_NB_state_idle;
  2457. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2458. "Unable to send FXP_RENAME command");
  2459. }
  2460. LIBSSH2_FREE(session, sftp->rename_packet);
  2461. sftp->rename_packet = NULL;
  2462. sftp->rename_state = libssh2_NB_state_sent;
  2463. }
  2464. rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
  2465. sftp->rename_request_id, &data,
  2466. &data_len, 9);
  2467. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2468. return rc;
  2469. }
  2470. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2471. if(data_len > 0) {
  2472. LIBSSH2_FREE(session, data);
  2473. }
  2474. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2475. "SFTP rename packet too short");
  2476. }
  2477. else if(rc) {
  2478. sftp->rename_state = libssh2_NB_state_idle;
  2479. return _libssh2_error(session, rc,
  2480. "Error waiting for FXP STATUS");
  2481. }
  2482. sftp->rename_state = libssh2_NB_state_idle;
  2483. retcode = _libssh2_ntohu32(data + 5);
  2484. LIBSSH2_FREE(session, data);
  2485. sftp->last_errno = retcode;
  2486. /* now convert the SFTP error code to libssh2 return code or error
  2487. message */
  2488. switch(retcode) {
  2489. case LIBSSH2_FX_OK:
  2490. retcode = LIBSSH2_ERROR_NONE;
  2491. break;
  2492. case LIBSSH2_FX_FILE_ALREADY_EXISTS:
  2493. retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2494. "File already exists and "
  2495. "SSH_FXP_RENAME_OVERWRITE not specified");
  2496. break;
  2497. case LIBSSH2_FX_OP_UNSUPPORTED:
  2498. retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2499. "Operation Not Supported");
  2500. break;
  2501. default:
  2502. retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2503. "SFTP Protocol Error");
  2504. break;
  2505. }
  2506. return retcode;
  2507. }
  2508. /* libssh2_sftp_rename_ex
  2509. * Rename a file on the remote server
  2510. */
  2511. LIBSSH2_API int
  2512. libssh2_sftp_rename_ex(LIBSSH2_SFTP *sftp, const char *source_filename,
  2513. unsigned int source_filename_len,
  2514. const char *dest_filename,
  2515. unsigned int dest_filename_len, long flags)
  2516. {
  2517. int rc;
  2518. if(!sftp)
  2519. return LIBSSH2_ERROR_BAD_USE;
  2520. BLOCK_ADJUST(rc, sftp->channel->session,
  2521. sftp_rename(sftp, source_filename, source_filename_len,
  2522. dest_filename, dest_filename_len, flags));
  2523. return rc;
  2524. }
  2525. /*
  2526. * sftp_fstatvfs
  2527. *
  2528. * Get file system statistics
  2529. */
  2530. static int sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
  2531. {
  2532. LIBSSH2_SFTP *sftp = handle->sftp;
  2533. LIBSSH2_CHANNEL *channel = sftp->channel;
  2534. LIBSSH2_SESSION *session = channel->session;
  2535. size_t data_len;
  2536. /* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4)
  2537. + handle_len (4) */
  2538. /* 20 = strlen ("fstatvfs@openssh.com") */
  2539. uint32_t packet_len = handle->handle_len + 20 + 17;
  2540. unsigned char *packet, *s, *data;
  2541. ssize_t rc;
  2542. unsigned int flag;
  2543. static const unsigned char responses[2] =
  2544. { SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS };
  2545. if(sftp->fstatvfs_state == libssh2_NB_state_idle) {
  2546. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  2547. "Getting file system statistics");
  2548. s = packet = LIBSSH2_ALLOC(session, packet_len);
  2549. if(!packet) {
  2550. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2551. "Unable to allocate memory for FXP_EXTENDED "
  2552. "packet");
  2553. }
  2554. _libssh2_store_u32(&s, packet_len - 4);
  2555. *(s++) = SSH_FXP_EXTENDED;
  2556. sftp->fstatvfs_request_id = sftp->request_id++;
  2557. _libssh2_store_u32(&s, sftp->fstatvfs_request_id);
  2558. _libssh2_store_str(&s, "fstatvfs@openssh.com", 20);
  2559. _libssh2_store_str(&s, handle->handle, handle->handle_len);
  2560. sftp->fstatvfs_state = libssh2_NB_state_created;
  2561. }
  2562. else {
  2563. packet = sftp->fstatvfs_packet;
  2564. }
  2565. if(sftp->fstatvfs_state == libssh2_NB_state_created) {
  2566. rc = _libssh2_channel_write(channel, 0, packet, packet_len);
  2567. if(rc == LIBSSH2_ERROR_EAGAIN ||
  2568. (0 <= rc && rc < (ssize_t)packet_len)) {
  2569. sftp->fstatvfs_packet = packet;
  2570. return LIBSSH2_ERROR_EAGAIN;
  2571. }
  2572. LIBSSH2_FREE(session, packet);
  2573. sftp->fstatvfs_packet = NULL;
  2574. if(rc < 0) {
  2575. sftp->fstatvfs_state = libssh2_NB_state_idle;
  2576. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2577. "_libssh2_channel_write() failed");
  2578. }
  2579. sftp->fstatvfs_state = libssh2_NB_state_sent;
  2580. }
  2581. rc = sftp_packet_requirev(sftp, 2, responses, sftp->fstatvfs_request_id,
  2582. &data, &data_len, 9);
  2583. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2584. return rc;
  2585. }
  2586. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2587. if(data_len > 0) {
  2588. LIBSSH2_FREE(session, data);
  2589. }
  2590. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2591. "SFTP rename packet too short");
  2592. }
  2593. else if(rc) {
  2594. sftp->fstatvfs_state = libssh2_NB_state_idle;
  2595. return _libssh2_error(session, rc,
  2596. "Error waiting for FXP EXTENDED REPLY");
  2597. }
  2598. if(data[0] == SSH_FXP_STATUS) {
  2599. int retcode = _libssh2_ntohu32(data + 5);
  2600. sftp->fstatvfs_state = libssh2_NB_state_idle;
  2601. LIBSSH2_FREE(session, data);
  2602. sftp->last_errno = retcode;
  2603. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2604. "SFTP Protocol Error");
  2605. }
  2606. if(data_len < 93) {
  2607. LIBSSH2_FREE(session, data);
  2608. sftp->fstatvfs_state = libssh2_NB_state_idle;
  2609. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2610. "SFTP Protocol Error: short response");
  2611. }
  2612. sftp->fstatvfs_state = libssh2_NB_state_idle;
  2613. st->f_bsize = _libssh2_ntohu64(data + 5);
  2614. st->f_frsize = _libssh2_ntohu64(data + 13);
  2615. st->f_blocks = _libssh2_ntohu64(data + 21);
  2616. st->f_bfree = _libssh2_ntohu64(data + 29);
  2617. st->f_bavail = _libssh2_ntohu64(data + 37);
  2618. st->f_files = _libssh2_ntohu64(data + 45);
  2619. st->f_ffree = _libssh2_ntohu64(data + 53);
  2620. st->f_favail = _libssh2_ntohu64(data + 61);
  2621. st->f_fsid = _libssh2_ntohu64(data + 69);
  2622. flag = (unsigned int)_libssh2_ntohu64(data + 77);
  2623. st->f_namemax = _libssh2_ntohu64(data + 85);
  2624. st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY)
  2625. ? LIBSSH2_SFTP_ST_RDONLY : 0;
  2626. st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID)
  2627. ? LIBSSH2_SFTP_ST_NOSUID : 0;
  2628. LIBSSH2_FREE(session, data);
  2629. return 0;
  2630. }
  2631. /* libssh2_sftp_fstatvfs
  2632. * Get filesystem space and inode utilization (requires fstatvfs@openssh.com
  2633. * support on the server)
  2634. */
  2635. LIBSSH2_API int
  2636. libssh2_sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st)
  2637. {
  2638. int rc;
  2639. if(!handle || !st)
  2640. return LIBSSH2_ERROR_BAD_USE;
  2641. BLOCK_ADJUST(rc, handle->sftp->channel->session,
  2642. sftp_fstatvfs(handle, st));
  2643. return rc;
  2644. }
  2645. /*
  2646. * sftp_statvfs
  2647. *
  2648. * Get file system statistics
  2649. */
  2650. static int sftp_statvfs(LIBSSH2_SFTP *sftp, const char *path,
  2651. unsigned int path_len, LIBSSH2_SFTP_STATVFS *st)
  2652. {
  2653. LIBSSH2_CHANNEL *channel = sftp->channel;
  2654. LIBSSH2_SESSION *session = channel->session;
  2655. size_t data_len;
  2656. /* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4)
  2657. + path_len (4) */
  2658. /* 19 = strlen ("statvfs@openssh.com") */
  2659. uint32_t packet_len = path_len + 19 + 17;
  2660. unsigned char *packet, *s, *data;
  2661. ssize_t rc;
  2662. unsigned int flag;
  2663. static const unsigned char responses[2] =
  2664. { SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS };
  2665. if(sftp->statvfs_state == libssh2_NB_state_idle) {
  2666. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  2667. "Getting file system statistics of %s", path);
  2668. s = packet = LIBSSH2_ALLOC(session, packet_len);
  2669. if(!packet) {
  2670. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2671. "Unable to allocate memory for FXP_EXTENDED "
  2672. "packet");
  2673. }
  2674. _libssh2_store_u32(&s, packet_len - 4);
  2675. *(s++) = SSH_FXP_EXTENDED;
  2676. sftp->statvfs_request_id = sftp->request_id++;
  2677. _libssh2_store_u32(&s, sftp->statvfs_request_id);
  2678. _libssh2_store_str(&s, "statvfs@openssh.com", 19);
  2679. _libssh2_store_str(&s, path, path_len);
  2680. sftp->statvfs_state = libssh2_NB_state_created;
  2681. }
  2682. else {
  2683. packet = sftp->statvfs_packet;
  2684. }
  2685. if(sftp->statvfs_state == libssh2_NB_state_created) {
  2686. rc = _libssh2_channel_write(channel, 0, packet, packet_len);
  2687. if(rc == LIBSSH2_ERROR_EAGAIN ||
  2688. (0 <= rc && rc < (ssize_t)packet_len)) {
  2689. sftp->statvfs_packet = packet;
  2690. return LIBSSH2_ERROR_EAGAIN;
  2691. }
  2692. LIBSSH2_FREE(session, packet);
  2693. sftp->statvfs_packet = NULL;
  2694. if(rc < 0) {
  2695. sftp->statvfs_state = libssh2_NB_state_idle;
  2696. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2697. "_libssh2_channel_write() failed");
  2698. }
  2699. sftp->statvfs_state = libssh2_NB_state_sent;
  2700. }
  2701. rc = sftp_packet_requirev(sftp, 2, responses, sftp->statvfs_request_id,
  2702. &data, &data_len, 9);
  2703. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2704. return rc;
  2705. }
  2706. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2707. if(data_len > 0) {
  2708. LIBSSH2_FREE(session, data);
  2709. }
  2710. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2711. "SFTP fstat packet too short");
  2712. }
  2713. else if(rc) {
  2714. sftp->statvfs_state = libssh2_NB_state_idle;
  2715. return _libssh2_error(session, rc,
  2716. "Error waiting for FXP EXTENDED REPLY");
  2717. }
  2718. if(data[0] == SSH_FXP_STATUS) {
  2719. int retcode = _libssh2_ntohu32(data + 5);
  2720. sftp->statvfs_state = libssh2_NB_state_idle;
  2721. LIBSSH2_FREE(session, data);
  2722. sftp->last_errno = retcode;
  2723. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2724. "SFTP Protocol Error");
  2725. }
  2726. if(data_len < 93) {
  2727. LIBSSH2_FREE(session, data);
  2728. sftp->statvfs_state = libssh2_NB_state_idle;
  2729. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2730. "SFTP Protocol Error: short response");
  2731. }
  2732. sftp->statvfs_state = libssh2_NB_state_idle;
  2733. st->f_bsize = _libssh2_ntohu64(data + 5);
  2734. st->f_frsize = _libssh2_ntohu64(data + 13);
  2735. st->f_blocks = _libssh2_ntohu64(data + 21);
  2736. st->f_bfree = _libssh2_ntohu64(data + 29);
  2737. st->f_bavail = _libssh2_ntohu64(data + 37);
  2738. st->f_files = _libssh2_ntohu64(data + 45);
  2739. st->f_ffree = _libssh2_ntohu64(data + 53);
  2740. st->f_favail = _libssh2_ntohu64(data + 61);
  2741. st->f_fsid = _libssh2_ntohu64(data + 69);
  2742. flag = (unsigned int)_libssh2_ntohu64(data + 77);
  2743. st->f_namemax = _libssh2_ntohu64(data + 85);
  2744. st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY)
  2745. ? LIBSSH2_SFTP_ST_RDONLY : 0;
  2746. st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID)
  2747. ? LIBSSH2_SFTP_ST_NOSUID : 0;
  2748. LIBSSH2_FREE(session, data);
  2749. return 0;
  2750. }
  2751. /* libssh2_sftp_statvfs_ex
  2752. * Get filesystem space and inode utilization (requires statvfs@openssh.com
  2753. * support on the server)
  2754. */
  2755. LIBSSH2_API int
  2756. libssh2_sftp_statvfs(LIBSSH2_SFTP *sftp, const char *path,
  2757. size_t path_len, LIBSSH2_SFTP_STATVFS *st)
  2758. {
  2759. int rc;
  2760. if(!sftp || !st)
  2761. return LIBSSH2_ERROR_BAD_USE;
  2762. BLOCK_ADJUST(rc, sftp->channel->session, sftp_statvfs(sftp, path, path_len,
  2763. st));
  2764. return rc;
  2765. }
  2766. /*
  2767. * sftp_mkdir
  2768. *
  2769. * Create an SFTP directory
  2770. */
  2771. static int sftp_mkdir(LIBSSH2_SFTP *sftp, const char *path,
  2772. unsigned int path_len, long mode)
  2773. {
  2774. LIBSSH2_CHANNEL *channel = sftp->channel;
  2775. LIBSSH2_SESSION *session = channel->session;
  2776. LIBSSH2_SFTP_ATTRIBUTES attrs = {
  2777. 0, 0, 0, 0, 0, 0, 0
  2778. };
  2779. size_t data_len;
  2780. int retcode;
  2781. ssize_t packet_len;
  2782. unsigned char *packet, *s, *data;
  2783. int rc;
  2784. if(mode != LIBSSH2_SFTP_DEFAULT_MODE) {
  2785. /* Filetype in SFTP 3 and earlier */
  2786. attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS;
  2787. attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR;
  2788. }
  2789. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
  2790. packet_len = path_len + 13 + sftp_attrsize(attrs.flags);
  2791. if(sftp->mkdir_state == libssh2_NB_state_idle) {
  2792. _libssh2_debug(session, LIBSSH2_TRACE_SFTP,
  2793. "Creating directory %s with mode 0%lo", path, mode);
  2794. s = packet = LIBSSH2_ALLOC(session, packet_len);
  2795. if(!packet) {
  2796. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2797. "Unable to allocate memory for FXP_MKDIR "
  2798. "packet");
  2799. }
  2800. _libssh2_store_u32(&s, packet_len - 4);
  2801. *(s++) = SSH_FXP_MKDIR;
  2802. sftp->mkdir_request_id = sftp->request_id++;
  2803. _libssh2_store_u32(&s, sftp->mkdir_request_id);
  2804. _libssh2_store_str(&s, path, path_len);
  2805. s += sftp_attr2bin(s, &attrs);
  2806. sftp->mkdir_state = libssh2_NB_state_created;
  2807. }
  2808. else {
  2809. packet = sftp->mkdir_packet;
  2810. }
  2811. if(sftp->mkdir_state == libssh2_NB_state_created) {
  2812. rc = _libssh2_channel_write(channel, 0, packet, packet_len);
  2813. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2814. sftp->mkdir_packet = packet;
  2815. return rc;
  2816. }
  2817. if(packet_len != rc) {
  2818. LIBSSH2_FREE(session, packet);
  2819. sftp->mkdir_state = libssh2_NB_state_idle;
  2820. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2821. "_libssh2_channel_write() failed");
  2822. }
  2823. LIBSSH2_FREE(session, packet);
  2824. sftp->mkdir_state = libssh2_NB_state_sent;
  2825. sftp->mkdir_packet = NULL;
  2826. }
  2827. rc = sftp_packet_require(sftp, SSH_FXP_STATUS, sftp->mkdir_request_id,
  2828. &data, &data_len, 9);
  2829. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2830. return rc;
  2831. }
  2832. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2833. if(data_len > 0) {
  2834. LIBSSH2_FREE(session, data);
  2835. }
  2836. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2837. "SFTP mkdir packet too short");
  2838. }
  2839. else if(rc) {
  2840. sftp->mkdir_state = libssh2_NB_state_idle;
  2841. return _libssh2_error(session, rc,
  2842. "Error waiting for FXP STATUS");
  2843. }
  2844. sftp->mkdir_state = libssh2_NB_state_idle;
  2845. retcode = _libssh2_ntohu32(data + 5);
  2846. LIBSSH2_FREE(session, data);
  2847. if(retcode == LIBSSH2_FX_OK) {
  2848. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "OK!");
  2849. return 0;
  2850. }
  2851. else {
  2852. sftp->last_errno = retcode;
  2853. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2854. "SFTP Protocol Error");
  2855. }
  2856. }
  2857. /*
  2858. * libssh2_sftp_mkdir_ex
  2859. *
  2860. * Create an SFTP directory
  2861. */
  2862. LIBSSH2_API int
  2863. libssh2_sftp_mkdir_ex(LIBSSH2_SFTP *sftp, const char *path,
  2864. unsigned int path_len, long mode)
  2865. {
  2866. int rc;
  2867. if(!sftp)
  2868. return LIBSSH2_ERROR_BAD_USE;
  2869. BLOCK_ADJUST(rc, sftp->channel->session,
  2870. sftp_mkdir(sftp, path, path_len, mode));
  2871. return rc;
  2872. }
  2873. /* sftp_rmdir
  2874. * Remove a directory
  2875. */
  2876. static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path,
  2877. unsigned int path_len)
  2878. {
  2879. LIBSSH2_CHANNEL *channel = sftp->channel;
  2880. LIBSSH2_SESSION *session = channel->session;
  2881. size_t data_len;
  2882. int retcode;
  2883. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
  2884. ssize_t packet_len = path_len + 13;
  2885. unsigned char *s, *data;
  2886. int rc;
  2887. if(sftp->rmdir_state == libssh2_NB_state_idle) {
  2888. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "Removing directory: %s",
  2889. path);
  2890. s = sftp->rmdir_packet = LIBSSH2_ALLOC(session, packet_len);
  2891. if(!sftp->rmdir_packet) {
  2892. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2893. "Unable to allocate memory for FXP_RMDIR "
  2894. "packet");
  2895. }
  2896. _libssh2_store_u32(&s, packet_len - 4);
  2897. *(s++) = SSH_FXP_RMDIR;
  2898. sftp->rmdir_request_id = sftp->request_id++;
  2899. _libssh2_store_u32(&s, sftp->rmdir_request_id);
  2900. _libssh2_store_str(&s, path, path_len);
  2901. sftp->rmdir_state = libssh2_NB_state_created;
  2902. }
  2903. if(sftp->rmdir_state == libssh2_NB_state_created) {
  2904. rc = _libssh2_channel_write(channel, 0, sftp->rmdir_packet,
  2905. packet_len);
  2906. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2907. return rc;
  2908. }
  2909. else if(packet_len != rc) {
  2910. LIBSSH2_FREE(session, sftp->rmdir_packet);
  2911. sftp->rmdir_packet = NULL;
  2912. sftp->rmdir_state = libssh2_NB_state_idle;
  2913. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  2914. "Unable to send FXP_RMDIR command");
  2915. }
  2916. LIBSSH2_FREE(session, sftp->rmdir_packet);
  2917. sftp->rmdir_packet = NULL;
  2918. sftp->rmdir_state = libssh2_NB_state_sent;
  2919. }
  2920. rc = sftp_packet_require(sftp, SSH_FXP_STATUS,
  2921. sftp->rmdir_request_id, &data, &data_len, 9);
  2922. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2923. return rc;
  2924. }
  2925. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  2926. if(data_len > 0) {
  2927. LIBSSH2_FREE(session, data);
  2928. }
  2929. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2930. "SFTP rmdir packet too short");
  2931. }
  2932. else if(rc) {
  2933. sftp->rmdir_state = libssh2_NB_state_idle;
  2934. return _libssh2_error(session, rc,
  2935. "Error waiting for FXP STATUS");
  2936. }
  2937. sftp->rmdir_state = libssh2_NB_state_idle;
  2938. retcode = _libssh2_ntohu32(data + 5);
  2939. LIBSSH2_FREE(session, data);
  2940. if(retcode == LIBSSH2_FX_OK) {
  2941. return 0;
  2942. }
  2943. else {
  2944. sftp->last_errno = retcode;
  2945. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  2946. "SFTP Protocol Error");
  2947. }
  2948. }
  2949. /* libssh2_sftp_rmdir_ex
  2950. * Remove a directory
  2951. */
  2952. LIBSSH2_API int
  2953. libssh2_sftp_rmdir_ex(LIBSSH2_SFTP *sftp, const char *path,
  2954. unsigned int path_len)
  2955. {
  2956. int rc;
  2957. if(!sftp)
  2958. return LIBSSH2_ERROR_BAD_USE;
  2959. BLOCK_ADJUST(rc, sftp->channel->session,
  2960. sftp_rmdir(sftp, path, path_len));
  2961. return rc;
  2962. }
  2963. /* sftp_stat
  2964. * Stat a file or symbolic link
  2965. */
  2966. static int sftp_stat(LIBSSH2_SFTP *sftp, const char *path,
  2967. unsigned int path_len, int stat_type,
  2968. LIBSSH2_SFTP_ATTRIBUTES * attrs)
  2969. {
  2970. LIBSSH2_CHANNEL *channel = sftp->channel;
  2971. LIBSSH2_SESSION *session = channel->session;
  2972. size_t data_len;
  2973. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
  2974. ssize_t packet_len =
  2975. path_len + 13 +
  2976. ((stat_type ==
  2977. LIBSSH2_SFTP_SETSTAT) ? sftp_attrsize(attrs->flags) : 0);
  2978. unsigned char *s, *data;
  2979. static const unsigned char stat_responses[2] =
  2980. { SSH_FXP_ATTRS, SSH_FXP_STATUS };
  2981. int rc;
  2982. if(sftp->stat_state == libssh2_NB_state_idle) {
  2983. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%s %s",
  2984. (stat_type == LIBSSH2_SFTP_SETSTAT) ? "Set-statting" :
  2985. (stat_type ==
  2986. LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), path);
  2987. s = sftp->stat_packet = LIBSSH2_ALLOC(session, packet_len);
  2988. if(!sftp->stat_packet) {
  2989. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  2990. "Unable to allocate memory for FXP_*STAT "
  2991. "packet");
  2992. }
  2993. _libssh2_store_u32(&s, packet_len - 4);
  2994. switch(stat_type) {
  2995. case LIBSSH2_SFTP_SETSTAT:
  2996. *(s++) = SSH_FXP_SETSTAT;
  2997. break;
  2998. case LIBSSH2_SFTP_LSTAT:
  2999. *(s++) = SSH_FXP_LSTAT;
  3000. break;
  3001. case LIBSSH2_SFTP_STAT:
  3002. default:
  3003. *(s++) = SSH_FXP_STAT;
  3004. }
  3005. sftp->stat_request_id = sftp->request_id++;
  3006. _libssh2_store_u32(&s, sftp->stat_request_id);
  3007. _libssh2_store_str(&s, path, path_len);
  3008. if(stat_type == LIBSSH2_SFTP_SETSTAT)
  3009. s += sftp_attr2bin(s, attrs);
  3010. sftp->stat_state = libssh2_NB_state_created;
  3011. }
  3012. if(sftp->stat_state == libssh2_NB_state_created) {
  3013. rc = _libssh2_channel_write(channel, 0, sftp->stat_packet, packet_len);
  3014. if(rc == LIBSSH2_ERROR_EAGAIN) {
  3015. return rc;
  3016. }
  3017. else if(packet_len != rc) {
  3018. LIBSSH2_FREE(session, sftp->stat_packet);
  3019. sftp->stat_packet = NULL;
  3020. sftp->stat_state = libssh2_NB_state_idle;
  3021. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  3022. "Unable to send STAT/LSTAT/SETSTAT command");
  3023. }
  3024. LIBSSH2_FREE(session, sftp->stat_packet);
  3025. sftp->stat_packet = NULL;
  3026. sftp->stat_state = libssh2_NB_state_sent;
  3027. }
  3028. rc = sftp_packet_requirev(sftp, 2, stat_responses,
  3029. sftp->stat_request_id, &data, &data_len, 9);
  3030. if(rc == LIBSSH2_ERROR_EAGAIN)
  3031. return rc;
  3032. else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  3033. if(data_len > 0) {
  3034. LIBSSH2_FREE(session, data);
  3035. }
  3036. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3037. "SFTP stat packet too short");
  3038. }
  3039. else if(rc) {
  3040. sftp->stat_state = libssh2_NB_state_idle;
  3041. return _libssh2_error(session, rc,
  3042. "Timeout waiting for status message");
  3043. }
  3044. sftp->stat_state = libssh2_NB_state_idle;
  3045. if(data[0] == SSH_FXP_STATUS) {
  3046. int retcode;
  3047. retcode = _libssh2_ntohu32(data + 5);
  3048. LIBSSH2_FREE(session, data);
  3049. if(retcode == LIBSSH2_FX_OK) {
  3050. memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
  3051. return 0;
  3052. }
  3053. else {
  3054. sftp->last_errno = retcode;
  3055. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3056. "SFTP Protocol Error");
  3057. }
  3058. }
  3059. memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
  3060. if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) {
  3061. LIBSSH2_FREE(session, data);
  3062. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3063. "Attributes too short in SFTP fstat");
  3064. }
  3065. LIBSSH2_FREE(session, data);
  3066. return 0;
  3067. }
  3068. /* libssh2_sftp_stat_ex
  3069. * Stat a file or symbolic link
  3070. */
  3071. LIBSSH2_API int
  3072. libssh2_sftp_stat_ex(LIBSSH2_SFTP *sftp, const char *path,
  3073. unsigned int path_len, int stat_type,
  3074. LIBSSH2_SFTP_ATTRIBUTES *attrs)
  3075. {
  3076. int rc;
  3077. if(!sftp)
  3078. return LIBSSH2_ERROR_BAD_USE;
  3079. BLOCK_ADJUST(rc, sftp->channel->session,
  3080. sftp_stat(sftp, path, path_len, stat_type, attrs));
  3081. return rc;
  3082. }
  3083. /* sftp_symlink
  3084. * Read or set a symlink
  3085. */
  3086. static int sftp_symlink(LIBSSH2_SFTP *sftp, const char *path,
  3087. unsigned int path_len, char *target,
  3088. unsigned int target_len, int link_type)
  3089. {
  3090. LIBSSH2_CHANNEL *channel = sftp->channel;
  3091. LIBSSH2_SESSION *session = channel->session;
  3092. size_t data_len, link_len;
  3093. /* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */
  3094. ssize_t packet_len =
  3095. path_len + 13 +
  3096. ((link_type == LIBSSH2_SFTP_SYMLINK) ? (4 + target_len) : 0);
  3097. unsigned char *s, *data;
  3098. static const unsigned char link_responses[2] =
  3099. { SSH_FXP_NAME, SSH_FXP_STATUS };
  3100. int retcode;
  3101. if((sftp->version < 3) && (link_type != LIBSSH2_SFTP_REALPATH)) {
  3102. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3103. "Server does not support SYMLINK or READLINK");
  3104. }
  3105. if(sftp->symlink_state == libssh2_NB_state_idle) {
  3106. s = sftp->symlink_packet = LIBSSH2_ALLOC(session, packet_len);
  3107. if(!sftp->symlink_packet) {
  3108. return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
  3109. "Unable to allocate memory for "
  3110. "SYMLINK/READLINK/REALPATH packet");
  3111. }
  3112. _libssh2_debug(session, LIBSSH2_TRACE_SFTP, "%s %s on %s",
  3113. (link_type ==
  3114. LIBSSH2_SFTP_SYMLINK) ? "Creating" : "Reading",
  3115. (link_type ==
  3116. LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", path);
  3117. _libssh2_store_u32(&s, packet_len - 4);
  3118. switch(link_type) {
  3119. case LIBSSH2_SFTP_REALPATH:
  3120. *(s++) = SSH_FXP_REALPATH;
  3121. break;
  3122. case LIBSSH2_SFTP_SYMLINK:
  3123. *(s++) = SSH_FXP_SYMLINK;
  3124. break;
  3125. case LIBSSH2_SFTP_READLINK:
  3126. default:
  3127. *(s++) = SSH_FXP_READLINK;
  3128. }
  3129. sftp->symlink_request_id = sftp->request_id++;
  3130. _libssh2_store_u32(&s, sftp->symlink_request_id);
  3131. _libssh2_store_str(&s, path, path_len);
  3132. if(link_type == LIBSSH2_SFTP_SYMLINK)
  3133. _libssh2_store_str(&s, target, target_len);
  3134. sftp->symlink_state = libssh2_NB_state_created;
  3135. }
  3136. if(sftp->symlink_state == libssh2_NB_state_created) {
  3137. ssize_t rc = _libssh2_channel_write(channel, 0, sftp->symlink_packet,
  3138. packet_len);
  3139. if(rc == LIBSSH2_ERROR_EAGAIN)
  3140. return rc;
  3141. else if(packet_len != rc) {
  3142. LIBSSH2_FREE(session, sftp->symlink_packet);
  3143. sftp->symlink_packet = NULL;
  3144. sftp->symlink_state = libssh2_NB_state_idle;
  3145. return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
  3146. "Unable to send SYMLINK/READLINK command");
  3147. }
  3148. LIBSSH2_FREE(session, sftp->symlink_packet);
  3149. sftp->symlink_packet = NULL;
  3150. sftp->symlink_state = libssh2_NB_state_sent;
  3151. }
  3152. retcode = sftp_packet_requirev(sftp, 2, link_responses,
  3153. sftp->symlink_request_id, &data,
  3154. &data_len, 9);
  3155. if(retcode == LIBSSH2_ERROR_EAGAIN)
  3156. return retcode;
  3157. else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) {
  3158. if(data_len > 0) {
  3159. LIBSSH2_FREE(session, data);
  3160. }
  3161. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3162. "SFTP symlink packet too short");
  3163. }
  3164. else if(retcode) {
  3165. sftp->symlink_state = libssh2_NB_state_idle;
  3166. return _libssh2_error(session, retcode,
  3167. "Error waiting for status message");
  3168. }
  3169. sftp->symlink_state = libssh2_NB_state_idle;
  3170. if(data[0] == SSH_FXP_STATUS) {
  3171. retcode = _libssh2_ntohu32(data + 5);
  3172. LIBSSH2_FREE(session, data);
  3173. if(retcode == LIBSSH2_FX_OK)
  3174. return LIBSSH2_ERROR_NONE;
  3175. else {
  3176. sftp->last_errno = retcode;
  3177. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3178. "SFTP Protocol Error");
  3179. }
  3180. }
  3181. if(_libssh2_ntohu32(data + 5) < 1) {
  3182. LIBSSH2_FREE(session, data);
  3183. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3184. "Invalid READLINK/REALPATH response, "
  3185. "no name entries");
  3186. }
  3187. if(data_len < 13) {
  3188. if(data_len > 0) {
  3189. LIBSSH2_FREE(session, data);
  3190. }
  3191. return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
  3192. "SFTP stat packet too short");
  3193. }
  3194. /* this reads a u32 and stores it into a signed 32bit value */
  3195. link_len = _libssh2_ntohu32(data + 9);
  3196. if(link_len < target_len) {
  3197. memcpy(target, data + 13, link_len);
  3198. target[link_len] = 0;
  3199. retcode = (int)link_len;
  3200. }
  3201. else
  3202. retcode = LIBSSH2_ERROR_BUFFER_TOO_SMALL;
  3203. LIBSSH2_FREE(session, data);
  3204. return retcode;
  3205. }
  3206. /* libssh2_sftp_symlink_ex
  3207. * Read or set a symlink
  3208. */
  3209. LIBSSH2_API int
  3210. libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp, const char *path,
  3211. unsigned int path_len, char *target,
  3212. unsigned int target_len, int link_type)
  3213. {
  3214. int rc;
  3215. if(!sftp)
  3216. return LIBSSH2_ERROR_BAD_USE;
  3217. BLOCK_ADJUST(rc, sftp->channel->session,
  3218. sftp_symlink(sftp, path, path_len, target, target_len,
  3219. link_type));
  3220. return rc;
  3221. }
  3222. /* libssh2_sftp_last_error
  3223. * Returns the last error code reported by SFTP
  3224. */
  3225. LIBSSH2_API unsigned long
  3226. libssh2_sftp_last_error(LIBSSH2_SFTP *sftp)
  3227. {
  3228. if(!sftp)
  3229. return 0;
  3230. return sftp->last_errno;
  3231. }
  3232. /* libssh2_sftp_get_channel
  3233. * Return the channel of sftp, then caller can control the channel's behavior.
  3234. */
  3235. LIBSSH2_API LIBSSH2_CHANNEL *
  3236. libssh2_sftp_get_channel(LIBSSH2_SFTP *sftp)
  3237. {
  3238. if(!sftp)
  3239. return NULL;
  3240. return sftp->channel;
  3241. }