wolfssh.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 "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. PORT_SSH, /* defport */
  91. CURLPROTO_SCP, /* protocol */
  92. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
  93. | PROTOPT_NOURLQUERY /* flags */
  94. };
  95. #endif
  96. /*
  97. * SFTP protocol handler.
  98. */
  99. const struct Curl_handler Curl_handler_sftp = {
  100. "SFTP", /* scheme */
  101. wssh_setup_connection, /* setup_connection */
  102. wssh_do, /* do_it */
  103. wsftp_done, /* done */
  104. ZERO_NULL, /* do_more */
  105. wssh_connect, /* connect_it */
  106. wssh_multi_statemach, /* connecting */
  107. wsftp_doing, /* doing */
  108. wssh_getsock, /* proto_getsock */
  109. wssh_getsock, /* doing_getsock */
  110. ZERO_NULL, /* domore_getsock */
  111. wssh_getsock, /* perform_getsock */
  112. wsftp_disconnect, /* disconnect */
  113. ZERO_NULL, /* write_resp */
  114. ZERO_NULL, /* write_resp_hd */
  115. ZERO_NULL, /* connection_check */
  116. ZERO_NULL, /* attach connection */
  117. PORT_SSH, /* defport */
  118. CURLPROTO_SFTP, /* protocol */
  119. CURLPROTO_SFTP, /* family */
  120. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
  121. | PROTOPT_NOURLQUERY /* flags */
  122. };
  123. /*
  124. * SSH State machine related code
  125. */
  126. /* This is the ONLY way to change SSH state! */
  127. static void state(struct Curl_easy *data, sshstate nowstate)
  128. {
  129. struct connectdata *conn = data->conn;
  130. struct ssh_conn *sshc = &conn->proto.sshc;
  131. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  132. /* for debug purposes */
  133. static const char * const names[] = {
  134. "SSH_STOP",
  135. "SSH_INIT",
  136. "SSH_S_STARTUP",
  137. "SSH_HOSTKEY",
  138. "SSH_AUTHLIST",
  139. "SSH_AUTH_PKEY_INIT",
  140. "SSH_AUTH_PKEY",
  141. "SSH_AUTH_PASS_INIT",
  142. "SSH_AUTH_PASS",
  143. "SSH_AUTH_AGENT_INIT",
  144. "SSH_AUTH_AGENT_LIST",
  145. "SSH_AUTH_AGENT",
  146. "SSH_AUTH_HOST_INIT",
  147. "SSH_AUTH_HOST",
  148. "SSH_AUTH_KEY_INIT",
  149. "SSH_AUTH_KEY",
  150. "SSH_AUTH_GSSAPI",
  151. "SSH_AUTH_DONE",
  152. "SSH_SFTP_INIT",
  153. "SSH_SFTP_REALPATH",
  154. "SSH_SFTP_QUOTE_INIT",
  155. "SSH_SFTP_POSTQUOTE_INIT",
  156. "SSH_SFTP_QUOTE",
  157. "SSH_SFTP_NEXT_QUOTE",
  158. "SSH_SFTP_QUOTE_STAT",
  159. "SSH_SFTP_QUOTE_SETSTAT",
  160. "SSH_SFTP_QUOTE_SYMLINK",
  161. "SSH_SFTP_QUOTE_MKDIR",
  162. "SSH_SFTP_QUOTE_RENAME",
  163. "SSH_SFTP_QUOTE_RMDIR",
  164. "SSH_SFTP_QUOTE_UNLINK",
  165. "SSH_SFTP_QUOTE_STATVFS",
  166. "SSH_SFTP_GETINFO",
  167. "SSH_SFTP_FILETIME",
  168. "SSH_SFTP_TRANS_INIT",
  169. "SSH_SFTP_UPLOAD_INIT",
  170. "SSH_SFTP_CREATE_DIRS_INIT",
  171. "SSH_SFTP_CREATE_DIRS",
  172. "SSH_SFTP_CREATE_DIRS_MKDIR",
  173. "SSH_SFTP_READDIR_INIT",
  174. "SSH_SFTP_READDIR",
  175. "SSH_SFTP_READDIR_LINK",
  176. "SSH_SFTP_READDIR_BOTTOM",
  177. "SSH_SFTP_READDIR_DONE",
  178. "SSH_SFTP_DOWNLOAD_INIT",
  179. "SSH_SFTP_DOWNLOAD_STAT",
  180. "SSH_SFTP_CLOSE",
  181. "SSH_SFTP_SHUTDOWN",
  182. "SSH_SCP_TRANS_INIT",
  183. "SSH_SCP_UPLOAD_INIT",
  184. "SSH_SCP_DOWNLOAD_INIT",
  185. "SSH_SCP_DOWNLOAD",
  186. "SSH_SCP_DONE",
  187. "SSH_SCP_SEND_EOF",
  188. "SSH_SCP_WAIT_EOF",
  189. "SSH_SCP_WAIT_CLOSE",
  190. "SSH_SCP_CHANNEL_FREE",
  191. "SSH_SESSION_DISCONNECT",
  192. "SSH_SESSION_FREE",
  193. "QUIT"
  194. };
  195. /* a precaution to make sure the lists are in sync */
  196. DEBUGASSERT(sizeof(names)/sizeof(names[0]) == SSH_LAST);
  197. if(sshc->state != nowstate) {
  198. infof(data, "wolfssh %p state change from %s to %s",
  199. (void *)sshc, names[sshc->state], names[nowstate]);
  200. }
  201. #endif
  202. sshc->state = nowstate;
  203. }
  204. static ssize_t wscp_send(struct Curl_easy *data, int sockindex,
  205. const void *mem, size_t len, bool eos,
  206. CURLcode *err)
  207. {
  208. ssize_t nwrite = 0;
  209. (void)data;
  210. (void)sockindex; /* we only support SCP on the fixed known primary socket */
  211. (void)mem;
  212. (void)len;
  213. (void)eos;
  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, bool eos, 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. (void)eos;
  238. offset[0] = (word32)sshc->offset & 0xFFFFFFFF;
  239. offset[1] = (word32)(sshc->offset >> 32) & 0xFFFFFFFF;
  240. rc = wolfSSH_SFTP_SendWritePacket(sshc->ssh_session, sshc->handle,
  241. sshc->handleSz,
  242. &offset[0],
  243. (byte *)mem, (word32)len);
  244. if(rc == WS_FATAL_ERROR)
  245. rc = wolfSSH_get_error(sshc->ssh_session);
  246. if(rc == WS_WANT_READ) {
  247. conn->waitfor = KEEP_RECV;
  248. *err = CURLE_AGAIN;
  249. return -1;
  250. }
  251. else if(rc == WS_WANT_WRITE) {
  252. conn->waitfor = KEEP_SEND;
  253. *err = CURLE_AGAIN;
  254. return -1;
  255. }
  256. if(rc < 0) {
  257. failf(data, "wolfSSH_SFTP_SendWritePacket returned %d", rc);
  258. return -1;
  259. }
  260. DEBUGASSERT(rc == (int)len);
  261. infof(data, "sent %zu bytes SFTP from offset %" FMT_OFF_T,
  262. len, sshc->offset);
  263. sshc->offset += len;
  264. return (ssize_t)rc;
  265. }
  266. /*
  267. * Return number of received (decrypted) bytes
  268. * or <0 on error
  269. */
  270. static ssize_t wsftp_recv(struct Curl_easy *data, int sockindex,
  271. char *mem, size_t len, CURLcode *err)
  272. {
  273. int rc;
  274. struct connectdata *conn = data->conn;
  275. struct ssh_conn *sshc = &conn->proto.sshc;
  276. word32 offset[2];
  277. (void)sockindex;
  278. offset[0] = (word32)sshc->offset & 0xFFFFFFFF;
  279. offset[1] = (word32)(sshc->offset >> 32) & 0xFFFFFFFF;
  280. rc = wolfSSH_SFTP_SendReadPacket(sshc->ssh_session, sshc->handle,
  281. sshc->handleSz,
  282. &offset[0],
  283. (byte *)mem, (word32)len);
  284. if(rc == WS_FATAL_ERROR)
  285. rc = wolfSSH_get_error(sshc->ssh_session);
  286. if(rc == WS_WANT_READ) {
  287. conn->waitfor = KEEP_RECV;
  288. *err = CURLE_AGAIN;
  289. return -1;
  290. }
  291. else if(rc == WS_WANT_WRITE) {
  292. conn->waitfor = KEEP_SEND;
  293. *err = CURLE_AGAIN;
  294. return -1;
  295. }
  296. DEBUGASSERT(rc <= (int)len);
  297. if(rc < 0) {
  298. failf(data, "wolfSSH_SFTP_SendReadPacket returned %d", rc);
  299. return -1;
  300. }
  301. sshc->offset += len;
  302. return (ssize_t)rc;
  303. }
  304. /*
  305. * SSH setup and connection
  306. */
  307. static CURLcode wssh_setup_connection(struct Curl_easy *data,
  308. struct connectdata *conn)
  309. {
  310. struct SSHPROTO *ssh;
  311. (void)conn;
  312. data->req.p.ssh = ssh = calloc(1, sizeof(struct SSHPROTO));
  313. if(!ssh)
  314. return CURLE_OUT_OF_MEMORY;
  315. return CURLE_OK;
  316. }
  317. static int userauth(byte authtype,
  318. WS_UserAuthData* authdata,
  319. void *ctx)
  320. {
  321. struct Curl_easy *data = ctx;
  322. DEBUGF(infof(data, "wolfssh callback: type %s",
  323. authtype == WOLFSSH_USERAUTH_PASSWORD ? "PASSWORD" :
  324. "PUBLICCKEY"));
  325. if(authtype == WOLFSSH_USERAUTH_PASSWORD) {
  326. authdata->sf.password.password = (byte *)data->conn->passwd;
  327. authdata->sf.password.passwordSz = (word32) strlen(data->conn->passwd);
  328. }
  329. return 0;
  330. }
  331. static CURLcode wssh_connect(struct Curl_easy *data, bool *done)
  332. {
  333. struct connectdata *conn = data->conn;
  334. struct ssh_conn *sshc;
  335. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  336. int rc;
  337. /* initialize per-handle data if not already */
  338. if(!data->req.p.ssh)
  339. wssh_setup_connection(data, conn);
  340. /* We default to persistent connections. We set this already in this connect
  341. function to make the reuse checks properly be able to check this bit. */
  342. connkeep(conn, "SSH default");
  343. if(conn->handler->protocol & CURLPROTO_SCP) {
  344. conn->recv[FIRSTSOCKET] = wscp_recv;
  345. conn->send[FIRSTSOCKET] = wscp_send;
  346. }
  347. else {
  348. conn->recv[FIRSTSOCKET] = wsftp_recv;
  349. conn->send[FIRSTSOCKET] = wsftp_send;
  350. }
  351. sshc = &conn->proto.sshc;
  352. sshc->ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL);
  353. if(!sshc->ctx) {
  354. failf(data, "No wolfSSH context");
  355. goto error;
  356. }
  357. sshc->ssh_session = wolfSSH_new(sshc->ctx);
  358. if(!sshc->ssh_session) {
  359. failf(data, "No wolfSSH session");
  360. goto error;
  361. }
  362. rc = wolfSSH_SetUsername(sshc->ssh_session, conn->user);
  363. if(rc != WS_SUCCESS) {
  364. failf(data, "wolfSSH failed to set username");
  365. goto error;
  366. }
  367. /* set callback for authentication */
  368. wolfSSH_SetUserAuth(sshc->ctx, userauth);
  369. wolfSSH_SetUserAuthCtx(sshc->ssh_session, data);
  370. rc = wolfSSH_set_fd(sshc->ssh_session, (int)sock);
  371. if(rc) {
  372. failf(data, "wolfSSH failed to set socket");
  373. goto error;
  374. }
  375. #if 0
  376. wolfSSH_Debugging_ON();
  377. #endif
  378. *done = TRUE;
  379. if(conn->handler->protocol & CURLPROTO_SCP)
  380. state(data, SSH_INIT);
  381. else
  382. state(data, SSH_SFTP_INIT);
  383. return wssh_multi_statemach(data, done);
  384. error:
  385. wolfSSH_free(sshc->ssh_session);
  386. wolfSSH_CTX_free(sshc->ctx);
  387. return CURLE_FAILED_INIT;
  388. }
  389. /*
  390. * wssh_statemach_act() runs the SSH state machine as far as it can without
  391. * blocking and without reaching the end. The data the pointer 'block' points
  392. * to will be set to TRUE if the wolfssh function returns EAGAIN meaning it
  393. * wants to be called again when the socket is ready
  394. */
  395. static CURLcode wssh_statemach_act(struct Curl_easy *data, bool *block)
  396. {
  397. CURLcode result = CURLE_OK;
  398. struct connectdata *conn = data->conn;
  399. struct ssh_conn *sshc = &conn->proto.sshc;
  400. struct SSHPROTO *sftp_scp = data->req.p.ssh;
  401. WS_SFTPNAME *name;
  402. int rc = 0;
  403. *block = FALSE; /* we are not blocking by default */
  404. do {
  405. switch(sshc->state) {
  406. case SSH_INIT:
  407. state(data, SSH_S_STARTUP);
  408. break;
  409. case SSH_S_STARTUP:
  410. rc = wolfSSH_connect(sshc->ssh_session);
  411. if(rc != WS_SUCCESS)
  412. rc = wolfSSH_get_error(sshc->ssh_session);
  413. if(rc == WS_WANT_READ) {
  414. *block = TRUE;
  415. conn->waitfor = KEEP_RECV;
  416. return CURLE_OK;
  417. }
  418. else if(rc == WS_WANT_WRITE) {
  419. *block = TRUE;
  420. conn->waitfor = KEEP_SEND;
  421. return CURLE_OK;
  422. }
  423. else if(rc != WS_SUCCESS) {
  424. state(data, SSH_STOP);
  425. return CURLE_SSH;
  426. }
  427. infof(data, "wolfssh connected");
  428. state(data, SSH_STOP);
  429. break;
  430. case SSH_STOP:
  431. break;
  432. case SSH_SFTP_INIT:
  433. rc = wolfSSH_SFTP_connect(sshc->ssh_session);
  434. if(rc != WS_SUCCESS)
  435. rc = wolfSSH_get_error(sshc->ssh_session);
  436. if(rc == WS_WANT_READ) {
  437. *block = TRUE;
  438. conn->waitfor = KEEP_RECV;
  439. return CURLE_OK;
  440. }
  441. else if(rc == WS_WANT_WRITE) {
  442. *block = TRUE;
  443. conn->waitfor = KEEP_SEND;
  444. return CURLE_OK;
  445. }
  446. else if(rc == WS_SUCCESS) {
  447. infof(data, "wolfssh SFTP connected");
  448. state(data, SSH_SFTP_REALPATH);
  449. }
  450. else {
  451. failf(data, "wolfssh SFTP connect error %d", rc);
  452. return CURLE_SSH;
  453. }
  454. break;
  455. case SSH_SFTP_REALPATH:
  456. name = wolfSSH_SFTP_RealPath(sshc->ssh_session, (char *)".");
  457. rc = wolfSSH_get_error(sshc->ssh_session);
  458. if(rc == WS_WANT_READ) {
  459. *block = TRUE;
  460. conn->waitfor = KEEP_RECV;
  461. return CURLE_OK;
  462. }
  463. else if(rc == WS_WANT_WRITE) {
  464. *block = TRUE;
  465. conn->waitfor = KEEP_SEND;
  466. return CURLE_OK;
  467. }
  468. else if(name && (rc == WS_SUCCESS)) {
  469. sshc->homedir = Curl_memdup0(name->fName, name->fSz);
  470. if(!sshc->homedir)
  471. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  472. wolfSSH_SFTPNAME_list_free(name);
  473. state(data, SSH_STOP);
  474. return CURLE_OK;
  475. }
  476. failf(data, "wolfssh SFTP realpath %d", rc);
  477. return CURLE_SSH;
  478. case SSH_SFTP_QUOTE_INIT:
  479. result = Curl_getworkingpath(data, sshc->homedir, &sftp_scp->path);
  480. if(result) {
  481. sshc->actualcode = result;
  482. state(data, SSH_STOP);
  483. break;
  484. }
  485. if(data->set.quote) {
  486. infof(data, "Sending quote commands");
  487. sshc->quote_item = data->set.quote;
  488. state(data, SSH_SFTP_QUOTE);
  489. }
  490. else {
  491. state(data, SSH_SFTP_GETINFO);
  492. }
  493. break;
  494. case SSH_SFTP_GETINFO:
  495. if(data->set.get_filetime) {
  496. state(data, SSH_SFTP_FILETIME);
  497. }
  498. else {
  499. state(data, SSH_SFTP_TRANS_INIT);
  500. }
  501. break;
  502. case SSH_SFTP_TRANS_INIT:
  503. if(data->state.upload)
  504. state(data, SSH_SFTP_UPLOAD_INIT);
  505. else {
  506. if(sftp_scp->path[strlen(sftp_scp->path)-1] == '/')
  507. state(data, SSH_SFTP_READDIR_INIT);
  508. else
  509. state(data, SSH_SFTP_DOWNLOAD_INIT);
  510. }
  511. break;
  512. case SSH_SFTP_UPLOAD_INIT: {
  513. word32 flags;
  514. WS_SFTP_FILEATRB createattrs;
  515. if(data->state.resume_from) {
  516. WS_SFTP_FILEATRB attrs;
  517. if(data->state.resume_from < 0) {
  518. rc = wolfSSH_SFTP_STAT(sshc->ssh_session, sftp_scp->path,
  519. &attrs);
  520. if(rc != WS_SUCCESS)
  521. break;
  522. if(rc) {
  523. data->state.resume_from = 0;
  524. }
  525. else {
  526. curl_off_t size = ((curl_off_t)attrs.sz[1] << 32) | attrs.sz[0];
  527. if(size < 0) {
  528. failf(data, "Bad file size (%" FMT_OFF_T ")", size);
  529. return CURLE_BAD_DOWNLOAD_RESUME;
  530. }
  531. data->state.resume_from = size;
  532. }
  533. }
  534. }
  535. if(data->set.remote_append)
  536. /* Try to open for append, but create if nonexisting */
  537. flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_CREAT|WOLFSSH_FXF_APPEND;
  538. else if(data->state.resume_from > 0)
  539. /* If we have restart position then open for append */
  540. flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_APPEND;
  541. else
  542. /* Clear file before writing (normal behavior) */
  543. flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_CREAT|WOLFSSH_FXF_TRUNC;
  544. memset(&createattrs, 0, sizeof(createattrs));
  545. createattrs.per = (word32)data->set.new_file_perms;
  546. sshc->handleSz = sizeof(sshc->handle);
  547. rc = wolfSSH_SFTP_Open(sshc->ssh_session, sftp_scp->path,
  548. flags, &createattrs,
  549. sshc->handle, &sshc->handleSz);
  550. if(rc == WS_FATAL_ERROR)
  551. rc = wolfSSH_get_error(sshc->ssh_session);
  552. if(rc == WS_WANT_READ) {
  553. *block = TRUE;
  554. conn->waitfor = KEEP_RECV;
  555. return CURLE_OK;
  556. }
  557. else if(rc == WS_WANT_WRITE) {
  558. *block = TRUE;
  559. conn->waitfor = KEEP_SEND;
  560. return CURLE_OK;
  561. }
  562. else if(rc == WS_SUCCESS) {
  563. infof(data, "wolfssh SFTP open succeeded");
  564. }
  565. else {
  566. failf(data, "wolfssh SFTP upload open failed: %d", rc);
  567. return CURLE_SSH;
  568. }
  569. state(data, SSH_SFTP_DOWNLOAD_STAT);
  570. /* If we have a restart point then we need to seek to the correct
  571. position. */
  572. if(data->state.resume_from > 0) {
  573. /* Let's read off the proper amount of bytes from the input. */
  574. int seekerr = CURL_SEEKFUNC_OK;
  575. if(data->set.seek_func) {
  576. Curl_set_in_callback(data, true);
  577. seekerr = data->set.seek_func(data->set.seek_client,
  578. data->state.resume_from, SEEK_SET);
  579. Curl_set_in_callback(data, false);
  580. }
  581. if(seekerr != CURL_SEEKFUNC_OK) {
  582. curl_off_t passed = 0;
  583. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  584. failf(data, "Could not seek stream");
  585. return CURLE_FTP_COULDNT_USE_REST;
  586. }
  587. /* seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */
  588. do {
  589. char scratch[4*1024];
  590. size_t readthisamountnow =
  591. (data->state.resume_from - passed >
  592. (curl_off_t)sizeof(scratch)) ?
  593. sizeof(scratch) : curlx_sotouz(data->state.resume_from - passed);
  594. size_t actuallyread;
  595. Curl_set_in_callback(data, true);
  596. actuallyread = data->state.fread_func(scratch, 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_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
  623. /* not set by Curl_xfer_setup 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 cannot
  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. data->state.select_bits = CURL_CSELECT_OUT;
  637. /* since we do not 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 %" FMT_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_xfer_setup_nop(data);
  710. infof(data, "File already completely downloaded");
  711. state(data, SSH_STOP);
  712. break;
  713. }
  714. Curl_xfer_setup1(data, CURL_XFER_RECV, data->req.size, FALSE);
  715. /* not set by Curl_xfer_setup 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. data->state.select_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->req.no_body) {
  757. state(data, SSH_STOP);
  758. break;
  759. }
  760. state(data, SSH_SFTP_READDIR);
  761. break;
  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 is no error, it is not done and it did not 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. DEBUGF(infof(data, "DO phase starts"));
  853. *dophase_done = FALSE; /* not done yet */
  854. /* start the first command in the DO phase */
  855. state(data, SSH_SFTP_QUOTE_INIT);
  856. /* run the state-machine */
  857. result = wssh_multi_statemach(data, dophase_done);
  858. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  859. if(*dophase_done) {
  860. DEBUGF(infof(data, "DO phase is complete"));
  861. }
  862. return result;
  863. }
  864. /*
  865. * The DO function is generic for both protocols.
  866. */
  867. static CURLcode wssh_do(struct Curl_easy *data, bool *done)
  868. {
  869. CURLcode result;
  870. bool connected = 0;
  871. struct connectdata *conn = data->conn;
  872. struct ssh_conn *sshc = &conn->proto.sshc;
  873. *done = FALSE; /* default to false */
  874. data->req.size = -1; /* make sure this is unknown at this point */
  875. sshc->actualcode = CURLE_OK; /* reset error code */
  876. sshc->secondCreateDirs = 0; /* reset the create dir attempt state
  877. variable */
  878. Curl_pgrsSetUploadCounter(data, 0);
  879. Curl_pgrsSetDownloadCounter(data, 0);
  880. Curl_pgrsSetUploadSize(data, -1);
  881. Curl_pgrsSetDownloadSize(data, -1);
  882. if(conn->handler->protocol & CURLPROTO_SCP)
  883. result = wscp_perform(data, &connected, done);
  884. else
  885. result = wsftp_perform(data, &connected, done);
  886. return result;
  887. }
  888. static CURLcode wssh_block_statemach(struct Curl_easy *data,
  889. bool disconnect)
  890. {
  891. struct connectdata *conn = data->conn;
  892. struct ssh_conn *sshc = &conn->proto.sshc;
  893. CURLcode result = CURLE_OK;
  894. while((sshc->state != SSH_STOP) && !result) {
  895. bool block;
  896. timediff_t left = 1000;
  897. struct curltime now = Curl_now();
  898. result = wssh_statemach_act(data, &block);
  899. if(result)
  900. break;
  901. if(!disconnect) {
  902. if(Curl_pgrsUpdate(data))
  903. return CURLE_ABORTED_BY_CALLBACK;
  904. result = Curl_speedcheck(data, now);
  905. if(result)
  906. break;
  907. left = Curl_timeleft(data, NULL, FALSE);
  908. if(left < 0) {
  909. failf(data, "Operation timed out");
  910. return CURLE_OPERATION_TIMEDOUT;
  911. }
  912. }
  913. if(!result) {
  914. int dir = conn->waitfor;
  915. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  916. curl_socket_t fd_read = CURL_SOCKET_BAD;
  917. curl_socket_t fd_write = CURL_SOCKET_BAD;
  918. if(dir == KEEP_RECV)
  919. fd_read = sock;
  920. else if(dir == KEEP_SEND)
  921. fd_write = sock;
  922. /* wait for the socket to become ready */
  923. (void)Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write,
  924. left > 1000 ? 1000 : left); /* ignore result */
  925. }
  926. }
  927. return result;
  928. }
  929. /* generic done function for both SCP and SFTP called from their specific
  930. done functions */
  931. static CURLcode wssh_done(struct Curl_easy *data, CURLcode status)
  932. {
  933. CURLcode result = CURLE_OK;
  934. struct SSHPROTO *sftp_scp = data->req.p.ssh;
  935. if(!status) {
  936. /* run the state-machine */
  937. result = wssh_block_statemach(data, FALSE);
  938. }
  939. else
  940. result = status;
  941. if(sftp_scp)
  942. Curl_safefree(sftp_scp->path);
  943. if(Curl_pgrsDone(data))
  944. return CURLE_ABORTED_BY_CALLBACK;
  945. data->req.keepon = 0; /* clear all bits */
  946. return result;
  947. }
  948. #if 0
  949. static CURLcode wscp_done(struct Curl_easy *data,
  950. CURLcode code, bool premature)
  951. {
  952. CURLcode result = CURLE_OK;
  953. (void)conn;
  954. (void)code;
  955. (void)premature;
  956. return result;
  957. }
  958. static CURLcode wscp_doing(struct Curl_easy *data,
  959. bool *dophase_done)
  960. {
  961. CURLcode result = CURLE_OK;
  962. (void)conn;
  963. (void)dophase_done;
  964. return result;
  965. }
  966. static CURLcode wscp_disconnect(struct Curl_easy *data,
  967. struct connectdata *conn, bool dead_connection)
  968. {
  969. CURLcode result = CURLE_OK;
  970. (void)data;
  971. (void)conn;
  972. (void)dead_connection;
  973. return result;
  974. }
  975. #endif
  976. static CURLcode wsftp_done(struct Curl_easy *data,
  977. CURLcode code, bool premature)
  978. {
  979. (void)premature;
  980. state(data, SSH_SFTP_CLOSE);
  981. return wssh_done(data, code);
  982. }
  983. static CURLcode wsftp_doing(struct Curl_easy *data,
  984. bool *dophase_done)
  985. {
  986. CURLcode result = wssh_multi_statemach(data, dophase_done);
  987. if(*dophase_done) {
  988. DEBUGF(infof(data, "DO phase is complete"));
  989. }
  990. return result;
  991. }
  992. static CURLcode wsftp_disconnect(struct Curl_easy *data,
  993. struct connectdata *conn,
  994. bool dead)
  995. {
  996. CURLcode result = CURLE_OK;
  997. (void)dead;
  998. DEBUGF(infof(data, "SSH DISCONNECT starts now"));
  999. if(conn->proto.sshc.ssh_session) {
  1000. /* only if there is a session still around to use! */
  1001. state(data, SSH_SFTP_SHUTDOWN);
  1002. result = wssh_block_statemach(data, TRUE);
  1003. }
  1004. DEBUGF(infof(data, "SSH DISCONNECT is done"));
  1005. return result;
  1006. }
  1007. static int wssh_getsock(struct Curl_easy *data,
  1008. struct connectdata *conn,
  1009. curl_socket_t *sock)
  1010. {
  1011. int bitmap = GETSOCK_BLANK;
  1012. int dir = conn->waitfor;
  1013. (void)data;
  1014. sock[0] = conn->sock[FIRSTSOCKET];
  1015. if(dir == KEEP_RECV)
  1016. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  1017. else if(dir == KEEP_SEND)
  1018. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  1019. return bitmap;
  1020. }
  1021. void Curl_ssh_version(char *buffer, size_t buflen)
  1022. {
  1023. (void)msnprintf(buffer, buflen, "wolfssh/%s", LIBWOLFSSH_VERSION_STRING);
  1024. }
  1025. CURLcode Curl_ssh_init(void)
  1026. {
  1027. if(WS_SUCCESS != wolfSSH_Init()) {
  1028. DEBUGF(fprintf(stderr, "Error: wolfSSH_Init failed\n"));
  1029. return CURLE_FAILED_INIT;
  1030. }
  1031. return CURLE_OK;
  1032. }
  1033. void Curl_ssh_cleanup(void)
  1034. {
  1035. (void)wolfSSH_Cleanup();
  1036. }
  1037. #endif /* USE_WOLFSSH */