wolfssh.c 34 KB

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