wolfssh.c 34 KB

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