wolfssh.c 34 KB

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