wolfssh.c 33 KB

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