wolfssh.c 34 KB

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