wolfssh.c 34 KB

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