wolfssh.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef USE_WOLFSSH
  26. #include <limits.h>
  27. #include <wolfssh/ssh.h>
  28. #include <wolfssh/wolfsftp.h>
  29. #include "urldata.h"
  30. #include "cfilters.h"
  31. #include "connect.h"
  32. #include "sendf.h"
  33. #include "progress.h"
  34. #include "curl_path.h"
  35. #include "strtoofft.h"
  36. #include "transfer.h"
  37. #include "speedcheck.h"
  38. #include "select.h"
  39. #include "multiif.h"
  40. #include "warnless.h"
  41. #include "strdup.h"
  42. /* The last 3 #include files should be in this order */
  43. #include "curl_printf.h"
  44. #include "curl_memory.h"
  45. #include "memdebug.h"
  46. static CURLcode wssh_connect(struct Curl_easy *data, bool *done);
  47. static CURLcode wssh_multi_statemach(struct Curl_easy *data, bool *done);
  48. static CURLcode wssh_do(struct Curl_easy *data, bool *done);
  49. #if 0
  50. static CURLcode wscp_done(struct Curl_easy *data,
  51. CURLcode, bool premature);
  52. static CURLcode wscp_doing(struct Curl_easy *data,
  53. bool *dophase_done);
  54. static CURLcode wscp_disconnect(struct Curl_easy *data,
  55. struct connectdata *conn,
  56. bool dead_connection);
  57. #endif
  58. static CURLcode wsftp_done(struct Curl_easy *data,
  59. CURLcode, bool premature);
  60. static CURLcode wsftp_doing(struct Curl_easy *data,
  61. bool *dophase_done);
  62. static CURLcode wsftp_disconnect(struct Curl_easy *data,
  63. struct connectdata *conn,
  64. bool dead);
  65. static int wssh_getsock(struct Curl_easy *data,
  66. struct connectdata *conn,
  67. curl_socket_t *sock);
  68. static CURLcode wssh_setup_connection(struct Curl_easy *data,
  69. struct connectdata *conn);
  70. #if 0
  71. /*
  72. * SCP protocol handler.
  73. */
  74. const struct Curl_handler Curl_handler_scp = {
  75. "SCP", /* scheme */
  76. wssh_setup_connection, /* setup_connection */
  77. wssh_do, /* do_it */
  78. wscp_done, /* done */
  79. ZERO_NULL, /* do_more */
  80. wssh_connect, /* connect_it */
  81. wssh_multi_statemach, /* connecting */
  82. wscp_doing, /* doing */
  83. wssh_getsock, /* proto_getsock */
  84. wssh_getsock, /* doing_getsock */
  85. ZERO_NULL, /* domore_getsock */
  86. wssh_getsock, /* perform_getsock */
  87. wscp_disconnect, /* disconnect */
  88. ZERO_NULL, /* write_resp */
  89. ZERO_NULL, /* write_resp_hd */
  90. ZERO_NULL, /* connection_check */
  91. ZERO_NULL, /* attach connection */
  92. PORT_SSH, /* defport */
  93. CURLPROTO_SCP, /* protocol */
  94. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
  95. | PROTOPT_NOURLQUERY /* flags */
  96. };
  97. #endif
  98. /*
  99. * SFTP protocol handler.
  100. */
  101. const struct Curl_handler Curl_handler_sftp = {
  102. "SFTP", /* scheme */
  103. wssh_setup_connection, /* setup_connection */
  104. wssh_do, /* do_it */
  105. wsftp_done, /* done */
  106. ZERO_NULL, /* do_more */
  107. wssh_connect, /* connect_it */
  108. wssh_multi_statemach, /* connecting */
  109. wsftp_doing, /* doing */
  110. wssh_getsock, /* proto_getsock */
  111. wssh_getsock, /* doing_getsock */
  112. ZERO_NULL, /* domore_getsock */
  113. wssh_getsock, /* perform_getsock */
  114. wsftp_disconnect, /* disconnect */
  115. ZERO_NULL, /* write_resp */
  116. ZERO_NULL, /* write_resp_hd */
  117. ZERO_NULL, /* connection_check */
  118. ZERO_NULL, /* attach connection */
  119. PORT_SSH, /* defport */
  120. CURLPROTO_SFTP, /* protocol */
  121. CURLPROTO_SFTP, /* family */
  122. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
  123. | PROTOPT_NOURLQUERY /* flags */
  124. };
  125. /*
  126. * SSH State machine related code
  127. */
  128. /* This is the ONLY way to change SSH state! */
  129. static void state(struct Curl_easy *data, sshstate nowstate)
  130. {
  131. struct connectdata *conn = data->conn;
  132. struct ssh_conn *sshc = &conn->proto.sshc;
  133. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  134. /* for debug purposes */
  135. static const char * const names[] = {
  136. "SSH_STOP",
  137. "SSH_INIT",
  138. "SSH_S_STARTUP",
  139. "SSH_HOSTKEY",
  140. "SSH_AUTHLIST",
  141. "SSH_AUTH_PKEY_INIT",
  142. "SSH_AUTH_PKEY",
  143. "SSH_AUTH_PASS_INIT",
  144. "SSH_AUTH_PASS",
  145. "SSH_AUTH_AGENT_INIT",
  146. "SSH_AUTH_AGENT_LIST",
  147. "SSH_AUTH_AGENT",
  148. "SSH_AUTH_HOST_INIT",
  149. "SSH_AUTH_HOST",
  150. "SSH_AUTH_KEY_INIT",
  151. "SSH_AUTH_KEY",
  152. "SSH_AUTH_GSSAPI",
  153. "SSH_AUTH_DONE",
  154. "SSH_SFTP_INIT",
  155. "SSH_SFTP_REALPATH",
  156. "SSH_SFTP_QUOTE_INIT",
  157. "SSH_SFTP_POSTQUOTE_INIT",
  158. "SSH_SFTP_QUOTE",
  159. "SSH_SFTP_NEXT_QUOTE",
  160. "SSH_SFTP_QUOTE_STAT",
  161. "SSH_SFTP_QUOTE_SETSTAT",
  162. "SSH_SFTP_QUOTE_SYMLINK",
  163. "SSH_SFTP_QUOTE_MKDIR",
  164. "SSH_SFTP_QUOTE_RENAME",
  165. "SSH_SFTP_QUOTE_RMDIR",
  166. "SSH_SFTP_QUOTE_UNLINK",
  167. "SSH_SFTP_QUOTE_STATVFS",
  168. "SSH_SFTP_GETINFO",
  169. "SSH_SFTP_FILETIME",
  170. "SSH_SFTP_TRANS_INIT",
  171. "SSH_SFTP_UPLOAD_INIT",
  172. "SSH_SFTP_CREATE_DIRS_INIT",
  173. "SSH_SFTP_CREATE_DIRS",
  174. "SSH_SFTP_CREATE_DIRS_MKDIR",
  175. "SSH_SFTP_READDIR_INIT",
  176. "SSH_SFTP_READDIR",
  177. "SSH_SFTP_READDIR_LINK",
  178. "SSH_SFTP_READDIR_BOTTOM",
  179. "SSH_SFTP_READDIR_DONE",
  180. "SSH_SFTP_DOWNLOAD_INIT",
  181. "SSH_SFTP_DOWNLOAD_STAT",
  182. "SSH_SFTP_CLOSE",
  183. "SSH_SFTP_SHUTDOWN",
  184. "SSH_SCP_TRANS_INIT",
  185. "SSH_SCP_UPLOAD_INIT",
  186. "SSH_SCP_DOWNLOAD_INIT",
  187. "SSH_SCP_DOWNLOAD",
  188. "SSH_SCP_DONE",
  189. "SSH_SCP_SEND_EOF",
  190. "SSH_SCP_WAIT_EOF",
  191. "SSH_SCP_WAIT_CLOSE",
  192. "SSH_SCP_CHANNEL_FREE",
  193. "SSH_SESSION_DISCONNECT",
  194. "SSH_SESSION_FREE",
  195. "QUIT"
  196. };
  197. /* a precaution to make sure the lists are in sync */
  198. DEBUGASSERT(sizeof(names)/sizeof(names[0]) == SSH_LAST);
  199. if(sshc->state != nowstate) {
  200. infof(data, "wolfssh %p state change from %s to %s",
  201. (void *)sshc, names[sshc->state], names[nowstate]);
  202. }
  203. #endif
  204. sshc->state = nowstate;
  205. }
  206. static ssize_t wscp_send(struct Curl_easy *data, int sockindex,
  207. const void *mem, size_t len, CURLcode *err)
  208. {
  209. ssize_t nwrite = 0;
  210. (void)data;
  211. (void)sockindex; /* we only support SCP on the fixed known primary socket */
  212. (void)mem;
  213. (void)len;
  214. (void)err;
  215. return nwrite;
  216. }
  217. static ssize_t wscp_recv(struct Curl_easy *data, int sockindex,
  218. char *mem, size_t len, CURLcode *err)
  219. {
  220. ssize_t nread = 0;
  221. (void)data;
  222. (void)sockindex; /* we only support SCP on the fixed known primary socket */
  223. (void)mem;
  224. (void)len;
  225. (void)err;
  226. return nread;
  227. }
  228. /* return number of sent bytes */
  229. static ssize_t wsftp_send(struct Curl_easy *data, int sockindex,
  230. const void *mem, size_t len, CURLcode *err)
  231. {
  232. struct connectdata *conn = data->conn;
  233. struct ssh_conn *sshc = &conn->proto.sshc;
  234. word32 offset[2];
  235. int rc;
  236. (void)sockindex;
  237. offset[0] = (word32)sshc->offset&0xFFFFFFFF;
  238. offset[1] = (word32)(sshc->offset>>32)&0xFFFFFFFF;
  239. rc = wolfSSH_SFTP_SendWritePacket(sshc->ssh_session, sshc->handle,
  240. sshc->handleSz,
  241. &offset[0],
  242. (byte *)mem, (word32)len);
  243. if(rc == WS_FATAL_ERROR)
  244. rc = wolfSSH_get_error(sshc->ssh_session);
  245. if(rc == WS_WANT_READ) {
  246. conn->waitfor = KEEP_RECV;
  247. *err = CURLE_AGAIN;
  248. return -1;
  249. }
  250. else if(rc == WS_WANT_WRITE) {
  251. conn->waitfor = KEEP_SEND;
  252. *err = CURLE_AGAIN;
  253. return -1;
  254. }
  255. if(rc < 0) {
  256. failf(data, "wolfSSH_SFTP_SendWritePacket returned %d", rc);
  257. return -1;
  258. }
  259. DEBUGASSERT(rc == (int)len);
  260. infof(data, "sent %zu bytes SFTP from offset %" CURL_FORMAT_CURL_OFF_T,
  261. len, sshc->offset);
  262. sshc->offset += len;
  263. return (ssize_t)rc;
  264. }
  265. /*
  266. * Return number of received (decrypted) bytes
  267. * or <0 on error
  268. */
  269. static ssize_t wsftp_recv(struct Curl_easy *data, int sockindex,
  270. char *mem, size_t len, CURLcode *err)
  271. {
  272. int rc;
  273. struct connectdata *conn = data->conn;
  274. struct ssh_conn *sshc = &conn->proto.sshc;
  275. word32 offset[2];
  276. (void)sockindex;
  277. offset[0] = (word32)sshc->offset&0xFFFFFFFF;
  278. offset[1] = (word32)(sshc->offset>>32)&0xFFFFFFFF;
  279. rc = wolfSSH_SFTP_SendReadPacket(sshc->ssh_session, sshc->handle,
  280. sshc->handleSz,
  281. &offset[0],
  282. (byte *)mem, (word32)len);
  283. if(rc == WS_FATAL_ERROR)
  284. rc = wolfSSH_get_error(sshc->ssh_session);
  285. if(rc == WS_WANT_READ) {
  286. conn->waitfor = KEEP_RECV;
  287. *err = CURLE_AGAIN;
  288. return -1;
  289. }
  290. else if(rc == WS_WANT_WRITE) {
  291. conn->waitfor = KEEP_SEND;
  292. *err = CURLE_AGAIN;
  293. return -1;
  294. }
  295. DEBUGASSERT(rc <= (int)len);
  296. if(rc < 0) {
  297. failf(data, "wolfSSH_SFTP_SendReadPacket returned %d", rc);
  298. return -1;
  299. }
  300. sshc->offset += len;
  301. return (ssize_t)rc;
  302. }
  303. /*
  304. * SSH setup and connection
  305. */
  306. static CURLcode wssh_setup_connection(struct Curl_easy *data,
  307. struct connectdata *conn)
  308. {
  309. struct SSHPROTO *ssh;
  310. (void)conn;
  311. data->req.p.ssh = ssh = calloc(1, sizeof(struct SSHPROTO));
  312. if(!ssh)
  313. return CURLE_OUT_OF_MEMORY;
  314. return CURLE_OK;
  315. }
  316. static int userauth(byte authtype,
  317. WS_UserAuthData* authdata,
  318. void *ctx)
  319. {
  320. struct Curl_easy *data = ctx;
  321. DEBUGF(infof(data, "wolfssh callback: type %s",
  322. authtype == WOLFSSH_USERAUTH_PASSWORD ? "PASSWORD" :
  323. "PUBLICCKEY"));
  324. if(authtype == WOLFSSH_USERAUTH_PASSWORD) {
  325. authdata->sf.password.password = (byte *)data->conn->passwd;
  326. authdata->sf.password.passwordSz = (word32) strlen(data->conn->passwd);
  327. }
  328. return 0;
  329. }
  330. static CURLcode wssh_connect(struct Curl_easy *data, bool *done)
  331. {
  332. struct connectdata *conn = data->conn;
  333. struct ssh_conn *sshc;
  334. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  335. int rc;
  336. /* initialize per-handle data if not already */
  337. if(!data->req.p.ssh)
  338. wssh_setup_connection(data, conn);
  339. /* We default to persistent connections. We set this already in this connect
  340. function to make the reuse checks properly be able to check this bit. */
  341. connkeep(conn, "SSH default");
  342. if(conn->handler->protocol & CURLPROTO_SCP) {
  343. conn->recv[FIRSTSOCKET] = wscp_recv;
  344. conn->send[FIRSTSOCKET] = wscp_send;
  345. }
  346. else {
  347. conn->recv[FIRSTSOCKET] = wsftp_recv;
  348. conn->send[FIRSTSOCKET] = wsftp_send;
  349. }
  350. sshc = &conn->proto.sshc;
  351. sshc->ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL);
  352. if(!sshc->ctx) {
  353. failf(data, "No wolfSSH context");
  354. goto error;
  355. }
  356. sshc->ssh_session = wolfSSH_new(sshc->ctx);
  357. if(!sshc->ssh_session) {
  358. failf(data, "No wolfSSH session");
  359. goto error;
  360. }
  361. rc = wolfSSH_SetUsername(sshc->ssh_session, conn->user);
  362. if(rc != WS_SUCCESS) {
  363. failf(data, "wolfSSH failed to set user name");
  364. goto error;
  365. }
  366. /* set callback for authentication */
  367. wolfSSH_SetUserAuth(sshc->ctx, userauth);
  368. wolfSSH_SetUserAuthCtx(sshc->ssh_session, data);
  369. rc = wolfSSH_set_fd(sshc->ssh_session, (int)sock);
  370. if(rc) {
  371. failf(data, "wolfSSH failed to set socket");
  372. goto error;
  373. }
  374. #if 0
  375. wolfSSH_Debugging_ON();
  376. #endif
  377. *done = TRUE;
  378. if(conn->handler->protocol & CURLPROTO_SCP)
  379. state(data, SSH_INIT);
  380. else
  381. state(data, SSH_SFTP_INIT);
  382. return wssh_multi_statemach(data, done);
  383. error:
  384. wolfSSH_free(sshc->ssh_session);
  385. wolfSSH_CTX_free(sshc->ctx);
  386. return CURLE_FAILED_INIT;
  387. }
  388. /*
  389. * wssh_statemach_act() runs the SSH state machine as far as it can without
  390. * blocking and without reaching the end. The data the pointer 'block' points
  391. * to will be set to TRUE if the wolfssh function returns EAGAIN meaning it
  392. * wants to be called again when the socket is ready
  393. */
  394. static CURLcode wssh_statemach_act(struct Curl_easy *data, bool *block)
  395. {
  396. CURLcode result = CURLE_OK;
  397. struct connectdata *conn = data->conn;
  398. struct ssh_conn *sshc = &conn->proto.sshc;
  399. struct SSHPROTO *sftp_scp = data->req.p.ssh;
  400. WS_SFTPNAME *name;
  401. int rc = 0;
  402. *block = FALSE; /* we're not blocking by default */
  403. do {
  404. switch(sshc->state) {
  405. case SSH_INIT:
  406. state(data, SSH_S_STARTUP);
  407. break;
  408. case SSH_S_STARTUP:
  409. rc = wolfSSH_connect(sshc->ssh_session);
  410. if(rc != WS_SUCCESS)
  411. rc = wolfSSH_get_error(sshc->ssh_session);
  412. if(rc == WS_WANT_READ) {
  413. *block = TRUE;
  414. conn->waitfor = KEEP_RECV;
  415. return CURLE_OK;
  416. }
  417. else if(rc == WS_WANT_WRITE) {
  418. *block = TRUE;
  419. conn->waitfor = KEEP_SEND;
  420. return CURLE_OK;
  421. }
  422. else if(rc != WS_SUCCESS) {
  423. state(data, SSH_STOP);
  424. return CURLE_SSH;
  425. }
  426. infof(data, "wolfssh connected");
  427. state(data, SSH_STOP);
  428. break;
  429. case SSH_STOP:
  430. break;
  431. case SSH_SFTP_INIT:
  432. rc = wolfSSH_SFTP_connect(sshc->ssh_session);
  433. if(rc != WS_SUCCESS)
  434. rc = wolfSSH_get_error(sshc->ssh_session);
  435. if(rc == WS_WANT_READ) {
  436. *block = TRUE;
  437. conn->waitfor = KEEP_RECV;
  438. return CURLE_OK;
  439. }
  440. else if(rc == WS_WANT_WRITE) {
  441. *block = TRUE;
  442. conn->waitfor = KEEP_SEND;
  443. return CURLE_OK;
  444. }
  445. else if(rc == WS_SUCCESS) {
  446. infof(data, "wolfssh SFTP connected");
  447. state(data, SSH_SFTP_REALPATH);
  448. }
  449. else {
  450. failf(data, "wolfssh SFTP connect error %d", rc);
  451. return CURLE_SSH;
  452. }
  453. break;
  454. case SSH_SFTP_REALPATH:
  455. name = wolfSSH_SFTP_RealPath(sshc->ssh_session, (char *)".");
  456. rc = wolfSSH_get_error(sshc->ssh_session);
  457. if(rc == WS_WANT_READ) {
  458. *block = TRUE;
  459. conn->waitfor = KEEP_RECV;
  460. return CURLE_OK;
  461. }
  462. else if(rc == WS_WANT_WRITE) {
  463. *block = TRUE;
  464. conn->waitfor = KEEP_SEND;
  465. return CURLE_OK;
  466. }
  467. else if(name && (rc == WS_SUCCESS)) {
  468. sshc->homedir = Curl_memdup0(name->fName, name->fSz);
  469. if(!sshc->homedir)
  470. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  471. wolfSSH_SFTPNAME_list_free(name);
  472. state(data, SSH_STOP);
  473. return CURLE_OK;
  474. }
  475. failf(data, "wolfssh SFTP realpath %d", rc);
  476. return CURLE_SSH;
  477. case SSH_SFTP_QUOTE_INIT:
  478. result = Curl_getworkingpath(data, sshc->homedir, &sftp_scp->path);
  479. if(result) {
  480. sshc->actualcode = result;
  481. state(data, SSH_STOP);
  482. break;
  483. }
  484. if(data->set.quote) {
  485. infof(data, "Sending quote commands");
  486. sshc->quote_item = data->set.quote;
  487. state(data, SSH_SFTP_QUOTE);
  488. }
  489. else {
  490. state(data, SSH_SFTP_GETINFO);
  491. }
  492. break;
  493. case SSH_SFTP_GETINFO:
  494. if(data->set.get_filetime) {
  495. state(data, SSH_SFTP_FILETIME);
  496. }
  497. else {
  498. state(data, SSH_SFTP_TRANS_INIT);
  499. }
  500. break;
  501. case SSH_SFTP_TRANS_INIT:
  502. if(data->state.upload)
  503. state(data, SSH_SFTP_UPLOAD_INIT);
  504. else {
  505. if(sftp_scp->path[strlen(sftp_scp->path)-1] == '/')
  506. state(data, SSH_SFTP_READDIR_INIT);
  507. else
  508. state(data, SSH_SFTP_DOWNLOAD_INIT);
  509. }
  510. break;
  511. case SSH_SFTP_UPLOAD_INIT: {
  512. word32 flags;
  513. WS_SFTP_FILEATRB createattrs;
  514. if(data->state.resume_from) {
  515. WS_SFTP_FILEATRB attrs;
  516. if(data->state.resume_from < 0) {
  517. rc = wolfSSH_SFTP_STAT(sshc->ssh_session, sftp_scp->path,
  518. &attrs);
  519. if(rc != WS_SUCCESS)
  520. break;
  521. if(rc) {
  522. data->state.resume_from = 0;
  523. }
  524. else {
  525. curl_off_t size = ((curl_off_t)attrs.sz[1] << 32) | attrs.sz[0];
  526. if(size < 0) {
  527. failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
  528. return CURLE_BAD_DOWNLOAD_RESUME;
  529. }
  530. data->state.resume_from = size;
  531. }
  532. }
  533. }
  534. if(data->set.remote_append)
  535. /* Try to open for append, but create if nonexisting */
  536. flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_CREAT|WOLFSSH_FXF_APPEND;
  537. else if(data->state.resume_from > 0)
  538. /* If we have restart position then open for append */
  539. flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_APPEND;
  540. else
  541. /* Clear file before writing (normal behavior) */
  542. flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_CREAT|WOLFSSH_FXF_TRUNC;
  543. memset(&createattrs, 0, sizeof(createattrs));
  544. createattrs.per = (word32)data->set.new_file_perms;
  545. sshc->handleSz = sizeof(sshc->handle);
  546. rc = wolfSSH_SFTP_Open(sshc->ssh_session, sftp_scp->path,
  547. flags, &createattrs,
  548. sshc->handle, &sshc->handleSz);
  549. if(rc == WS_FATAL_ERROR)
  550. rc = wolfSSH_get_error(sshc->ssh_session);
  551. if(rc == WS_WANT_READ) {
  552. *block = TRUE;
  553. conn->waitfor = KEEP_RECV;
  554. return CURLE_OK;
  555. }
  556. else if(rc == WS_WANT_WRITE) {
  557. *block = TRUE;
  558. conn->waitfor = KEEP_SEND;
  559. return CURLE_OK;
  560. }
  561. else if(rc == WS_SUCCESS) {
  562. infof(data, "wolfssh SFTP open succeeded");
  563. }
  564. else {
  565. failf(data, "wolfssh SFTP upload open failed: %d", rc);
  566. return CURLE_SSH;
  567. }
  568. state(data, SSH_SFTP_DOWNLOAD_STAT);
  569. /* If we have a restart point then we need to seek to the correct
  570. position. */
  571. if(data->state.resume_from > 0) {
  572. /* Let's read off the proper amount of bytes from the input. */
  573. int seekerr = CURL_SEEKFUNC_OK;
  574. if(data->set.seek_func) {
  575. Curl_set_in_callback(data, true);
  576. seekerr = data->set.seek_func(data->set.seek_client,
  577. data->state.resume_from, SEEK_SET);
  578. Curl_set_in_callback(data, false);
  579. }
  580. if(seekerr != CURL_SEEKFUNC_OK) {
  581. curl_off_t passed = 0;
  582. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  583. failf(data, "Could not seek stream");
  584. return CURLE_FTP_COULDNT_USE_REST;
  585. }
  586. /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  587. do {
  588. char scratch[4*1024];
  589. size_t readthisamountnow =
  590. (data->state.resume_from - passed >
  591. (curl_off_t)sizeof(scratch)) ?
  592. sizeof(scratch) : curlx_sotouz(data->state.resume_from - passed);
  593. size_t actuallyread;
  594. Curl_set_in_callback(data, true);
  595. actuallyread = data->state.fread_func(scratch, 1,
  596. readthisamountnow,
  597. data->state.in);
  598. Curl_set_in_callback(data, false);
  599. passed += actuallyread;
  600. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  601. /* this checks for greater-than only to make sure that the
  602. CURL_READFUNC_ABORT return code still aborts */
  603. failf(data, "Failed to read data");
  604. return CURLE_FTP_COULDNT_USE_REST;
  605. }
  606. } while(passed < data->state.resume_from);
  607. }
  608. /* now, decrease the size of the read */
  609. if(data->state.infilesize > 0) {
  610. data->state.infilesize -= data->state.resume_from;
  611. data->req.size = data->state.infilesize;
  612. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  613. }
  614. sshc->offset += data->state.resume_from;
  615. }
  616. if(data->state.infilesize > 0) {
  617. data->req.size = data->state.infilesize;
  618. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  619. }
  620. /* upload data */
  621. Curl_xfer_setup(data, -1, -1, FALSE, FIRSTSOCKET);
  622. /* not set by Curl_xfer_setup to preserve keepon bits */
  623. conn->sockfd = conn->writesockfd;
  624. if(result) {
  625. state(data, SSH_SFTP_CLOSE);
  626. sshc->actualcode = result;
  627. }
  628. else {
  629. /* store this original bitmask setup to use later on if we can't
  630. figure out a "real" bitmask */
  631. sshc->orig_waitfor = data->req.keepon;
  632. /* we want to use the _sending_ function even when the socket turns
  633. out readable as the underlying libssh2 sftp send function will deal
  634. with both accordingly */
  635. data->state.select_bits = CURL_CSELECT_OUT;
  636. /* since we don't really wait for anything at this point, we want the
  637. state machine to move on as soon as possible so we set a very short
  638. timeout here */
  639. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  640. state(data, SSH_STOP);
  641. }
  642. break;
  643. }
  644. case SSH_SFTP_DOWNLOAD_INIT:
  645. sshc->handleSz = sizeof(sshc->handle);
  646. rc = wolfSSH_SFTP_Open(sshc->ssh_session, sftp_scp->path,
  647. WOLFSSH_FXF_READ, NULL,
  648. sshc->handle, &sshc->handleSz);
  649. if(rc == WS_FATAL_ERROR)
  650. rc = wolfSSH_get_error(sshc->ssh_session);
  651. if(rc == WS_WANT_READ) {
  652. *block = TRUE;
  653. conn->waitfor = KEEP_RECV;
  654. return CURLE_OK;
  655. }
  656. else if(rc == WS_WANT_WRITE) {
  657. *block = TRUE;
  658. conn->waitfor = KEEP_SEND;
  659. return CURLE_OK;
  660. }
  661. else if(rc == WS_SUCCESS) {
  662. infof(data, "wolfssh SFTP open succeeded");
  663. state(data, SSH_SFTP_DOWNLOAD_STAT);
  664. return CURLE_OK;
  665. }
  666. failf(data, "wolfssh SFTP open failed: %d", rc);
  667. return CURLE_SSH;
  668. case SSH_SFTP_DOWNLOAD_STAT: {
  669. WS_SFTP_FILEATRB attrs;
  670. curl_off_t size;
  671. rc = wolfSSH_SFTP_STAT(sshc->ssh_session, sftp_scp->path, &attrs);
  672. if(rc == WS_FATAL_ERROR)
  673. rc = wolfSSH_get_error(sshc->ssh_session);
  674. if(rc == WS_WANT_READ) {
  675. *block = TRUE;
  676. conn->waitfor = KEEP_RECV;
  677. return CURLE_OK;
  678. }
  679. else if(rc == WS_WANT_WRITE) {
  680. *block = TRUE;
  681. conn->waitfor = KEEP_SEND;
  682. return CURLE_OK;
  683. }
  684. else if(rc == WS_SUCCESS) {
  685. infof(data, "wolfssh STAT succeeded");
  686. }
  687. else {
  688. failf(data, "wolfssh SFTP open failed: %d", rc);
  689. data->req.size = -1;
  690. data->req.maxdownload = -1;
  691. Curl_pgrsSetDownloadSize(data, -1);
  692. return CURLE_SSH;
  693. }
  694. size = ((curl_off_t)attrs.sz[1] <<32) | attrs.sz[0];
  695. data->req.size = size;
  696. data->req.maxdownload = size;
  697. Curl_pgrsSetDownloadSize(data, size);
  698. infof(data, "SFTP download %" CURL_FORMAT_CURL_OFF_T " bytes", size);
  699. /* We cannot seek with wolfSSH so resuming and range requests are not
  700. possible */
  701. if(data->state.use_range || data->state.resume_from) {
  702. infof(data, "wolfSSH cannot do range/seek on SFTP");
  703. return CURLE_BAD_DOWNLOAD_RESUME;
  704. }
  705. /* Setup the actual download */
  706. if(data->req.size == 0) {
  707. /* no data to transfer */
  708. Curl_xfer_setup(data, -1, -1, FALSE, -1);
  709. infof(data, "File already completely downloaded");
  710. state(data, SSH_STOP);
  711. break;
  712. }
  713. Curl_xfer_setup(data, FIRSTSOCKET, data->req.size, FALSE, -1);
  714. /* not set by Curl_xfer_setup to preserve keepon bits */
  715. conn->writesockfd = conn->sockfd;
  716. /* we want to use the _receiving_ function even when the socket turns
  717. out writableable as the underlying libssh2 recv function will deal
  718. with both accordingly */
  719. data->state.select_bits = CURL_CSELECT_IN;
  720. if(result) {
  721. /* this should never occur; the close state should be entered
  722. at the time the error occurs */
  723. state(data, SSH_SFTP_CLOSE);
  724. sshc->actualcode = result;
  725. }
  726. else {
  727. state(data, SSH_STOP);
  728. }
  729. break;
  730. }
  731. case SSH_SFTP_CLOSE:
  732. if(sshc->handleSz)
  733. rc = wolfSSH_SFTP_Close(sshc->ssh_session, sshc->handle,
  734. sshc->handleSz);
  735. else
  736. rc = WS_SUCCESS; /* directory listing */
  737. if(rc == WS_WANT_READ) {
  738. *block = TRUE;
  739. conn->waitfor = KEEP_RECV;
  740. return CURLE_OK;
  741. }
  742. else if(rc == WS_WANT_WRITE) {
  743. *block = TRUE;
  744. conn->waitfor = KEEP_SEND;
  745. return CURLE_OK;
  746. }
  747. else if(rc == WS_SUCCESS) {
  748. state(data, SSH_STOP);
  749. return CURLE_OK;
  750. }
  751. failf(data, "wolfssh SFTP CLOSE failed: %d", rc);
  752. return CURLE_SSH;
  753. case SSH_SFTP_READDIR_INIT:
  754. Curl_pgrsSetDownloadSize(data, -1);
  755. if(data->req.no_body) {
  756. state(data, SSH_STOP);
  757. break;
  758. }
  759. state(data, SSH_SFTP_READDIR);
  760. break;
  761. case SSH_SFTP_READDIR:
  762. name = wolfSSH_SFTP_LS(sshc->ssh_session, sftp_scp->path);
  763. if(!name)
  764. rc = wolfSSH_get_error(sshc->ssh_session);
  765. else
  766. rc = WS_SUCCESS;
  767. if(rc == WS_WANT_READ) {
  768. *block = TRUE;
  769. conn->waitfor = KEEP_RECV;
  770. return CURLE_OK;
  771. }
  772. else if(rc == WS_WANT_WRITE) {
  773. *block = TRUE;
  774. conn->waitfor = KEEP_SEND;
  775. return CURLE_OK;
  776. }
  777. else if(name && (rc == WS_SUCCESS)) {
  778. WS_SFTPNAME *origname = name;
  779. result = CURLE_OK;
  780. while(name) {
  781. char *line = aprintf("%s\n",
  782. data->set.list_only ?
  783. name->fName : name->lName);
  784. if(!line) {
  785. state(data, SSH_SFTP_CLOSE);
  786. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  787. break;
  788. }
  789. result = Curl_client_write(data, CLIENTWRITE_BODY,
  790. line, strlen(line));
  791. free(line);
  792. if(result) {
  793. sshc->actualcode = result;
  794. break;
  795. }
  796. name = name->next;
  797. }
  798. wolfSSH_SFTPNAME_list_free(origname);
  799. state(data, SSH_STOP);
  800. return result;
  801. }
  802. failf(data, "wolfssh SFTP ls failed: %d", rc);
  803. return CURLE_SSH;
  804. case SSH_SFTP_SHUTDOWN:
  805. Curl_safefree(sshc->homedir);
  806. wolfSSH_free(sshc->ssh_session);
  807. wolfSSH_CTX_free(sshc->ctx);
  808. state(data, SSH_STOP);
  809. return CURLE_OK;
  810. default:
  811. break;
  812. }
  813. } while(!rc && (sshc->state != SSH_STOP));
  814. return result;
  815. }
  816. /* called repeatedly until done from multi.c */
  817. static CURLcode wssh_multi_statemach(struct Curl_easy *data, bool *done)
  818. {
  819. struct connectdata *conn = data->conn;
  820. struct ssh_conn *sshc = &conn->proto.sshc;
  821. CURLcode result = CURLE_OK;
  822. bool block; /* we store the status and use that to provide a ssh_getsock()
  823. implementation */
  824. do {
  825. result = wssh_statemach_act(data, &block);
  826. *done = (sshc->state == SSH_STOP) ? TRUE : FALSE;
  827. /* if there's no error, it isn't done and it didn't EWOULDBLOCK, then
  828. try again */
  829. if(*done) {
  830. DEBUGF(infof(data, "wssh_statemach_act says DONE"));
  831. }
  832. } while(!result && !*done && !block);
  833. return result;
  834. }
  835. static
  836. CURLcode wscp_perform(struct Curl_easy *data,
  837. bool *connected,
  838. bool *dophase_done)
  839. {
  840. (void)data;
  841. (void)connected;
  842. (void)dophase_done;
  843. return CURLE_OK;
  844. }
  845. static
  846. CURLcode wsftp_perform(struct Curl_easy *data,
  847. bool *connected,
  848. bool *dophase_done)
  849. {
  850. CURLcode result = CURLE_OK;
  851. DEBUGF(infof(data, "DO phase starts"));
  852. *dophase_done = FALSE; /* not done yet */
  853. /* start the first command in the DO phase */
  854. state(data, SSH_SFTP_QUOTE_INIT);
  855. /* run the state-machine */
  856. result = wssh_multi_statemach(data, dophase_done);
  857. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  858. if(*dophase_done) {
  859. DEBUGF(infof(data, "DO phase is complete"));
  860. }
  861. return result;
  862. }
  863. /*
  864. * The DO function is generic for both protocols.
  865. */
  866. static CURLcode wssh_do(struct Curl_easy *data, bool *done)
  867. {
  868. CURLcode result;
  869. bool connected = 0;
  870. struct connectdata *conn = data->conn;
  871. struct ssh_conn *sshc = &conn->proto.sshc;
  872. *done = FALSE; /* default to false */
  873. data->req.size = -1; /* make sure this is unknown at this point */
  874. sshc->actualcode = CURLE_OK; /* reset error code */
  875. sshc->secondCreateDirs = 0; /* reset the create dir attempt state
  876. variable */
  877. Curl_pgrsSetUploadCounter(data, 0);
  878. Curl_pgrsSetDownloadCounter(data, 0);
  879. Curl_pgrsSetUploadSize(data, -1);
  880. Curl_pgrsSetDownloadSize(data, -1);
  881. if(conn->handler->protocol & CURLPROTO_SCP)
  882. result = wscp_perform(data, &connected, done);
  883. else
  884. result = wsftp_perform(data, &connected, done);
  885. return result;
  886. }
  887. static CURLcode wssh_block_statemach(struct Curl_easy *data,
  888. bool disconnect)
  889. {
  890. struct connectdata *conn = data->conn;
  891. struct ssh_conn *sshc = &conn->proto.sshc;
  892. CURLcode result = CURLE_OK;
  893. while((sshc->state != SSH_STOP) && !result) {
  894. bool block;
  895. timediff_t left = 1000;
  896. struct curltime now = Curl_now();
  897. result = wssh_statemach_act(data, &block);
  898. if(result)
  899. break;
  900. if(!disconnect) {
  901. if(Curl_pgrsUpdate(data))
  902. return CURLE_ABORTED_BY_CALLBACK;
  903. result = Curl_speedcheck(data, now);
  904. if(result)
  905. break;
  906. left = Curl_timeleft(data, NULL, FALSE);
  907. if(left < 0) {
  908. failf(data, "Operation timed out");
  909. return CURLE_OPERATION_TIMEDOUT;
  910. }
  911. }
  912. if(!result) {
  913. int dir = conn->waitfor;
  914. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  915. curl_socket_t fd_read = CURL_SOCKET_BAD;
  916. curl_socket_t fd_write = CURL_SOCKET_BAD;
  917. if(dir == KEEP_RECV)
  918. fd_read = sock;
  919. else if(dir == KEEP_SEND)
  920. fd_write = sock;
  921. /* wait for the socket to become ready */
  922. (void)Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write,
  923. left>1000?1000:left); /* ignore result */
  924. }
  925. }
  926. return result;
  927. }
  928. /* generic done function for both SCP and SFTP called from their specific
  929. done functions */
  930. static CURLcode wssh_done(struct Curl_easy *data, CURLcode status)
  931. {
  932. CURLcode result = CURLE_OK;
  933. struct SSHPROTO *sftp_scp = data->req.p.ssh;
  934. if(!status) {
  935. /* run the state-machine */
  936. result = wssh_block_statemach(data, FALSE);
  937. }
  938. else
  939. result = status;
  940. if(sftp_scp)
  941. Curl_safefree(sftp_scp->path);
  942. if(Curl_pgrsDone(data))
  943. return CURLE_ABORTED_BY_CALLBACK;
  944. data->req.keepon = 0; /* clear all bits */
  945. return result;
  946. }
  947. #if 0
  948. static CURLcode wscp_done(struct Curl_easy *data,
  949. CURLcode code, bool premature)
  950. {
  951. CURLcode result = CURLE_OK;
  952. (void)conn;
  953. (void)code;
  954. (void)premature;
  955. return result;
  956. }
  957. static CURLcode wscp_doing(struct Curl_easy *data,
  958. bool *dophase_done)
  959. {
  960. CURLcode result = CURLE_OK;
  961. (void)conn;
  962. (void)dophase_done;
  963. return result;
  964. }
  965. static CURLcode wscp_disconnect(struct Curl_easy *data,
  966. struct connectdata *conn, bool dead_connection)
  967. {
  968. CURLcode result = CURLE_OK;
  969. (void)data;
  970. (void)conn;
  971. (void)dead_connection;
  972. return result;
  973. }
  974. #endif
  975. static CURLcode wsftp_done(struct Curl_easy *data,
  976. CURLcode code, bool premature)
  977. {
  978. (void)premature;
  979. state(data, SSH_SFTP_CLOSE);
  980. return wssh_done(data, code);
  981. }
  982. static CURLcode wsftp_doing(struct Curl_easy *data,
  983. bool *dophase_done)
  984. {
  985. CURLcode result = wssh_multi_statemach(data, dophase_done);
  986. if(*dophase_done) {
  987. DEBUGF(infof(data, "DO phase is complete"));
  988. }
  989. return result;
  990. }
  991. static CURLcode wsftp_disconnect(struct Curl_easy *data,
  992. struct connectdata *conn,
  993. bool dead)
  994. {
  995. CURLcode result = CURLE_OK;
  996. (void)dead;
  997. DEBUGF(infof(data, "SSH DISCONNECT starts now"));
  998. if(conn->proto.sshc.ssh_session) {
  999. /* only if there's a session still around to use! */
  1000. state(data, SSH_SFTP_SHUTDOWN);
  1001. result = wssh_block_statemach(data, TRUE);
  1002. }
  1003. DEBUGF(infof(data, "SSH DISCONNECT is done"));
  1004. return result;
  1005. }
  1006. static int wssh_getsock(struct Curl_easy *data,
  1007. struct connectdata *conn,
  1008. curl_socket_t *sock)
  1009. {
  1010. int bitmap = GETSOCK_BLANK;
  1011. int dir = conn->waitfor;
  1012. (void)data;
  1013. sock[0] = conn->sock[FIRSTSOCKET];
  1014. if(dir == KEEP_RECV)
  1015. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  1016. else if(dir == KEEP_SEND)
  1017. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  1018. return bitmap;
  1019. }
  1020. void Curl_ssh_version(char *buffer, size_t buflen)
  1021. {
  1022. (void)msnprintf(buffer, buflen, "wolfssh/%s", LIBWOLFSSH_VERSION_STRING);
  1023. }
  1024. CURLcode Curl_ssh_init(void)
  1025. {
  1026. if(WS_SUCCESS != wolfSSH_Init()) {
  1027. DEBUGF(fprintf(stderr, "Error: wolfSSH_Init failed\n"));
  1028. return CURLE_FAILED_INIT;
  1029. }
  1030. return CURLE_OK;
  1031. }
  1032. void Curl_ssh_cleanup(void)
  1033. {
  1034. (void)wolfSSH_Cleanup();
  1035. }
  1036. #endif /* USE_WOLFSSH */