2
0

libssh.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Red Hat, Inc.
  9. *
  10. * Authors: Nikos Mavrogiannopoulos, Tomas Mraz, Stanislav Zidek,
  11. * Robert Kolcun, Andreas Schneider
  12. *
  13. * This software is licensed as described in the file COPYING, which
  14. * you should have received as part of this distribution. The terms
  15. * are also available at https://curl.se/docs/copyright.html.
  16. *
  17. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  18. * copies of the Software, and permit persons to whom the Software is
  19. * furnished to do so, under the terms of the COPYING file.
  20. *
  21. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  22. * KIND, either express or implied.
  23. *
  24. * SPDX-License-Identifier: curl
  25. *
  26. ***************************************************************************/
  27. #include "curl_setup.h"
  28. #ifdef USE_LIBSSH
  29. #include <limits.h>
  30. #include <libssh/libssh.h>
  31. #include <libssh/sftp.h>
  32. #ifdef HAVE_NETINET_IN_H
  33. #include <netinet/in.h>
  34. #endif
  35. #ifdef HAVE_ARPA_INET_H
  36. #include <arpa/inet.h>
  37. #endif
  38. #ifdef HAVE_UTSNAME_H
  39. #include <sys/utsname.h>
  40. #endif
  41. #ifdef HAVE_NETDB_H
  42. #include <netdb.h>
  43. #endif
  44. #ifdef __VMS
  45. #include <in.h>
  46. #include <inet.h>
  47. #endif
  48. #include <curl/curl.h>
  49. #include "urldata.h"
  50. #include "sendf.h"
  51. #include "hostip.h"
  52. #include "progress.h"
  53. #include "transfer.h"
  54. #include "escape.h"
  55. #include "http.h" /* for HTTP proxy tunnel stuff */
  56. #include "ssh.h"
  57. #include "url.h"
  58. #include "speedcheck.h"
  59. #include "getinfo.h"
  60. #include "strdup.h"
  61. #include "strcase.h"
  62. #include "vtls/vtls.h"
  63. #include "cfilters.h"
  64. #include "connect.h"
  65. #include "inet_ntop.h"
  66. #include "parsedate.h" /* for the week day and month names */
  67. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  68. #include "strtoofft.h"
  69. #include "multiif.h"
  70. #include "select.h"
  71. #include "warnless.h"
  72. #include "curl_path.h"
  73. #ifdef HAVE_SYS_STAT_H
  74. #include <sys/stat.h>
  75. #endif
  76. #ifdef HAVE_UNISTD_H
  77. #include <unistd.h>
  78. #endif
  79. #ifdef HAVE_FCNTL_H
  80. #include <fcntl.h>
  81. #endif
  82. /* The last 3 #include files should be in this order */
  83. #include "curl_printf.h"
  84. #include "curl_memory.h"
  85. #include "memdebug.h"
  86. /* in 0.10.0 or later, ignore deprecated warnings */
  87. #if defined(__GNUC__) && \
  88. (LIBSSH_VERSION_MINOR >= 10) || \
  89. (LIBSSH_VERSION_MAJOR > 0)
  90. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  91. #endif
  92. /* A recent macro provided by libssh. Or make our own. */
  93. #ifndef SSH_STRING_FREE_CHAR
  94. #define SSH_STRING_FREE_CHAR(x) \
  95. do { \
  96. if(x) { \
  97. ssh_string_free_char(x); \
  98. x = NULL; \
  99. } \
  100. } while(0)
  101. #endif
  102. /* These stat values may not be the same as the user's S_IFMT / S_IFLNK */
  103. #ifndef SSH_S_IFMT
  104. #define SSH_S_IFMT 00170000
  105. #endif
  106. #ifndef SSH_S_IFLNK
  107. #define SSH_S_IFLNK 0120000
  108. #endif
  109. /* Local functions: */
  110. static CURLcode myssh_connect(struct Curl_easy *data, bool *done);
  111. static CURLcode myssh_multi_statemach(struct Curl_easy *data,
  112. bool *done);
  113. static CURLcode myssh_do_it(struct Curl_easy *data, bool *done);
  114. static CURLcode scp_done(struct Curl_easy *data,
  115. CURLcode, bool premature);
  116. static CURLcode scp_doing(struct Curl_easy *data, bool *dophase_done);
  117. static CURLcode scp_disconnect(struct Curl_easy *data,
  118. struct connectdata *conn,
  119. bool dead_connection);
  120. static CURLcode sftp_done(struct Curl_easy *data,
  121. CURLcode, bool premature);
  122. static CURLcode sftp_doing(struct Curl_easy *data,
  123. bool *dophase_done);
  124. static CURLcode sftp_disconnect(struct Curl_easy *data,
  125. struct connectdata *conn,
  126. bool dead);
  127. static
  128. CURLcode sftp_perform(struct Curl_easy *data,
  129. bool *connected,
  130. bool *dophase_done);
  131. static void sftp_quote(struct Curl_easy *data);
  132. static void sftp_quote_stat(struct Curl_easy *data);
  133. static int myssh_getsock(struct Curl_easy *data,
  134. struct connectdata *conn, curl_socket_t *sock);
  135. static CURLcode myssh_setup_connection(struct Curl_easy *data,
  136. struct connectdata *conn);
  137. /*
  138. * SCP protocol handler.
  139. */
  140. const struct Curl_handler Curl_handler_scp = {
  141. "SCP", /* scheme */
  142. myssh_setup_connection, /* setup_connection */
  143. myssh_do_it, /* do_it */
  144. scp_done, /* done */
  145. ZERO_NULL, /* do_more */
  146. myssh_connect, /* connect_it */
  147. myssh_multi_statemach, /* connecting */
  148. scp_doing, /* doing */
  149. myssh_getsock, /* proto_getsock */
  150. myssh_getsock, /* doing_getsock */
  151. ZERO_NULL, /* domore_getsock */
  152. myssh_getsock, /* perform_getsock */
  153. scp_disconnect, /* disconnect */
  154. ZERO_NULL, /* readwrite */
  155. ZERO_NULL, /* connection_check */
  156. ZERO_NULL, /* attach connection */
  157. PORT_SSH, /* defport */
  158. CURLPROTO_SCP, /* protocol */
  159. CURLPROTO_SCP, /* family */
  160. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY /* flags */
  161. };
  162. /*
  163. * SFTP protocol handler.
  164. */
  165. const struct Curl_handler Curl_handler_sftp = {
  166. "SFTP", /* scheme */
  167. myssh_setup_connection, /* setup_connection */
  168. myssh_do_it, /* do_it */
  169. sftp_done, /* done */
  170. ZERO_NULL, /* do_more */
  171. myssh_connect, /* connect_it */
  172. myssh_multi_statemach, /* connecting */
  173. sftp_doing, /* doing */
  174. myssh_getsock, /* proto_getsock */
  175. myssh_getsock, /* doing_getsock */
  176. ZERO_NULL, /* domore_getsock */
  177. myssh_getsock, /* perform_getsock */
  178. sftp_disconnect, /* disconnect */
  179. ZERO_NULL, /* readwrite */
  180. ZERO_NULL, /* connection_check */
  181. ZERO_NULL, /* attach connection */
  182. PORT_SSH, /* defport */
  183. CURLPROTO_SFTP, /* protocol */
  184. CURLPROTO_SFTP, /* family */
  185. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
  186. | PROTOPT_NOURLQUERY /* flags */
  187. };
  188. static CURLcode sftp_error_to_CURLE(int err)
  189. {
  190. switch(err) {
  191. case SSH_FX_OK:
  192. return CURLE_OK;
  193. case SSH_FX_NO_SUCH_FILE:
  194. case SSH_FX_NO_SUCH_PATH:
  195. return CURLE_REMOTE_FILE_NOT_FOUND;
  196. case SSH_FX_PERMISSION_DENIED:
  197. case SSH_FX_WRITE_PROTECT:
  198. return CURLE_REMOTE_ACCESS_DENIED;
  199. case SSH_FX_FILE_ALREADY_EXISTS:
  200. return CURLE_REMOTE_FILE_EXISTS;
  201. default:
  202. break;
  203. }
  204. return CURLE_SSH;
  205. }
  206. #ifndef DEBUGBUILD
  207. #define state(x,y) mystate(x,y)
  208. #else
  209. #define state(x,y) mystate(x,y, __LINE__)
  210. #endif
  211. /*
  212. * SSH State machine related code
  213. */
  214. /* This is the ONLY way to change SSH state! */
  215. static void mystate(struct Curl_easy *data, sshstate nowstate
  216. #ifdef DEBUGBUILD
  217. , int lineno
  218. #endif
  219. )
  220. {
  221. struct connectdata *conn = data->conn;
  222. struct ssh_conn *sshc = &conn->proto.sshc;
  223. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  224. /* for debug purposes */
  225. static const char *const names[] = {
  226. "SSH_STOP",
  227. "SSH_INIT",
  228. "SSH_S_STARTUP",
  229. "SSH_HOSTKEY",
  230. "SSH_AUTHLIST",
  231. "SSH_AUTH_PKEY_INIT",
  232. "SSH_AUTH_PKEY",
  233. "SSH_AUTH_PASS_INIT",
  234. "SSH_AUTH_PASS",
  235. "SSH_AUTH_AGENT_INIT",
  236. "SSH_AUTH_AGENT_LIST",
  237. "SSH_AUTH_AGENT",
  238. "SSH_AUTH_HOST_INIT",
  239. "SSH_AUTH_HOST",
  240. "SSH_AUTH_KEY_INIT",
  241. "SSH_AUTH_KEY",
  242. "SSH_AUTH_GSSAPI",
  243. "SSH_AUTH_DONE",
  244. "SSH_SFTP_INIT",
  245. "SSH_SFTP_REALPATH",
  246. "SSH_SFTP_QUOTE_INIT",
  247. "SSH_SFTP_POSTQUOTE_INIT",
  248. "SSH_SFTP_QUOTE",
  249. "SSH_SFTP_NEXT_QUOTE",
  250. "SSH_SFTP_QUOTE_STAT",
  251. "SSH_SFTP_QUOTE_SETSTAT",
  252. "SSH_SFTP_QUOTE_SYMLINK",
  253. "SSH_SFTP_QUOTE_MKDIR",
  254. "SSH_SFTP_QUOTE_RENAME",
  255. "SSH_SFTP_QUOTE_RMDIR",
  256. "SSH_SFTP_QUOTE_UNLINK",
  257. "SSH_SFTP_QUOTE_STATVFS",
  258. "SSH_SFTP_GETINFO",
  259. "SSH_SFTP_FILETIME",
  260. "SSH_SFTP_TRANS_INIT",
  261. "SSH_SFTP_UPLOAD_INIT",
  262. "SSH_SFTP_CREATE_DIRS_INIT",
  263. "SSH_SFTP_CREATE_DIRS",
  264. "SSH_SFTP_CREATE_DIRS_MKDIR",
  265. "SSH_SFTP_READDIR_INIT",
  266. "SSH_SFTP_READDIR",
  267. "SSH_SFTP_READDIR_LINK",
  268. "SSH_SFTP_READDIR_BOTTOM",
  269. "SSH_SFTP_READDIR_DONE",
  270. "SSH_SFTP_DOWNLOAD_INIT",
  271. "SSH_SFTP_DOWNLOAD_STAT",
  272. "SSH_SFTP_CLOSE",
  273. "SSH_SFTP_SHUTDOWN",
  274. "SSH_SCP_TRANS_INIT",
  275. "SSH_SCP_UPLOAD_INIT",
  276. "SSH_SCP_DOWNLOAD_INIT",
  277. "SSH_SCP_DOWNLOAD",
  278. "SSH_SCP_DONE",
  279. "SSH_SCP_SEND_EOF",
  280. "SSH_SCP_WAIT_EOF",
  281. "SSH_SCP_WAIT_CLOSE",
  282. "SSH_SCP_CHANNEL_FREE",
  283. "SSH_SESSION_DISCONNECT",
  284. "SSH_SESSION_FREE",
  285. "QUIT"
  286. };
  287. if(sshc->state != nowstate) {
  288. infof(data, "SSH %p state change from %s to %s (line %d)",
  289. (void *) sshc, names[sshc->state], names[nowstate],
  290. lineno);
  291. }
  292. #endif
  293. sshc->state = nowstate;
  294. }
  295. /* Multiple options:
  296. * 1. data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5] is set with an MD5
  297. * hash (90s style auth, not sure we should have it here)
  298. * 2. data->set.ssh_keyfunc callback is set. Then we do trust on first
  299. * use. We even save on knownhosts if CURLKHSTAT_FINE_ADD_TO_FILE
  300. * is returned by it.
  301. * 3. none of the above. We only accept if it is present on known hosts.
  302. *
  303. * Returns SSH_OK or SSH_ERROR.
  304. */
  305. static int myssh_is_known(struct Curl_easy *data)
  306. {
  307. int rc;
  308. struct connectdata *conn = data->conn;
  309. struct ssh_conn *sshc = &conn->proto.sshc;
  310. ssh_key pubkey;
  311. size_t hlen;
  312. unsigned char *hash = NULL;
  313. char *found_base64 = NULL;
  314. char *known_base64 = NULL;
  315. int vstate;
  316. enum curl_khmatch keymatch;
  317. struct curl_khkey foundkey;
  318. struct curl_khkey *knownkeyp = NULL;
  319. curl_sshkeycallback func =
  320. data->set.ssh_keyfunc;
  321. #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,9,0)
  322. struct ssh_knownhosts_entry *knownhostsentry = NULL;
  323. struct curl_khkey knownkey;
  324. #endif
  325. #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,8,0)
  326. rc = ssh_get_server_publickey(sshc->ssh_session, &pubkey);
  327. #else
  328. rc = ssh_get_publickey(sshc->ssh_session, &pubkey);
  329. #endif
  330. if(rc != SSH_OK)
  331. return rc;
  332. if(data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]) {
  333. int i;
  334. char md5buffer[33];
  335. const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5];
  336. rc = ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_MD5,
  337. &hash, &hlen);
  338. if(rc != SSH_OK || hlen != 16) {
  339. failf(data,
  340. "Denied establishing ssh session: md5 fingerprint not available");
  341. goto cleanup;
  342. }
  343. for(i = 0; i < 16; i++)
  344. msnprintf(&md5buffer[i*2], 3, "%02x", (unsigned char)hash[i]);
  345. infof(data, "SSH MD5 fingerprint: %s", md5buffer);
  346. if(!strcasecompare(md5buffer, pubkey_md5)) {
  347. failf(data,
  348. "Denied establishing ssh session: mismatch md5 fingerprint. "
  349. "Remote %s is not equal to %s", md5buffer, pubkey_md5);
  350. rc = SSH_ERROR;
  351. goto cleanup;
  352. }
  353. rc = SSH_OK;
  354. goto cleanup;
  355. }
  356. if(data->set.ssl.primary.verifyhost != TRUE) {
  357. rc = SSH_OK;
  358. goto cleanup;
  359. }
  360. #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,9,0)
  361. /* Get the known_key from the known hosts file */
  362. vstate = ssh_session_get_known_hosts_entry(sshc->ssh_session,
  363. &knownhostsentry);
  364. /* Case an entry was found in a known hosts file */
  365. if(knownhostsentry) {
  366. if(knownhostsentry->publickey) {
  367. rc = ssh_pki_export_pubkey_base64(knownhostsentry->publickey,
  368. &known_base64);
  369. if(rc != SSH_OK) {
  370. goto cleanup;
  371. }
  372. knownkey.key = known_base64;
  373. knownkey.len = strlen(known_base64);
  374. switch(ssh_key_type(knownhostsentry->publickey)) {
  375. case SSH_KEYTYPE_RSA:
  376. knownkey.keytype = CURLKHTYPE_RSA;
  377. break;
  378. case SSH_KEYTYPE_RSA1:
  379. knownkey.keytype = CURLKHTYPE_RSA1;
  380. break;
  381. case SSH_KEYTYPE_ECDSA:
  382. case SSH_KEYTYPE_ECDSA_P256:
  383. case SSH_KEYTYPE_ECDSA_P384:
  384. case SSH_KEYTYPE_ECDSA_P521:
  385. knownkey.keytype = CURLKHTYPE_ECDSA;
  386. break;
  387. case SSH_KEYTYPE_ED25519:
  388. knownkey.keytype = CURLKHTYPE_ED25519;
  389. break;
  390. case SSH_KEYTYPE_DSS:
  391. knownkey.keytype = CURLKHTYPE_DSS;
  392. break;
  393. default:
  394. rc = SSH_ERROR;
  395. goto cleanup;
  396. }
  397. knownkeyp = &knownkey;
  398. }
  399. }
  400. switch(vstate) {
  401. case SSH_KNOWN_HOSTS_OK:
  402. keymatch = CURLKHMATCH_OK;
  403. break;
  404. case SSH_KNOWN_HOSTS_OTHER:
  405. /* fallthrough */
  406. case SSH_KNOWN_HOSTS_NOT_FOUND:
  407. /* fallthrough */
  408. case SSH_KNOWN_HOSTS_UNKNOWN:
  409. /* fallthrough */
  410. case SSH_KNOWN_HOSTS_ERROR:
  411. keymatch = CURLKHMATCH_MISSING;
  412. break;
  413. default:
  414. keymatch = CURLKHMATCH_MISMATCH;
  415. break;
  416. }
  417. #else
  418. vstate = ssh_is_server_known(sshc->ssh_session);
  419. switch(vstate) {
  420. case SSH_SERVER_KNOWN_OK:
  421. keymatch = CURLKHMATCH_OK;
  422. break;
  423. case SSH_SERVER_FILE_NOT_FOUND:
  424. /* fallthrough */
  425. case SSH_SERVER_NOT_KNOWN:
  426. keymatch = CURLKHMATCH_MISSING;
  427. break;
  428. default:
  429. keymatch = CURLKHMATCH_MISMATCH;
  430. break;
  431. }
  432. #endif
  433. if(func) { /* use callback to determine action */
  434. rc = ssh_pki_export_pubkey_base64(pubkey, &found_base64);
  435. if(rc != SSH_OK)
  436. goto cleanup;
  437. foundkey.key = found_base64;
  438. foundkey.len = strlen(found_base64);
  439. switch(ssh_key_type(pubkey)) {
  440. case SSH_KEYTYPE_RSA:
  441. foundkey.keytype = CURLKHTYPE_RSA;
  442. break;
  443. case SSH_KEYTYPE_RSA1:
  444. foundkey.keytype = CURLKHTYPE_RSA1;
  445. break;
  446. case SSH_KEYTYPE_ECDSA:
  447. #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,9,0)
  448. case SSH_KEYTYPE_ECDSA_P256:
  449. case SSH_KEYTYPE_ECDSA_P384:
  450. case SSH_KEYTYPE_ECDSA_P521:
  451. #endif
  452. foundkey.keytype = CURLKHTYPE_ECDSA;
  453. break;
  454. #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,7,0)
  455. case SSH_KEYTYPE_ED25519:
  456. foundkey.keytype = CURLKHTYPE_ED25519;
  457. break;
  458. #endif
  459. case SSH_KEYTYPE_DSS:
  460. foundkey.keytype = CURLKHTYPE_DSS;
  461. break;
  462. default:
  463. rc = SSH_ERROR;
  464. goto cleanup;
  465. }
  466. Curl_set_in_callback(data, true);
  467. rc = func(data, knownkeyp, /* from the knownhosts file */
  468. &foundkey, /* from the remote host */
  469. keymatch, data->set.ssh_keyfunc_userp);
  470. Curl_set_in_callback(data, false);
  471. switch(rc) {
  472. case CURLKHSTAT_FINE_ADD_TO_FILE:
  473. #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,8,0)
  474. rc = ssh_session_update_known_hosts(sshc->ssh_session);
  475. #else
  476. rc = ssh_write_knownhost(sshc->ssh_session);
  477. #endif
  478. if(rc != SSH_OK) {
  479. goto cleanup;
  480. }
  481. break;
  482. case CURLKHSTAT_FINE:
  483. break;
  484. default: /* REJECT/DEFER */
  485. rc = SSH_ERROR;
  486. goto cleanup;
  487. }
  488. }
  489. else {
  490. if(keymatch != CURLKHMATCH_OK) {
  491. rc = SSH_ERROR;
  492. goto cleanup;
  493. }
  494. }
  495. rc = SSH_OK;
  496. cleanup:
  497. if(found_base64) {
  498. (free)(found_base64);
  499. }
  500. if(known_base64) {
  501. (free)(known_base64);
  502. }
  503. if(hash)
  504. ssh_clean_pubkey_hash(&hash);
  505. ssh_key_free(pubkey);
  506. #if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0,9,0)
  507. if(knownhostsentry) {
  508. ssh_knownhosts_entry_free(knownhostsentry);
  509. }
  510. #endif
  511. return rc;
  512. }
  513. #define MOVE_TO_ERROR_STATE(_r) do { \
  514. state(data, SSH_SESSION_DISCONNECT); \
  515. sshc->actualcode = _r; \
  516. rc = SSH_ERROR; \
  517. } while(0)
  518. #define MOVE_TO_SFTP_CLOSE_STATE() do { \
  519. state(data, SSH_SFTP_CLOSE); \
  520. sshc->actualcode = \
  521. sftp_error_to_CURLE(sftp_get_error(sshc->sftp_session)); \
  522. rc = SSH_ERROR; \
  523. } while(0)
  524. #define MOVE_TO_PASSWD_AUTH do { \
  525. if(sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD) { \
  526. rc = SSH_OK; \
  527. state(data, SSH_AUTH_PASS_INIT); \
  528. } \
  529. else { \
  530. MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED); \
  531. } \
  532. } while(0)
  533. #define MOVE_TO_KEY_AUTH do { \
  534. if(sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE) { \
  535. rc = SSH_OK; \
  536. state(data, SSH_AUTH_KEY_INIT); \
  537. } \
  538. else { \
  539. MOVE_TO_PASSWD_AUTH; \
  540. } \
  541. } while(0)
  542. #define MOVE_TO_GSSAPI_AUTH do { \
  543. if(sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC) { \
  544. rc = SSH_OK; \
  545. state(data, SSH_AUTH_GSSAPI); \
  546. } \
  547. else { \
  548. MOVE_TO_KEY_AUTH; \
  549. } \
  550. } while(0)
  551. static
  552. int myssh_auth_interactive(struct connectdata *conn)
  553. {
  554. int rc;
  555. struct ssh_conn *sshc = &conn->proto.sshc;
  556. int nprompts;
  557. restart:
  558. switch(sshc->kbd_state) {
  559. case 0:
  560. rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
  561. if(rc == SSH_AUTH_AGAIN)
  562. return SSH_AGAIN;
  563. if(rc != SSH_AUTH_INFO)
  564. return SSH_ERROR;
  565. nprompts = ssh_userauth_kbdint_getnprompts(sshc->ssh_session);
  566. if(nprompts != 1)
  567. return SSH_ERROR;
  568. rc = ssh_userauth_kbdint_setanswer(sshc->ssh_session, 0, conn->passwd);
  569. if(rc < 0)
  570. return SSH_ERROR;
  571. /* FALLTHROUGH */
  572. case 1:
  573. sshc->kbd_state = 1;
  574. rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
  575. if(rc == SSH_AUTH_AGAIN)
  576. return SSH_AGAIN;
  577. else if(rc == SSH_AUTH_SUCCESS)
  578. rc = SSH_OK;
  579. else if(rc == SSH_AUTH_INFO) {
  580. nprompts = ssh_userauth_kbdint_getnprompts(sshc->ssh_session);
  581. if(nprompts)
  582. return SSH_ERROR;
  583. sshc->kbd_state = 2;
  584. goto restart;
  585. }
  586. else
  587. rc = SSH_ERROR;
  588. break;
  589. case 2:
  590. sshc->kbd_state = 2;
  591. rc = ssh_userauth_kbdint(sshc->ssh_session, NULL, NULL);
  592. if(rc == SSH_AUTH_AGAIN)
  593. return SSH_AGAIN;
  594. else if(rc == SSH_AUTH_SUCCESS)
  595. rc = SSH_OK;
  596. else
  597. rc = SSH_ERROR;
  598. break;
  599. default:
  600. return SSH_ERROR;
  601. }
  602. sshc->kbd_state = 0;
  603. return rc;
  604. }
  605. /*
  606. * ssh_statemach_act() runs the SSH state machine as far as it can without
  607. * blocking and without reaching the end. The data the pointer 'block' points
  608. * to will be set to TRUE if the libssh function returns SSH_AGAIN
  609. * meaning it wants to be called again when the socket is ready
  610. */
  611. static CURLcode myssh_statemach_act(struct Curl_easy *data, bool *block)
  612. {
  613. CURLcode result = CURLE_OK;
  614. struct connectdata *conn = data->conn;
  615. struct SSHPROTO *protop = data->req.p.ssh;
  616. struct ssh_conn *sshc = &conn->proto.sshc;
  617. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  618. int rc = SSH_NO_ERROR, err;
  619. int seekerr = CURL_SEEKFUNC_OK;
  620. const char *err_msg;
  621. *block = 0; /* we're not blocking by default */
  622. do {
  623. switch(sshc->state) {
  624. case SSH_INIT:
  625. sshc->secondCreateDirs = 0;
  626. sshc->nextstate = SSH_NO_STATE;
  627. sshc->actualcode = CURLE_OK;
  628. #if 0
  629. ssh_set_log_level(SSH_LOG_PROTOCOL);
  630. #endif
  631. /* Set libssh to non-blocking, since everything internally is
  632. non-blocking */
  633. ssh_set_blocking(sshc->ssh_session, 0);
  634. state(data, SSH_S_STARTUP);
  635. /* FALLTHROUGH */
  636. case SSH_S_STARTUP:
  637. rc = ssh_connect(sshc->ssh_session);
  638. if(rc == SSH_AGAIN)
  639. break;
  640. if(rc != SSH_OK) {
  641. failf(data, "Failure establishing ssh session");
  642. MOVE_TO_ERROR_STATE(CURLE_FAILED_INIT);
  643. break;
  644. }
  645. state(data, SSH_HOSTKEY);
  646. /* FALLTHROUGH */
  647. case SSH_HOSTKEY:
  648. rc = myssh_is_known(data);
  649. if(rc != SSH_OK) {
  650. MOVE_TO_ERROR_STATE(CURLE_PEER_FAILED_VERIFICATION);
  651. break;
  652. }
  653. state(data, SSH_AUTHLIST);
  654. /* FALLTHROUGH */
  655. case SSH_AUTHLIST:{
  656. sshc->authed = FALSE;
  657. rc = ssh_userauth_none(sshc->ssh_session, NULL);
  658. if(rc == SSH_AUTH_AGAIN) {
  659. rc = SSH_AGAIN;
  660. break;
  661. }
  662. if(rc == SSH_AUTH_SUCCESS) {
  663. sshc->authed = TRUE;
  664. infof(data, "Authenticated with none");
  665. state(data, SSH_AUTH_DONE);
  666. break;
  667. }
  668. else if(rc == SSH_AUTH_ERROR) {
  669. MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
  670. break;
  671. }
  672. sshc->auth_methods = ssh_userauth_list(sshc->ssh_session, NULL);
  673. if(sshc->auth_methods)
  674. infof(data, "SSH authentication methods available: %s%s%s%s",
  675. sshc->auth_methods & SSH_AUTH_METHOD_PUBLICKEY ?
  676. "public key, ": "",
  677. sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC ?
  678. "GSSAPI, " : "",
  679. sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE ?
  680. "keyboard-interactive, " : "",
  681. sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD ?
  682. "password": "");
  683. if(sshc->auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
  684. state(data, SSH_AUTH_PKEY_INIT);
  685. infof(data, "Authentication using SSH public key file");
  686. }
  687. else if(sshc->auth_methods & SSH_AUTH_METHOD_GSSAPI_MIC) {
  688. state(data, SSH_AUTH_GSSAPI);
  689. }
  690. else if(sshc->auth_methods & SSH_AUTH_METHOD_INTERACTIVE) {
  691. state(data, SSH_AUTH_KEY_INIT);
  692. }
  693. else if(sshc->auth_methods & SSH_AUTH_METHOD_PASSWORD) {
  694. state(data, SSH_AUTH_PASS_INIT);
  695. }
  696. else { /* unsupported authentication method */
  697. MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
  698. break;
  699. }
  700. break;
  701. }
  702. case SSH_AUTH_PKEY_INIT:
  703. if(!(data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY)) {
  704. MOVE_TO_GSSAPI_AUTH;
  705. break;
  706. }
  707. /* Two choices, (1) private key was given on CMD,
  708. * (2) use the "default" keys. */
  709. if(data->set.str[STRING_SSH_PRIVATE_KEY]) {
  710. if(sshc->pubkey && !data->set.ssl.key_passwd) {
  711. rc = ssh_userauth_try_publickey(sshc->ssh_session, NULL,
  712. sshc->pubkey);
  713. if(rc == SSH_AUTH_AGAIN) {
  714. rc = SSH_AGAIN;
  715. break;
  716. }
  717. if(rc != SSH_OK) {
  718. MOVE_TO_GSSAPI_AUTH;
  719. break;
  720. }
  721. }
  722. rc = ssh_pki_import_privkey_file(data->
  723. set.str[STRING_SSH_PRIVATE_KEY],
  724. data->set.ssl.key_passwd, NULL,
  725. NULL, &sshc->privkey);
  726. if(rc != SSH_OK) {
  727. failf(data, "Could not load private key file %s",
  728. data->set.str[STRING_SSH_PRIVATE_KEY]);
  729. MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
  730. break;
  731. }
  732. state(data, SSH_AUTH_PKEY);
  733. break;
  734. }
  735. else {
  736. rc = ssh_userauth_publickey_auto(sshc->ssh_session, NULL,
  737. data->set.ssl.key_passwd);
  738. if(rc == SSH_AUTH_AGAIN) {
  739. rc = SSH_AGAIN;
  740. break;
  741. }
  742. if(rc == SSH_AUTH_SUCCESS) {
  743. rc = SSH_OK;
  744. sshc->authed = TRUE;
  745. infof(data, "Completed public key authentication");
  746. state(data, SSH_AUTH_DONE);
  747. break;
  748. }
  749. MOVE_TO_GSSAPI_AUTH;
  750. }
  751. break;
  752. case SSH_AUTH_PKEY:
  753. rc = ssh_userauth_publickey(sshc->ssh_session, NULL, sshc->privkey);
  754. if(rc == SSH_AUTH_AGAIN) {
  755. rc = SSH_AGAIN;
  756. break;
  757. }
  758. if(rc == SSH_AUTH_SUCCESS) {
  759. sshc->authed = TRUE;
  760. infof(data, "Completed public key authentication");
  761. state(data, SSH_AUTH_DONE);
  762. break;
  763. }
  764. else {
  765. infof(data, "Failed public key authentication (rc: %d)", rc);
  766. MOVE_TO_GSSAPI_AUTH;
  767. }
  768. break;
  769. case SSH_AUTH_GSSAPI:
  770. if(!(data->set.ssh_auth_types & CURLSSH_AUTH_GSSAPI)) {
  771. MOVE_TO_KEY_AUTH;
  772. break;
  773. }
  774. rc = ssh_userauth_gssapi(sshc->ssh_session);
  775. if(rc == SSH_AUTH_AGAIN) {
  776. rc = SSH_AGAIN;
  777. break;
  778. }
  779. if(rc == SSH_AUTH_SUCCESS) {
  780. rc = SSH_OK;
  781. sshc->authed = TRUE;
  782. infof(data, "Completed gssapi authentication");
  783. state(data, SSH_AUTH_DONE);
  784. break;
  785. }
  786. MOVE_TO_KEY_AUTH;
  787. break;
  788. case SSH_AUTH_KEY_INIT:
  789. if(data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD) {
  790. state(data, SSH_AUTH_KEY);
  791. }
  792. else {
  793. MOVE_TO_PASSWD_AUTH;
  794. }
  795. break;
  796. case SSH_AUTH_KEY:
  797. /* keyboard-interactive authentication */
  798. rc = myssh_auth_interactive(conn);
  799. if(rc == SSH_AGAIN) {
  800. break;
  801. }
  802. if(rc == SSH_OK) {
  803. sshc->authed = TRUE;
  804. infof(data, "completed keyboard interactive authentication");
  805. state(data, SSH_AUTH_DONE);
  806. }
  807. else {
  808. MOVE_TO_PASSWD_AUTH;
  809. }
  810. break;
  811. case SSH_AUTH_PASS_INIT:
  812. if(!(data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD)) {
  813. MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
  814. break;
  815. }
  816. state(data, SSH_AUTH_PASS);
  817. /* FALLTHROUGH */
  818. case SSH_AUTH_PASS:
  819. rc = ssh_userauth_password(sshc->ssh_session, NULL, conn->passwd);
  820. if(rc == SSH_AUTH_AGAIN) {
  821. rc = SSH_AGAIN;
  822. break;
  823. }
  824. if(rc == SSH_AUTH_SUCCESS) {
  825. sshc->authed = TRUE;
  826. infof(data, "Completed password authentication");
  827. state(data, SSH_AUTH_DONE);
  828. }
  829. else {
  830. MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
  831. }
  832. break;
  833. case SSH_AUTH_DONE:
  834. if(!sshc->authed) {
  835. failf(data, "Authentication failure");
  836. MOVE_TO_ERROR_STATE(CURLE_LOGIN_DENIED);
  837. break;
  838. }
  839. /*
  840. * At this point we have an authenticated ssh session.
  841. */
  842. infof(data, "Authentication complete");
  843. Curl_pgrsTime(data, TIMER_APPCONNECT); /* SSH is connected */
  844. conn->sockfd = sock;
  845. conn->writesockfd = CURL_SOCKET_BAD;
  846. if(conn->handler->protocol == CURLPROTO_SFTP) {
  847. state(data, SSH_SFTP_INIT);
  848. break;
  849. }
  850. infof(data, "SSH CONNECT phase done");
  851. state(data, SSH_STOP);
  852. break;
  853. case SSH_SFTP_INIT:
  854. ssh_set_blocking(sshc->ssh_session, 1);
  855. sshc->sftp_session = sftp_new(sshc->ssh_session);
  856. if(!sshc->sftp_session) {
  857. failf(data, "Failure initializing sftp session: %s",
  858. ssh_get_error(sshc->ssh_session));
  859. MOVE_TO_ERROR_STATE(CURLE_COULDNT_CONNECT);
  860. break;
  861. }
  862. rc = sftp_init(sshc->sftp_session);
  863. if(rc != SSH_OK) {
  864. failf(data, "Failure initializing sftp session: %s",
  865. ssh_get_error(sshc->ssh_session));
  866. MOVE_TO_ERROR_STATE(sftp_error_to_CURLE(SSH_FX_FAILURE));
  867. break;
  868. }
  869. state(data, SSH_SFTP_REALPATH);
  870. /* FALLTHROUGH */
  871. case SSH_SFTP_REALPATH:
  872. /*
  873. * Get the "home" directory
  874. */
  875. sshc->homedir = sftp_canonicalize_path(sshc->sftp_session, ".");
  876. if(!sshc->homedir) {
  877. MOVE_TO_ERROR_STATE(CURLE_COULDNT_CONNECT);
  878. break;
  879. }
  880. data->state.most_recent_ftp_entrypath = sshc->homedir;
  881. /* This is the last step in the SFTP connect phase. Do note that while
  882. we get the homedir here, we get the "workingpath" in the DO action
  883. since the homedir will remain the same between request but the
  884. working path will not. */
  885. DEBUGF(infof(data, "SSH CONNECT phase done"));
  886. state(data, SSH_STOP);
  887. break;
  888. case SSH_SFTP_QUOTE_INIT:
  889. result = Curl_getworkingpath(data, sshc->homedir, &protop->path);
  890. if(result) {
  891. sshc->actualcode = result;
  892. state(data, SSH_STOP);
  893. break;
  894. }
  895. if(data->set.quote) {
  896. infof(data, "Sending quote commands");
  897. sshc->quote_item = data->set.quote;
  898. state(data, SSH_SFTP_QUOTE);
  899. }
  900. else {
  901. state(data, SSH_SFTP_GETINFO);
  902. }
  903. break;
  904. case SSH_SFTP_POSTQUOTE_INIT:
  905. if(data->set.postquote) {
  906. infof(data, "Sending quote commands");
  907. sshc->quote_item = data->set.postquote;
  908. state(data, SSH_SFTP_QUOTE);
  909. }
  910. else {
  911. state(data, SSH_STOP);
  912. }
  913. break;
  914. case SSH_SFTP_QUOTE:
  915. /* Send any quote commands */
  916. sftp_quote(data);
  917. break;
  918. case SSH_SFTP_NEXT_QUOTE:
  919. Curl_safefree(sshc->quote_path1);
  920. Curl_safefree(sshc->quote_path2);
  921. sshc->quote_item = sshc->quote_item->next;
  922. if(sshc->quote_item) {
  923. state(data, SSH_SFTP_QUOTE);
  924. }
  925. else {
  926. if(sshc->nextstate != SSH_NO_STATE) {
  927. state(data, sshc->nextstate);
  928. sshc->nextstate = SSH_NO_STATE;
  929. }
  930. else {
  931. state(data, SSH_SFTP_GETINFO);
  932. }
  933. }
  934. break;
  935. case SSH_SFTP_QUOTE_STAT:
  936. sftp_quote_stat(data);
  937. break;
  938. case SSH_SFTP_QUOTE_SETSTAT:
  939. rc = sftp_setstat(sshc->sftp_session, sshc->quote_path2,
  940. sshc->quote_attrs);
  941. if(rc && !sshc->acceptfail) {
  942. Curl_safefree(sshc->quote_path1);
  943. Curl_safefree(sshc->quote_path2);
  944. failf(data, "Attempt to set SFTP stats failed: %s",
  945. ssh_get_error(sshc->ssh_session));
  946. state(data, SSH_SFTP_CLOSE);
  947. sshc->nextstate = SSH_NO_STATE;
  948. sshc->actualcode = CURLE_QUOTE_ERROR;
  949. /* sshc->actualcode = sftp_error_to_CURLE(err);
  950. * we do not send the actual error; we return
  951. * the error the libssh2 backend is returning */
  952. break;
  953. }
  954. state(data, SSH_SFTP_NEXT_QUOTE);
  955. break;
  956. case SSH_SFTP_QUOTE_SYMLINK:
  957. rc = sftp_symlink(sshc->sftp_session, sshc->quote_path2,
  958. sshc->quote_path1);
  959. if(rc && !sshc->acceptfail) {
  960. Curl_safefree(sshc->quote_path1);
  961. Curl_safefree(sshc->quote_path2);
  962. failf(data, "symlink command failed: %s",
  963. ssh_get_error(sshc->ssh_session));
  964. state(data, SSH_SFTP_CLOSE);
  965. sshc->nextstate = SSH_NO_STATE;
  966. sshc->actualcode = CURLE_QUOTE_ERROR;
  967. break;
  968. }
  969. state(data, SSH_SFTP_NEXT_QUOTE);
  970. break;
  971. case SSH_SFTP_QUOTE_MKDIR:
  972. rc = sftp_mkdir(sshc->sftp_session, sshc->quote_path1,
  973. (mode_t)data->set.new_directory_perms);
  974. if(rc && !sshc->acceptfail) {
  975. Curl_safefree(sshc->quote_path1);
  976. failf(data, "mkdir command failed: %s",
  977. ssh_get_error(sshc->ssh_session));
  978. state(data, SSH_SFTP_CLOSE);
  979. sshc->nextstate = SSH_NO_STATE;
  980. sshc->actualcode = CURLE_QUOTE_ERROR;
  981. break;
  982. }
  983. state(data, SSH_SFTP_NEXT_QUOTE);
  984. break;
  985. case SSH_SFTP_QUOTE_RENAME:
  986. rc = sftp_rename(sshc->sftp_session, sshc->quote_path1,
  987. sshc->quote_path2);
  988. if(rc && !sshc->acceptfail) {
  989. Curl_safefree(sshc->quote_path1);
  990. Curl_safefree(sshc->quote_path2);
  991. failf(data, "rename command failed: %s",
  992. ssh_get_error(sshc->ssh_session));
  993. state(data, SSH_SFTP_CLOSE);
  994. sshc->nextstate = SSH_NO_STATE;
  995. sshc->actualcode = CURLE_QUOTE_ERROR;
  996. break;
  997. }
  998. state(data, SSH_SFTP_NEXT_QUOTE);
  999. break;
  1000. case SSH_SFTP_QUOTE_RMDIR:
  1001. rc = sftp_rmdir(sshc->sftp_session, sshc->quote_path1);
  1002. if(rc && !sshc->acceptfail) {
  1003. Curl_safefree(sshc->quote_path1);
  1004. failf(data, "rmdir command failed: %s",
  1005. ssh_get_error(sshc->ssh_session));
  1006. state(data, SSH_SFTP_CLOSE);
  1007. sshc->nextstate = SSH_NO_STATE;
  1008. sshc->actualcode = CURLE_QUOTE_ERROR;
  1009. break;
  1010. }
  1011. state(data, SSH_SFTP_NEXT_QUOTE);
  1012. break;
  1013. case SSH_SFTP_QUOTE_UNLINK:
  1014. rc = sftp_unlink(sshc->sftp_session, sshc->quote_path1);
  1015. if(rc && !sshc->acceptfail) {
  1016. Curl_safefree(sshc->quote_path1);
  1017. failf(data, "rm command failed: %s",
  1018. ssh_get_error(sshc->ssh_session));
  1019. state(data, SSH_SFTP_CLOSE);
  1020. sshc->nextstate = SSH_NO_STATE;
  1021. sshc->actualcode = CURLE_QUOTE_ERROR;
  1022. break;
  1023. }
  1024. state(data, SSH_SFTP_NEXT_QUOTE);
  1025. break;
  1026. case SSH_SFTP_QUOTE_STATVFS:
  1027. {
  1028. sftp_statvfs_t statvfs;
  1029. statvfs = sftp_statvfs(sshc->sftp_session, sshc->quote_path1);
  1030. if(!statvfs && !sshc->acceptfail) {
  1031. Curl_safefree(sshc->quote_path1);
  1032. failf(data, "statvfs command failed: %s",
  1033. ssh_get_error(sshc->ssh_session));
  1034. state(data, SSH_SFTP_CLOSE);
  1035. sshc->nextstate = SSH_NO_STATE;
  1036. sshc->actualcode = CURLE_QUOTE_ERROR;
  1037. break;
  1038. }
  1039. else if(statvfs) {
  1040. char *tmp = aprintf("statvfs:\n"
  1041. "f_bsize: %llu\n" "f_frsize: %llu\n"
  1042. "f_blocks: %llu\n" "f_bfree: %llu\n"
  1043. "f_bavail: %llu\n" "f_files: %llu\n"
  1044. "f_ffree: %llu\n" "f_favail: %llu\n"
  1045. "f_fsid: %llu\n" "f_flag: %llu\n"
  1046. "f_namemax: %llu\n",
  1047. statvfs->f_bsize, statvfs->f_frsize,
  1048. statvfs->f_blocks, statvfs->f_bfree,
  1049. statvfs->f_bavail, statvfs->f_files,
  1050. statvfs->f_ffree, statvfs->f_favail,
  1051. statvfs->f_fsid, statvfs->f_flag,
  1052. statvfs->f_namemax);
  1053. sftp_statvfs_free(statvfs);
  1054. if(!tmp) {
  1055. result = CURLE_OUT_OF_MEMORY;
  1056. state(data, SSH_SFTP_CLOSE);
  1057. sshc->nextstate = SSH_NO_STATE;
  1058. break;
  1059. }
  1060. result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
  1061. free(tmp);
  1062. if(result) {
  1063. state(data, SSH_SFTP_CLOSE);
  1064. sshc->nextstate = SSH_NO_STATE;
  1065. sshc->actualcode = result;
  1066. }
  1067. }
  1068. state(data, SSH_SFTP_NEXT_QUOTE);
  1069. break;
  1070. }
  1071. case SSH_SFTP_GETINFO:
  1072. if(data->set.get_filetime) {
  1073. state(data, SSH_SFTP_FILETIME);
  1074. }
  1075. else {
  1076. state(data, SSH_SFTP_TRANS_INIT);
  1077. }
  1078. break;
  1079. case SSH_SFTP_FILETIME:
  1080. {
  1081. sftp_attributes attrs;
  1082. attrs = sftp_stat(sshc->sftp_session, protop->path);
  1083. if(attrs) {
  1084. data->info.filetime = attrs->mtime;
  1085. sftp_attributes_free(attrs);
  1086. }
  1087. state(data, SSH_SFTP_TRANS_INIT);
  1088. break;
  1089. }
  1090. case SSH_SFTP_TRANS_INIT:
  1091. if(data->state.upload)
  1092. state(data, SSH_SFTP_UPLOAD_INIT);
  1093. else {
  1094. if(protop->path[strlen(protop->path)-1] == '/')
  1095. state(data, SSH_SFTP_READDIR_INIT);
  1096. else
  1097. state(data, SSH_SFTP_DOWNLOAD_INIT);
  1098. }
  1099. break;
  1100. case SSH_SFTP_UPLOAD_INIT:
  1101. {
  1102. int flags;
  1103. if(data->state.resume_from) {
  1104. sftp_attributes attrs;
  1105. if(data->state.resume_from < 0) {
  1106. attrs = sftp_stat(sshc->sftp_session, protop->path);
  1107. if(attrs) {
  1108. curl_off_t size = attrs->size;
  1109. if(size < 0) {
  1110. failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
  1111. MOVE_TO_ERROR_STATE(CURLE_BAD_DOWNLOAD_RESUME);
  1112. break;
  1113. }
  1114. data->state.resume_from = attrs->size;
  1115. sftp_attributes_free(attrs);
  1116. }
  1117. else {
  1118. data->state.resume_from = 0;
  1119. }
  1120. }
  1121. }
  1122. if(data->set.remote_append)
  1123. /* Try to open for append, but create if nonexisting */
  1124. flags = O_WRONLY|O_CREAT|O_APPEND;
  1125. else if(data->state.resume_from > 0)
  1126. /* If we have restart position then open for append */
  1127. flags = O_WRONLY|O_APPEND;
  1128. else
  1129. /* Clear file before writing (normal behavior) */
  1130. flags = O_WRONLY|O_CREAT|O_TRUNC;
  1131. if(sshc->sftp_file)
  1132. sftp_close(sshc->sftp_file);
  1133. sshc->sftp_file =
  1134. sftp_open(sshc->sftp_session, protop->path,
  1135. flags, (mode_t)data->set.new_file_perms);
  1136. if(!sshc->sftp_file) {
  1137. err = sftp_get_error(sshc->sftp_session);
  1138. if(((err == SSH_FX_NO_SUCH_FILE || err == SSH_FX_FAILURE ||
  1139. err == SSH_FX_NO_SUCH_PATH)) &&
  1140. (data->set.ftp_create_missing_dirs &&
  1141. (strlen(protop->path) > 1))) {
  1142. /* try to create the path remotely */
  1143. rc = 0;
  1144. sshc->secondCreateDirs = 1;
  1145. state(data, SSH_SFTP_CREATE_DIRS_INIT);
  1146. break;
  1147. }
  1148. else {
  1149. MOVE_TO_SFTP_CLOSE_STATE();
  1150. break;
  1151. }
  1152. }
  1153. /* If we have a restart point then we need to seek to the correct
  1154. position. */
  1155. if(data->state.resume_from > 0) {
  1156. /* Let's read off the proper amount of bytes from the input. */
  1157. if(conn->seek_func) {
  1158. Curl_set_in_callback(data, true);
  1159. seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
  1160. SEEK_SET);
  1161. Curl_set_in_callback(data, false);
  1162. }
  1163. if(seekerr != CURL_SEEKFUNC_OK) {
  1164. curl_off_t passed = 0;
  1165. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  1166. failf(data, "Could not seek stream");
  1167. return CURLE_FTP_COULDNT_USE_REST;
  1168. }
  1169. /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  1170. do {
  1171. size_t readthisamountnow =
  1172. (data->state.resume_from - passed > data->set.buffer_size) ?
  1173. (size_t)data->set.buffer_size :
  1174. curlx_sotouz(data->state.resume_from - passed);
  1175. size_t actuallyread =
  1176. data->state.fread_func(data->state.buffer, 1,
  1177. readthisamountnow, data->state.in);
  1178. passed += actuallyread;
  1179. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  1180. /* this checks for greater-than only to make sure that the
  1181. CURL_READFUNC_ABORT return code still aborts */
  1182. failf(data, "Failed to read data");
  1183. MOVE_TO_ERROR_STATE(CURLE_FTP_COULDNT_USE_REST);
  1184. break;
  1185. }
  1186. } while(passed < data->state.resume_from);
  1187. if(rc)
  1188. break;
  1189. }
  1190. /* now, decrease the size of the read */
  1191. if(data->state.infilesize > 0) {
  1192. data->state.infilesize -= data->state.resume_from;
  1193. data->req.size = data->state.infilesize;
  1194. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  1195. }
  1196. rc = sftp_seek64(sshc->sftp_file, data->state.resume_from);
  1197. if(rc) {
  1198. MOVE_TO_SFTP_CLOSE_STATE();
  1199. break;
  1200. }
  1201. }
  1202. if(data->state.infilesize > 0) {
  1203. data->req.size = data->state.infilesize;
  1204. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  1205. }
  1206. /* upload data */
  1207. Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
  1208. /* not set by Curl_setup_transfer to preserve keepon bits */
  1209. conn->sockfd = conn->writesockfd;
  1210. /* store this original bitmask setup to use later on if we can't
  1211. figure out a "real" bitmask */
  1212. sshc->orig_waitfor = data->req.keepon;
  1213. /* we want to use the _sending_ function even when the socket turns
  1214. out readable as the underlying libssh sftp send function will deal
  1215. with both accordingly */
  1216. conn->cselect_bits = CURL_CSELECT_OUT;
  1217. /* since we don't really wait for anything at this point, we want the
  1218. state machine to move on as soon as possible so we set a very short
  1219. timeout here */
  1220. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1221. state(data, SSH_STOP);
  1222. break;
  1223. }
  1224. case SSH_SFTP_CREATE_DIRS_INIT:
  1225. if(strlen(protop->path) > 1) {
  1226. sshc->slash_pos = protop->path + 1; /* ignore the leading '/' */
  1227. state(data, SSH_SFTP_CREATE_DIRS);
  1228. }
  1229. else {
  1230. state(data, SSH_SFTP_UPLOAD_INIT);
  1231. }
  1232. break;
  1233. case SSH_SFTP_CREATE_DIRS:
  1234. sshc->slash_pos = strchr(sshc->slash_pos, '/');
  1235. if(sshc->slash_pos) {
  1236. *sshc->slash_pos = 0;
  1237. infof(data, "Creating directory '%s'", protop->path);
  1238. state(data, SSH_SFTP_CREATE_DIRS_MKDIR);
  1239. break;
  1240. }
  1241. state(data, SSH_SFTP_UPLOAD_INIT);
  1242. break;
  1243. case SSH_SFTP_CREATE_DIRS_MKDIR:
  1244. /* 'mode' - parameter is preliminary - default to 0644 */
  1245. rc = sftp_mkdir(sshc->sftp_session, protop->path,
  1246. (mode_t)data->set.new_directory_perms);
  1247. *sshc->slash_pos = '/';
  1248. ++sshc->slash_pos;
  1249. if(rc < 0) {
  1250. /*
  1251. * Abort if failure wasn't that the dir already exists or the
  1252. * permission was denied (creation might succeed further down the
  1253. * path) - retry on unspecific FAILURE also
  1254. */
  1255. err = sftp_get_error(sshc->sftp_session);
  1256. if((err != SSH_FX_FILE_ALREADY_EXISTS) &&
  1257. (err != SSH_FX_FAILURE) &&
  1258. (err != SSH_FX_PERMISSION_DENIED)) {
  1259. MOVE_TO_SFTP_CLOSE_STATE();
  1260. break;
  1261. }
  1262. rc = 0; /* clear rc and continue */
  1263. }
  1264. state(data, SSH_SFTP_CREATE_DIRS);
  1265. break;
  1266. case SSH_SFTP_READDIR_INIT:
  1267. Curl_pgrsSetDownloadSize(data, -1);
  1268. if(data->req.no_body) {
  1269. state(data, SSH_STOP);
  1270. break;
  1271. }
  1272. /*
  1273. * This is a directory that we are trying to get, so produce a directory
  1274. * listing
  1275. */
  1276. sshc->sftp_dir = sftp_opendir(sshc->sftp_session,
  1277. protop->path);
  1278. if(!sshc->sftp_dir) {
  1279. failf(data, "Could not open directory for reading: %s",
  1280. ssh_get_error(sshc->ssh_session));
  1281. MOVE_TO_SFTP_CLOSE_STATE();
  1282. break;
  1283. }
  1284. state(data, SSH_SFTP_READDIR);
  1285. break;
  1286. case SSH_SFTP_READDIR:
  1287. Curl_dyn_reset(&sshc->readdir_buf);
  1288. if(sshc->readdir_attrs)
  1289. sftp_attributes_free(sshc->readdir_attrs);
  1290. sshc->readdir_attrs = sftp_readdir(sshc->sftp_session, sshc->sftp_dir);
  1291. if(sshc->readdir_attrs) {
  1292. sshc->readdir_filename = sshc->readdir_attrs->name;
  1293. sshc->readdir_longentry = sshc->readdir_attrs->longname;
  1294. sshc->readdir_len = strlen(sshc->readdir_filename);
  1295. if(data->set.list_only) {
  1296. char *tmpLine;
  1297. tmpLine = aprintf("%s\n", sshc->readdir_filename);
  1298. if(!tmpLine) {
  1299. state(data, SSH_SFTP_CLOSE);
  1300. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  1301. break;
  1302. }
  1303. result = Curl_client_write(data, CLIENTWRITE_BODY,
  1304. tmpLine, sshc->readdir_len + 1);
  1305. free(tmpLine);
  1306. if(result) {
  1307. state(data, SSH_STOP);
  1308. break;
  1309. }
  1310. /* since this counts what we send to the client, we include the
  1311. newline in this counter */
  1312. data->req.bytecount += sshc->readdir_len + 1;
  1313. /* output debug output if that is requested */
  1314. Curl_debug(data, CURLINFO_DATA_OUT, (char *)sshc->readdir_filename,
  1315. sshc->readdir_len);
  1316. }
  1317. else {
  1318. if(Curl_dyn_add(&sshc->readdir_buf, sshc->readdir_longentry)) {
  1319. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  1320. state(data, SSH_STOP);
  1321. break;
  1322. }
  1323. if((sshc->readdir_attrs->flags & SSH_FILEXFER_ATTR_PERMISSIONS) &&
  1324. ((sshc->readdir_attrs->permissions & SSH_S_IFMT) ==
  1325. SSH_S_IFLNK)) {
  1326. sshc->readdir_linkPath = aprintf("%s%s", protop->path,
  1327. sshc->readdir_filename);
  1328. if(!sshc->readdir_linkPath) {
  1329. state(data, SSH_SFTP_CLOSE);
  1330. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  1331. break;
  1332. }
  1333. state(data, SSH_SFTP_READDIR_LINK);
  1334. break;
  1335. }
  1336. state(data, SSH_SFTP_READDIR_BOTTOM);
  1337. break;
  1338. }
  1339. }
  1340. else if(sftp_dir_eof(sshc->sftp_dir)) {
  1341. state(data, SSH_SFTP_READDIR_DONE);
  1342. break;
  1343. }
  1344. else {
  1345. failf(data, "Could not open remote file for reading: %s",
  1346. ssh_get_error(sshc->ssh_session));
  1347. MOVE_TO_SFTP_CLOSE_STATE();
  1348. break;
  1349. }
  1350. break;
  1351. case SSH_SFTP_READDIR_LINK:
  1352. if(sshc->readdir_link_attrs)
  1353. sftp_attributes_free(sshc->readdir_link_attrs);
  1354. sshc->readdir_link_attrs = sftp_lstat(sshc->sftp_session,
  1355. sshc->readdir_linkPath);
  1356. if(sshc->readdir_link_attrs == 0) {
  1357. failf(data, "Could not read symlink for reading: %s",
  1358. ssh_get_error(sshc->ssh_session));
  1359. MOVE_TO_SFTP_CLOSE_STATE();
  1360. break;
  1361. }
  1362. if(!sshc->readdir_link_attrs->name) {
  1363. sshc->readdir_tmp = sftp_readlink(sshc->sftp_session,
  1364. sshc->readdir_linkPath);
  1365. if(!sshc->readdir_filename)
  1366. sshc->readdir_len = 0;
  1367. else
  1368. sshc->readdir_len = strlen(sshc->readdir_tmp);
  1369. sshc->readdir_longentry = NULL;
  1370. sshc->readdir_filename = sshc->readdir_tmp;
  1371. }
  1372. else {
  1373. sshc->readdir_len = strlen(sshc->readdir_link_attrs->name);
  1374. sshc->readdir_filename = sshc->readdir_link_attrs->name;
  1375. sshc->readdir_longentry = sshc->readdir_link_attrs->longname;
  1376. }
  1377. Curl_safefree(sshc->readdir_linkPath);
  1378. if(Curl_dyn_addf(&sshc->readdir_buf, " -> %s",
  1379. sshc->readdir_filename)) {
  1380. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  1381. break;
  1382. }
  1383. sftp_attributes_free(sshc->readdir_link_attrs);
  1384. sshc->readdir_link_attrs = NULL;
  1385. sshc->readdir_filename = NULL;
  1386. sshc->readdir_longentry = NULL;
  1387. state(data, SSH_SFTP_READDIR_BOTTOM);
  1388. /* FALLTHROUGH */
  1389. case SSH_SFTP_READDIR_BOTTOM:
  1390. if(Curl_dyn_addn(&sshc->readdir_buf, "\n", 1))
  1391. result = CURLE_OUT_OF_MEMORY;
  1392. else
  1393. result = Curl_client_write(data, CLIENTWRITE_BODY,
  1394. Curl_dyn_ptr(&sshc->readdir_buf),
  1395. Curl_dyn_len(&sshc->readdir_buf));
  1396. if(!result) {
  1397. /* output debug output if that is requested */
  1398. Curl_debug(data, CURLINFO_DATA_OUT, Curl_dyn_ptr(&sshc->readdir_buf),
  1399. Curl_dyn_len(&sshc->readdir_buf));
  1400. data->req.bytecount += Curl_dyn_len(&sshc->readdir_buf);
  1401. }
  1402. ssh_string_free_char(sshc->readdir_tmp);
  1403. sshc->readdir_tmp = NULL;
  1404. if(result) {
  1405. state(data, SSH_STOP);
  1406. }
  1407. else
  1408. state(data, SSH_SFTP_READDIR);
  1409. break;
  1410. case SSH_SFTP_READDIR_DONE:
  1411. sftp_closedir(sshc->sftp_dir);
  1412. sshc->sftp_dir = NULL;
  1413. /* no data to transfer */
  1414. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  1415. state(data, SSH_STOP);
  1416. break;
  1417. case SSH_SFTP_DOWNLOAD_INIT:
  1418. /*
  1419. * Work on getting the specified file
  1420. */
  1421. if(sshc->sftp_file)
  1422. sftp_close(sshc->sftp_file);
  1423. sshc->sftp_file = sftp_open(sshc->sftp_session, protop->path,
  1424. O_RDONLY, (mode_t)data->set.new_file_perms);
  1425. if(!sshc->sftp_file) {
  1426. failf(data, "Could not open remote file for reading: %s",
  1427. ssh_get_error(sshc->ssh_session));
  1428. MOVE_TO_SFTP_CLOSE_STATE();
  1429. break;
  1430. }
  1431. sftp_file_set_nonblocking(sshc->sftp_file);
  1432. state(data, SSH_SFTP_DOWNLOAD_STAT);
  1433. break;
  1434. case SSH_SFTP_DOWNLOAD_STAT:
  1435. {
  1436. sftp_attributes attrs;
  1437. curl_off_t size;
  1438. attrs = sftp_fstat(sshc->sftp_file);
  1439. if(!attrs ||
  1440. !(attrs->flags & SSH_FILEXFER_ATTR_SIZE) ||
  1441. (attrs->size == 0)) {
  1442. /*
  1443. * sftp_fstat didn't return an error, so maybe the server
  1444. * just doesn't support stat()
  1445. * OR the server doesn't return a file size with a stat()
  1446. * OR file size is 0
  1447. */
  1448. data->req.size = -1;
  1449. data->req.maxdownload = -1;
  1450. Curl_pgrsSetDownloadSize(data, -1);
  1451. size = 0;
  1452. }
  1453. else {
  1454. size = attrs->size;
  1455. sftp_attributes_free(attrs);
  1456. if(size < 0) {
  1457. failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
  1458. return CURLE_BAD_DOWNLOAD_RESUME;
  1459. }
  1460. if(data->state.use_range) {
  1461. curl_off_t from, to;
  1462. char *ptr;
  1463. char *ptr2;
  1464. CURLofft to_t;
  1465. CURLofft from_t;
  1466. from_t = curlx_strtoofft(data->state.range, &ptr, 10, &from);
  1467. if(from_t == CURL_OFFT_FLOW) {
  1468. return CURLE_RANGE_ERROR;
  1469. }
  1470. while(*ptr && (ISBLANK(*ptr) || (*ptr == '-')))
  1471. ptr++;
  1472. to_t = curlx_strtoofft(ptr, &ptr2, 10, &to);
  1473. if(to_t == CURL_OFFT_FLOW) {
  1474. return CURLE_RANGE_ERROR;
  1475. }
  1476. if((to_t == CURL_OFFT_INVAL) /* no "to" value given */
  1477. || (to >= size)) {
  1478. to = size - 1;
  1479. }
  1480. if(from_t) {
  1481. /* from is relative to end of file */
  1482. from = size - to;
  1483. to = size - 1;
  1484. }
  1485. if(from > size) {
  1486. failf(data, "Offset (%"
  1487. CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
  1488. CURL_FORMAT_CURL_OFF_T ")", from, size);
  1489. return CURLE_BAD_DOWNLOAD_RESUME;
  1490. }
  1491. if(from > to) {
  1492. from = to;
  1493. size = 0;
  1494. }
  1495. else {
  1496. size = to - from + 1;
  1497. }
  1498. rc = sftp_seek64(sshc->sftp_file, from);
  1499. if(rc) {
  1500. MOVE_TO_SFTP_CLOSE_STATE();
  1501. break;
  1502. }
  1503. }
  1504. data->req.size = size;
  1505. data->req.maxdownload = size;
  1506. Curl_pgrsSetDownloadSize(data, size);
  1507. }
  1508. /* We can resume if we can seek to the resume position */
  1509. if(data->state.resume_from) {
  1510. if(data->state.resume_from < 0) {
  1511. /* We're supposed to download the last abs(from) bytes */
  1512. if((curl_off_t)size < -data->state.resume_from) {
  1513. failf(data, "Offset (%"
  1514. CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
  1515. CURL_FORMAT_CURL_OFF_T ")",
  1516. data->state.resume_from, size);
  1517. return CURLE_BAD_DOWNLOAD_RESUME;
  1518. }
  1519. /* download from where? */
  1520. data->state.resume_from += size;
  1521. }
  1522. else {
  1523. if((curl_off_t)size < data->state.resume_from) {
  1524. failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
  1525. ") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
  1526. data->state.resume_from, size);
  1527. return CURLE_BAD_DOWNLOAD_RESUME;
  1528. }
  1529. }
  1530. /* Now store the number of bytes we are expected to download */
  1531. data->req.size = size - data->state.resume_from;
  1532. data->req.maxdownload = size - data->state.resume_from;
  1533. Curl_pgrsSetDownloadSize(data,
  1534. size - data->state.resume_from);
  1535. rc = sftp_seek64(sshc->sftp_file, data->state.resume_from);
  1536. if(rc) {
  1537. MOVE_TO_SFTP_CLOSE_STATE();
  1538. break;
  1539. }
  1540. }
  1541. }
  1542. /* Setup the actual download */
  1543. if(data->req.size == 0) {
  1544. /* no data to transfer */
  1545. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  1546. infof(data, "File already completely downloaded");
  1547. state(data, SSH_STOP);
  1548. break;
  1549. }
  1550. Curl_setup_transfer(data, FIRSTSOCKET, data->req.size, FALSE, -1);
  1551. /* not set by Curl_setup_transfer to preserve keepon bits */
  1552. conn->writesockfd = conn->sockfd;
  1553. /* we want to use the _receiving_ function even when the socket turns
  1554. out writableable as the underlying libssh recv function will deal
  1555. with both accordingly */
  1556. conn->cselect_bits = CURL_CSELECT_IN;
  1557. if(result) {
  1558. /* this should never occur; the close state should be entered
  1559. at the time the error occurs */
  1560. state(data, SSH_SFTP_CLOSE);
  1561. sshc->actualcode = result;
  1562. }
  1563. else {
  1564. sshc->sftp_recv_state = 0;
  1565. state(data, SSH_STOP);
  1566. }
  1567. break;
  1568. case SSH_SFTP_CLOSE:
  1569. if(sshc->sftp_file) {
  1570. sftp_close(sshc->sftp_file);
  1571. sshc->sftp_file = NULL;
  1572. }
  1573. Curl_safefree(protop->path);
  1574. DEBUGF(infof(data, "SFTP DONE done"));
  1575. /* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT
  1576. After nextstate is executed, the control should come back to
  1577. SSH_SFTP_CLOSE to pass the correct result back */
  1578. if(sshc->nextstate != SSH_NO_STATE &&
  1579. sshc->nextstate != SSH_SFTP_CLOSE) {
  1580. state(data, sshc->nextstate);
  1581. sshc->nextstate = SSH_SFTP_CLOSE;
  1582. }
  1583. else {
  1584. state(data, SSH_STOP);
  1585. result = sshc->actualcode;
  1586. }
  1587. break;
  1588. case SSH_SFTP_SHUTDOWN:
  1589. /* during times we get here due to a broken transfer and then the
  1590. sftp_handle might not have been taken down so make sure that is done
  1591. before we proceed */
  1592. if(sshc->sftp_file) {
  1593. sftp_close(sshc->sftp_file);
  1594. sshc->sftp_file = NULL;
  1595. }
  1596. if(sshc->sftp_session) {
  1597. sftp_free(sshc->sftp_session);
  1598. sshc->sftp_session = NULL;
  1599. }
  1600. SSH_STRING_FREE_CHAR(sshc->homedir);
  1601. data->state.most_recent_ftp_entrypath = NULL;
  1602. state(data, SSH_SESSION_DISCONNECT);
  1603. break;
  1604. case SSH_SCP_TRANS_INIT:
  1605. result = Curl_getworkingpath(data, sshc->homedir, &protop->path);
  1606. if(result) {
  1607. sshc->actualcode = result;
  1608. state(data, SSH_STOP);
  1609. break;
  1610. }
  1611. /* Functions from the SCP subsystem cannot handle/return SSH_AGAIN */
  1612. ssh_set_blocking(sshc->ssh_session, 1);
  1613. if(data->state.upload) {
  1614. if(data->state.infilesize < 0) {
  1615. failf(data, "SCP requires a known file size for upload");
  1616. sshc->actualcode = CURLE_UPLOAD_FAILED;
  1617. MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
  1618. break;
  1619. }
  1620. sshc->scp_session =
  1621. ssh_scp_new(sshc->ssh_session, SSH_SCP_WRITE, protop->path);
  1622. state(data, SSH_SCP_UPLOAD_INIT);
  1623. }
  1624. else {
  1625. sshc->scp_session =
  1626. ssh_scp_new(sshc->ssh_session, SSH_SCP_READ, protop->path);
  1627. state(data, SSH_SCP_DOWNLOAD_INIT);
  1628. }
  1629. if(!sshc->scp_session) {
  1630. err_msg = ssh_get_error(sshc->ssh_session);
  1631. failf(data, "%s", err_msg);
  1632. MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
  1633. }
  1634. break;
  1635. case SSH_SCP_UPLOAD_INIT:
  1636. rc = ssh_scp_init(sshc->scp_session);
  1637. if(rc != SSH_OK) {
  1638. err_msg = ssh_get_error(sshc->ssh_session);
  1639. failf(data, "%s", err_msg);
  1640. MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
  1641. break;
  1642. }
  1643. rc = ssh_scp_push_file(sshc->scp_session, protop->path,
  1644. data->state.infilesize,
  1645. (int)data->set.new_file_perms);
  1646. if(rc != SSH_OK) {
  1647. err_msg = ssh_get_error(sshc->ssh_session);
  1648. failf(data, "%s", err_msg);
  1649. MOVE_TO_ERROR_STATE(CURLE_UPLOAD_FAILED);
  1650. break;
  1651. }
  1652. /* upload data */
  1653. Curl_setup_transfer(data, -1, data->req.size, FALSE, FIRSTSOCKET);
  1654. /* not set by Curl_setup_transfer to preserve keepon bits */
  1655. conn->sockfd = conn->writesockfd;
  1656. /* store this original bitmask setup to use later on if we can't
  1657. figure out a "real" bitmask */
  1658. sshc->orig_waitfor = data->req.keepon;
  1659. /* we want to use the _sending_ function even when the socket turns
  1660. out readable as the underlying libssh scp send function will deal
  1661. with both accordingly */
  1662. conn->cselect_bits = CURL_CSELECT_OUT;
  1663. state(data, SSH_STOP);
  1664. break;
  1665. case SSH_SCP_DOWNLOAD_INIT:
  1666. rc = ssh_scp_init(sshc->scp_session);
  1667. if(rc != SSH_OK) {
  1668. err_msg = ssh_get_error(sshc->ssh_session);
  1669. failf(data, "%s", err_msg);
  1670. MOVE_TO_ERROR_STATE(CURLE_COULDNT_CONNECT);
  1671. break;
  1672. }
  1673. state(data, SSH_SCP_DOWNLOAD);
  1674. /* FALLTHROUGH */
  1675. case SSH_SCP_DOWNLOAD:{
  1676. curl_off_t bytecount;
  1677. rc = ssh_scp_pull_request(sshc->scp_session);
  1678. if(rc != SSH_SCP_REQUEST_NEWFILE) {
  1679. err_msg = ssh_get_error(sshc->ssh_session);
  1680. failf(data, "%s", err_msg);
  1681. MOVE_TO_ERROR_STATE(CURLE_REMOTE_FILE_NOT_FOUND);
  1682. break;
  1683. }
  1684. /* download data */
  1685. bytecount = ssh_scp_request_get_size(sshc->scp_session);
  1686. data->req.maxdownload = (curl_off_t) bytecount;
  1687. Curl_setup_transfer(data, FIRSTSOCKET, bytecount, FALSE, -1);
  1688. /* not set by Curl_setup_transfer to preserve keepon bits */
  1689. conn->writesockfd = conn->sockfd;
  1690. /* we want to use the _receiving_ function even when the socket turns
  1691. out writableable as the underlying libssh recv function will deal
  1692. with both accordingly */
  1693. conn->cselect_bits = CURL_CSELECT_IN;
  1694. state(data, SSH_STOP);
  1695. break;
  1696. }
  1697. case SSH_SCP_DONE:
  1698. if(data->state.upload)
  1699. state(data, SSH_SCP_SEND_EOF);
  1700. else
  1701. state(data, SSH_SCP_CHANNEL_FREE);
  1702. break;
  1703. case SSH_SCP_SEND_EOF:
  1704. if(sshc->scp_session) {
  1705. rc = ssh_scp_close(sshc->scp_session);
  1706. if(rc == SSH_AGAIN) {
  1707. /* Currently the ssh_scp_close handles waiting for EOF in
  1708. * blocking way.
  1709. */
  1710. break;
  1711. }
  1712. if(rc != SSH_OK) {
  1713. infof(data, "Failed to close libssh scp channel: %s",
  1714. ssh_get_error(sshc->ssh_session));
  1715. }
  1716. }
  1717. state(data, SSH_SCP_CHANNEL_FREE);
  1718. break;
  1719. case SSH_SCP_CHANNEL_FREE:
  1720. if(sshc->scp_session) {
  1721. ssh_scp_free(sshc->scp_session);
  1722. sshc->scp_session = NULL;
  1723. }
  1724. DEBUGF(infof(data, "SCP DONE phase complete"));
  1725. ssh_set_blocking(sshc->ssh_session, 0);
  1726. state(data, SSH_SESSION_DISCONNECT);
  1727. /* FALLTHROUGH */
  1728. case SSH_SESSION_DISCONNECT:
  1729. /* during weird times when we've been prematurely aborted, the channel
  1730. is still alive when we reach this state and we MUST kill the channel
  1731. properly first */
  1732. if(sshc->scp_session) {
  1733. ssh_scp_free(sshc->scp_session);
  1734. sshc->scp_session = NULL;
  1735. }
  1736. ssh_disconnect(sshc->ssh_session);
  1737. if(!ssh_version(SSH_VERSION_INT(0, 10, 0))) {
  1738. /* conn->sock[FIRSTSOCKET] is closed by ssh_disconnect behind our back,
  1739. explicitly mark it as closed with the memdebug macro. This libssh
  1740. bug is fixed in 0.10.0. */
  1741. fake_sclose(conn->sock[FIRSTSOCKET]);
  1742. conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD;
  1743. }
  1744. SSH_STRING_FREE_CHAR(sshc->homedir);
  1745. data->state.most_recent_ftp_entrypath = NULL;
  1746. state(data, SSH_SESSION_FREE);
  1747. /* FALLTHROUGH */
  1748. case SSH_SESSION_FREE:
  1749. if(sshc->ssh_session) {
  1750. ssh_free(sshc->ssh_session);
  1751. sshc->ssh_session = NULL;
  1752. }
  1753. /* worst-case scenario cleanup */
  1754. DEBUGASSERT(sshc->ssh_session == NULL);
  1755. DEBUGASSERT(sshc->scp_session == NULL);
  1756. if(sshc->readdir_tmp) {
  1757. ssh_string_free_char(sshc->readdir_tmp);
  1758. sshc->readdir_tmp = NULL;
  1759. }
  1760. if(sshc->quote_attrs)
  1761. sftp_attributes_free(sshc->quote_attrs);
  1762. if(sshc->readdir_attrs)
  1763. sftp_attributes_free(sshc->readdir_attrs);
  1764. if(sshc->readdir_link_attrs)
  1765. sftp_attributes_free(sshc->readdir_link_attrs);
  1766. if(sshc->privkey)
  1767. ssh_key_free(sshc->privkey);
  1768. if(sshc->pubkey)
  1769. ssh_key_free(sshc->pubkey);
  1770. Curl_safefree(sshc->rsa_pub);
  1771. Curl_safefree(sshc->rsa);
  1772. Curl_safefree(sshc->quote_path1);
  1773. Curl_safefree(sshc->quote_path2);
  1774. Curl_dyn_free(&sshc->readdir_buf);
  1775. Curl_safefree(sshc->readdir_linkPath);
  1776. SSH_STRING_FREE_CHAR(sshc->homedir);
  1777. /* the code we are about to return */
  1778. result = sshc->actualcode;
  1779. memset(sshc, 0, sizeof(struct ssh_conn));
  1780. connclose(conn, "SSH session free");
  1781. sshc->state = SSH_SESSION_FREE; /* current */
  1782. sshc->nextstate = SSH_NO_STATE;
  1783. state(data, SSH_STOP);
  1784. break;
  1785. case SSH_QUIT:
  1786. /* fallthrough, just stop! */
  1787. default:
  1788. /* internal error */
  1789. sshc->nextstate = SSH_NO_STATE;
  1790. state(data, SSH_STOP);
  1791. break;
  1792. }
  1793. } while(!rc && (sshc->state != SSH_STOP));
  1794. if(rc == SSH_AGAIN) {
  1795. /* we would block, we need to wait for the socket to be ready (in the
  1796. right direction too)! */
  1797. *block = TRUE;
  1798. }
  1799. return result;
  1800. }
  1801. /* called by the multi interface to figure out what socket(s) to wait for and
  1802. for what actions in the DO_DONE, PERFORM and WAITPERFORM states */
  1803. static int myssh_getsock(struct Curl_easy *data,
  1804. struct connectdata *conn,
  1805. curl_socket_t *sock)
  1806. {
  1807. int bitmap = GETSOCK_BLANK;
  1808. (void)data;
  1809. sock[0] = conn->sock[FIRSTSOCKET];
  1810. if(conn->waitfor & KEEP_RECV)
  1811. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  1812. if(conn->waitfor & KEEP_SEND)
  1813. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  1814. if(!conn->waitfor)
  1815. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  1816. return bitmap;
  1817. }
  1818. static void myssh_block2waitfor(struct connectdata *conn, bool block)
  1819. {
  1820. struct ssh_conn *sshc = &conn->proto.sshc;
  1821. /* If it didn't block, or nothing was returned by ssh_get_poll_flags
  1822. * have the original set */
  1823. conn->waitfor = sshc->orig_waitfor;
  1824. if(block) {
  1825. int dir = ssh_get_poll_flags(sshc->ssh_session);
  1826. if(dir & SSH_READ_PENDING) {
  1827. /* translate the libssh define bits into our own bit defines */
  1828. conn->waitfor = KEEP_RECV;
  1829. }
  1830. else if(dir & SSH_WRITE_PENDING) {
  1831. conn->waitfor = KEEP_SEND;
  1832. }
  1833. }
  1834. }
  1835. /* called repeatedly until done from multi.c */
  1836. static CURLcode myssh_multi_statemach(struct Curl_easy *data,
  1837. bool *done)
  1838. {
  1839. struct connectdata *conn = data->conn;
  1840. struct ssh_conn *sshc = &conn->proto.sshc;
  1841. bool block; /* we store the status and use that to provide a ssh_getsock()
  1842. implementation */
  1843. CURLcode result = myssh_statemach_act(data, &block);
  1844. *done = (sshc->state == SSH_STOP) ? TRUE : FALSE;
  1845. myssh_block2waitfor(conn, block);
  1846. return result;
  1847. }
  1848. static CURLcode myssh_block_statemach(struct Curl_easy *data,
  1849. bool disconnect)
  1850. {
  1851. struct connectdata *conn = data->conn;
  1852. struct ssh_conn *sshc = &conn->proto.sshc;
  1853. CURLcode result = CURLE_OK;
  1854. while((sshc->state != SSH_STOP) && !result) {
  1855. bool block;
  1856. timediff_t left = 1000;
  1857. struct curltime now = Curl_now();
  1858. result = myssh_statemach_act(data, &block);
  1859. if(result)
  1860. break;
  1861. if(!disconnect) {
  1862. if(Curl_pgrsUpdate(data))
  1863. return CURLE_ABORTED_BY_CALLBACK;
  1864. result = Curl_speedcheck(data, now);
  1865. if(result)
  1866. break;
  1867. left = Curl_timeleft(data, NULL, FALSE);
  1868. if(left < 0) {
  1869. failf(data, "Operation timed out");
  1870. return CURLE_OPERATION_TIMEDOUT;
  1871. }
  1872. }
  1873. if(block) {
  1874. curl_socket_t fd_read = conn->sock[FIRSTSOCKET];
  1875. /* wait for the socket to become ready */
  1876. (void) Curl_socket_check(fd_read, CURL_SOCKET_BAD,
  1877. CURL_SOCKET_BAD, left > 1000 ? 1000 : left);
  1878. }
  1879. }
  1880. return result;
  1881. }
  1882. /*
  1883. * SSH setup connection
  1884. */
  1885. static CURLcode myssh_setup_connection(struct Curl_easy *data,
  1886. struct connectdata *conn)
  1887. {
  1888. struct SSHPROTO *ssh;
  1889. struct ssh_conn *sshc = &conn->proto.sshc;
  1890. data->req.p.ssh = ssh = calloc(1, sizeof(struct SSHPROTO));
  1891. if(!ssh)
  1892. return CURLE_OUT_OF_MEMORY;
  1893. Curl_dyn_init(&sshc->readdir_buf, PATH_MAX * 2);
  1894. return CURLE_OK;
  1895. }
  1896. static Curl_recv scp_recv, sftp_recv;
  1897. static Curl_send scp_send, sftp_send;
  1898. /*
  1899. * Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
  1900. * do protocol-specific actions at connect-time.
  1901. */
  1902. static CURLcode myssh_connect(struct Curl_easy *data, bool *done)
  1903. {
  1904. struct ssh_conn *ssh;
  1905. CURLcode result;
  1906. struct connectdata *conn = data->conn;
  1907. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  1908. int rc;
  1909. /* initialize per-handle data if not already */
  1910. if(!data->req.p.ssh)
  1911. myssh_setup_connection(data, conn);
  1912. /* We default to persistent connections. We set this already in this connect
  1913. function to make the re-use checks properly be able to check this bit. */
  1914. connkeep(conn, "SSH default");
  1915. if(conn->handler->protocol & CURLPROTO_SCP) {
  1916. conn->recv[FIRSTSOCKET] = scp_recv;
  1917. conn->send[FIRSTSOCKET] = scp_send;
  1918. }
  1919. else {
  1920. conn->recv[FIRSTSOCKET] = sftp_recv;
  1921. conn->send[FIRSTSOCKET] = sftp_send;
  1922. }
  1923. ssh = &conn->proto.sshc;
  1924. ssh->ssh_session = ssh_new();
  1925. if(!ssh->ssh_session) {
  1926. failf(data, "Failure initialising ssh session");
  1927. return CURLE_FAILED_INIT;
  1928. }
  1929. rc = ssh_options_set(ssh->ssh_session, SSH_OPTIONS_HOST, conn->host.name);
  1930. if(rc != SSH_OK) {
  1931. failf(data, "Could not set remote host");
  1932. return CURLE_FAILED_INIT;
  1933. }
  1934. rc = ssh_options_parse_config(ssh->ssh_session, NULL);
  1935. if(rc != SSH_OK) {
  1936. infof(data, "Could not parse SSH configuration files");
  1937. /* ignore */
  1938. }
  1939. rc = ssh_options_set(ssh->ssh_session, SSH_OPTIONS_FD, &sock);
  1940. if(rc != SSH_OK) {
  1941. failf(data, "Could not set socket");
  1942. return CURLE_FAILED_INIT;
  1943. }
  1944. if(conn->user && conn->user[0] != '\0') {
  1945. infof(data, "User: %s", conn->user);
  1946. rc = ssh_options_set(ssh->ssh_session, SSH_OPTIONS_USER, conn->user);
  1947. if(rc != SSH_OK) {
  1948. failf(data, "Could not set user");
  1949. return CURLE_FAILED_INIT;
  1950. }
  1951. }
  1952. if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
  1953. infof(data, "Known hosts: %s", data->set.str[STRING_SSH_KNOWNHOSTS]);
  1954. rc = ssh_options_set(ssh->ssh_session, SSH_OPTIONS_KNOWNHOSTS,
  1955. data->set.str[STRING_SSH_KNOWNHOSTS]);
  1956. if(rc != SSH_OK) {
  1957. failf(data, "Could not set known hosts file path");
  1958. return CURLE_FAILED_INIT;
  1959. }
  1960. }
  1961. if(conn->remote_port) {
  1962. rc = ssh_options_set(ssh->ssh_session, SSH_OPTIONS_PORT,
  1963. &conn->remote_port);
  1964. if(rc != SSH_OK) {
  1965. failf(data, "Could not set remote port");
  1966. return CURLE_FAILED_INIT;
  1967. }
  1968. }
  1969. if(data->set.ssh_compression) {
  1970. rc = ssh_options_set(ssh->ssh_session, SSH_OPTIONS_COMPRESSION,
  1971. "zlib,zlib@openssh.com,none");
  1972. if(rc != SSH_OK) {
  1973. failf(data, "Could not set compression");
  1974. return CURLE_FAILED_INIT;
  1975. }
  1976. }
  1977. ssh->privkey = NULL;
  1978. ssh->pubkey = NULL;
  1979. if(data->set.str[STRING_SSH_PUBLIC_KEY]) {
  1980. rc = ssh_pki_import_pubkey_file(data->set.str[STRING_SSH_PUBLIC_KEY],
  1981. &ssh->pubkey);
  1982. if(rc != SSH_OK) {
  1983. failf(data, "Could not load public key file");
  1984. return CURLE_FAILED_INIT;
  1985. }
  1986. }
  1987. /* we do not verify here, we do it at the state machine,
  1988. * after connection */
  1989. state(data, SSH_INIT);
  1990. result = myssh_multi_statemach(data, done);
  1991. return result;
  1992. }
  1993. /* called from multi.c while DOing */
  1994. static CURLcode scp_doing(struct Curl_easy *data, bool *dophase_done)
  1995. {
  1996. CURLcode result;
  1997. result = myssh_multi_statemach(data, dophase_done);
  1998. if(*dophase_done) {
  1999. DEBUGF(infof(data, "DO phase is complete"));
  2000. }
  2001. return result;
  2002. }
  2003. /*
  2004. ***********************************************************************
  2005. *
  2006. * scp_perform()
  2007. *
  2008. * This is the actual DO function for SCP. Get a file according to
  2009. * the options previously setup.
  2010. */
  2011. static
  2012. CURLcode scp_perform(struct Curl_easy *data,
  2013. bool *connected, bool *dophase_done)
  2014. {
  2015. CURLcode result = CURLE_OK;
  2016. DEBUGF(infof(data, "DO phase starts"));
  2017. *dophase_done = FALSE; /* not done yet */
  2018. /* start the first command in the DO phase */
  2019. state(data, SSH_SCP_TRANS_INIT);
  2020. result = myssh_multi_statemach(data, dophase_done);
  2021. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  2022. if(*dophase_done) {
  2023. DEBUGF(infof(data, "DO phase is complete"));
  2024. }
  2025. return result;
  2026. }
  2027. static CURLcode myssh_do_it(struct Curl_easy *data, bool *done)
  2028. {
  2029. CURLcode result;
  2030. bool connected = 0;
  2031. struct connectdata *conn = data->conn;
  2032. struct ssh_conn *sshc = &conn->proto.sshc;
  2033. *done = FALSE; /* default to false */
  2034. data->req.size = -1; /* make sure this is unknown at this point */
  2035. sshc->actualcode = CURLE_OK; /* reset error code */
  2036. sshc->secondCreateDirs = 0; /* reset the create dir attempt state
  2037. variable */
  2038. Curl_pgrsSetUploadCounter(data, 0);
  2039. Curl_pgrsSetDownloadCounter(data, 0);
  2040. Curl_pgrsSetUploadSize(data, -1);
  2041. Curl_pgrsSetDownloadSize(data, -1);
  2042. if(conn->handler->protocol & CURLPROTO_SCP)
  2043. result = scp_perform(data, &connected, done);
  2044. else
  2045. result = sftp_perform(data, &connected, done);
  2046. return result;
  2047. }
  2048. /* BLOCKING, but the function is using the state machine so the only reason
  2049. this is still blocking is that the multi interface code has no support for
  2050. disconnecting operations that takes a while */
  2051. static CURLcode scp_disconnect(struct Curl_easy *data,
  2052. struct connectdata *conn,
  2053. bool dead_connection)
  2054. {
  2055. CURLcode result = CURLE_OK;
  2056. struct ssh_conn *ssh = &conn->proto.sshc;
  2057. (void) dead_connection;
  2058. if(ssh->ssh_session) {
  2059. /* only if there's a session still around to use! */
  2060. state(data, SSH_SESSION_DISCONNECT);
  2061. result = myssh_block_statemach(data, TRUE);
  2062. }
  2063. return result;
  2064. }
  2065. /* generic done function for both SCP and SFTP called from their specific
  2066. done functions */
  2067. static CURLcode myssh_done(struct Curl_easy *data, CURLcode status)
  2068. {
  2069. CURLcode result = CURLE_OK;
  2070. struct SSHPROTO *protop = data->req.p.ssh;
  2071. if(!status) {
  2072. /* run the state-machine */
  2073. result = myssh_block_statemach(data, FALSE);
  2074. }
  2075. else
  2076. result = status;
  2077. if(protop)
  2078. Curl_safefree(protop->path);
  2079. if(Curl_pgrsDone(data))
  2080. return CURLE_ABORTED_BY_CALLBACK;
  2081. data->req.keepon = 0; /* clear all bits */
  2082. return result;
  2083. }
  2084. static CURLcode scp_done(struct Curl_easy *data, CURLcode status,
  2085. bool premature)
  2086. {
  2087. (void) premature; /* not used */
  2088. if(!status)
  2089. state(data, SSH_SCP_DONE);
  2090. return myssh_done(data, status);
  2091. }
  2092. static ssize_t scp_send(struct Curl_easy *data, int sockindex,
  2093. const void *mem, size_t len, CURLcode *err)
  2094. {
  2095. int rc;
  2096. struct connectdata *conn = data->conn;
  2097. (void) sockindex; /* we only support SCP on the fixed known primary socket */
  2098. (void) err;
  2099. rc = ssh_scp_write(conn->proto.sshc.scp_session, mem, len);
  2100. #if 0
  2101. /* The following code is misleading, mostly added as wishful thinking
  2102. * that libssh at some point will implement non-blocking ssh_scp_write/read.
  2103. * Currently rc can only be number of bytes read or SSH_ERROR. */
  2104. myssh_block2waitfor(conn, (rc == SSH_AGAIN) ? TRUE : FALSE);
  2105. if(rc == SSH_AGAIN) {
  2106. *err = CURLE_AGAIN;
  2107. return 0;
  2108. }
  2109. else
  2110. #endif
  2111. if(rc != SSH_OK) {
  2112. *err = CURLE_SSH;
  2113. return -1;
  2114. }
  2115. return len;
  2116. }
  2117. static ssize_t scp_recv(struct Curl_easy *data, int sockindex,
  2118. char *mem, size_t len, CURLcode *err)
  2119. {
  2120. ssize_t nread;
  2121. struct connectdata *conn = data->conn;
  2122. (void) err;
  2123. (void) sockindex; /* we only support SCP on the fixed known primary socket */
  2124. /* libssh returns int */
  2125. nread = ssh_scp_read(conn->proto.sshc.scp_session, mem, len);
  2126. #if 0
  2127. /* The following code is misleading, mostly added as wishful thinking
  2128. * that libssh at some point will implement non-blocking ssh_scp_write/read.
  2129. * Currently rc can only be SSH_OK or SSH_ERROR. */
  2130. myssh_block2waitfor(conn, (nread == SSH_AGAIN) ? TRUE : FALSE);
  2131. if(nread == SSH_AGAIN) {
  2132. *err = CURLE_AGAIN;
  2133. nread = -1;
  2134. }
  2135. #endif
  2136. return nread;
  2137. }
  2138. /*
  2139. * =============== SFTP ===============
  2140. */
  2141. /*
  2142. ***********************************************************************
  2143. *
  2144. * sftp_perform()
  2145. *
  2146. * This is the actual DO function for SFTP. Get a file/directory according to
  2147. * the options previously setup.
  2148. */
  2149. static
  2150. CURLcode sftp_perform(struct Curl_easy *data,
  2151. bool *connected,
  2152. bool *dophase_done)
  2153. {
  2154. CURLcode result = CURLE_OK;
  2155. DEBUGF(infof(data, "DO phase starts"));
  2156. *dophase_done = FALSE; /* not done yet */
  2157. /* start the first command in the DO phase */
  2158. state(data, SSH_SFTP_QUOTE_INIT);
  2159. /* run the state-machine */
  2160. result = myssh_multi_statemach(data, dophase_done);
  2161. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  2162. if(*dophase_done) {
  2163. DEBUGF(infof(data, "DO phase is complete"));
  2164. }
  2165. return result;
  2166. }
  2167. /* called from multi.c while DOing */
  2168. static CURLcode sftp_doing(struct Curl_easy *data,
  2169. bool *dophase_done)
  2170. {
  2171. CURLcode result = myssh_multi_statemach(data, dophase_done);
  2172. if(*dophase_done) {
  2173. DEBUGF(infof(data, "DO phase is complete"));
  2174. }
  2175. return result;
  2176. }
  2177. /* BLOCKING, but the function is using the state machine so the only reason
  2178. this is still blocking is that the multi interface code has no support for
  2179. disconnecting operations that takes a while */
  2180. static CURLcode sftp_disconnect(struct Curl_easy *data,
  2181. struct connectdata *conn,
  2182. bool dead_connection)
  2183. {
  2184. CURLcode result = CURLE_OK;
  2185. (void) dead_connection;
  2186. DEBUGF(infof(data, "SSH DISCONNECT starts now"));
  2187. if(conn->proto.sshc.ssh_session) {
  2188. /* only if there's a session still around to use! */
  2189. state(data, SSH_SFTP_SHUTDOWN);
  2190. result = myssh_block_statemach(data, TRUE);
  2191. }
  2192. DEBUGF(infof(data, "SSH DISCONNECT is done"));
  2193. return result;
  2194. }
  2195. static CURLcode sftp_done(struct Curl_easy *data, CURLcode status,
  2196. bool premature)
  2197. {
  2198. struct connectdata *conn = data->conn;
  2199. struct ssh_conn *sshc = &conn->proto.sshc;
  2200. if(!status) {
  2201. /* Post quote commands are executed after the SFTP_CLOSE state to avoid
  2202. errors that could happen due to open file handles during POSTQUOTE
  2203. operation */
  2204. if(!premature && data->set.postquote && !conn->bits.retry)
  2205. sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT;
  2206. state(data, SSH_SFTP_CLOSE);
  2207. }
  2208. return myssh_done(data, status);
  2209. }
  2210. /* return number of sent bytes */
  2211. static ssize_t sftp_send(struct Curl_easy *data, int sockindex,
  2212. const void *mem, size_t len, CURLcode *err)
  2213. {
  2214. ssize_t nwrite;
  2215. struct connectdata *conn = data->conn;
  2216. (void)sockindex;
  2217. nwrite = sftp_write(conn->proto.sshc.sftp_file, mem, len);
  2218. myssh_block2waitfor(conn, FALSE);
  2219. #if 0 /* not returned by libssh on write */
  2220. if(nwrite == SSH_AGAIN) {
  2221. *err = CURLE_AGAIN;
  2222. nwrite = 0;
  2223. }
  2224. else
  2225. #endif
  2226. if(nwrite < 0) {
  2227. *err = CURLE_SSH;
  2228. nwrite = -1;
  2229. }
  2230. return nwrite;
  2231. }
  2232. /*
  2233. * Return number of received (decrypted) bytes
  2234. * or <0 on error
  2235. */
  2236. static ssize_t sftp_recv(struct Curl_easy *data, int sockindex,
  2237. char *mem, size_t len, CURLcode *err)
  2238. {
  2239. ssize_t nread;
  2240. struct connectdata *conn = data->conn;
  2241. (void)sockindex;
  2242. DEBUGASSERT(len < CURL_MAX_READ_SIZE);
  2243. switch(conn->proto.sshc.sftp_recv_state) {
  2244. case 0:
  2245. conn->proto.sshc.sftp_file_index =
  2246. sftp_async_read_begin(conn->proto.sshc.sftp_file,
  2247. (uint32_t)len);
  2248. if(conn->proto.sshc.sftp_file_index < 0) {
  2249. *err = CURLE_RECV_ERROR;
  2250. return -1;
  2251. }
  2252. /* FALLTHROUGH */
  2253. case 1:
  2254. conn->proto.sshc.sftp_recv_state = 1;
  2255. nread = sftp_async_read(conn->proto.sshc.sftp_file,
  2256. mem, (uint32_t)len,
  2257. conn->proto.sshc.sftp_file_index);
  2258. myssh_block2waitfor(conn, (nread == SSH_AGAIN)?TRUE:FALSE);
  2259. if(nread == SSH_AGAIN) {
  2260. *err = CURLE_AGAIN;
  2261. return -1;
  2262. }
  2263. else if(nread < 0) {
  2264. *err = CURLE_RECV_ERROR;
  2265. return -1;
  2266. }
  2267. conn->proto.sshc.sftp_recv_state = 0;
  2268. return nread;
  2269. default:
  2270. /* we never reach here */
  2271. return -1;
  2272. }
  2273. }
  2274. static void sftp_quote(struct Curl_easy *data)
  2275. {
  2276. const char *cp;
  2277. struct connectdata *conn = data->conn;
  2278. struct SSHPROTO *protop = data->req.p.ssh;
  2279. struct ssh_conn *sshc = &conn->proto.sshc;
  2280. CURLcode result;
  2281. /*
  2282. * Support some of the "FTP" commands
  2283. */
  2284. char *cmd = sshc->quote_item->data;
  2285. sshc->acceptfail = FALSE;
  2286. /* if a command starts with an asterisk, which a legal SFTP command never
  2287. can, the command will be allowed to fail without it causing any
  2288. aborts or cancels etc. It will cause libcurl to act as if the command
  2289. is successful, whatever the server reponds. */
  2290. if(cmd[0] == '*') {
  2291. cmd++;
  2292. sshc->acceptfail = TRUE;
  2293. }
  2294. if(strcasecompare("pwd", cmd)) {
  2295. /* output debug output if that is requested */
  2296. char *tmp = aprintf("257 \"%s\" is current directory.\n",
  2297. protop->path);
  2298. if(!tmp) {
  2299. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  2300. state(data, SSH_SFTP_CLOSE);
  2301. sshc->nextstate = SSH_NO_STATE;
  2302. return;
  2303. }
  2304. Curl_debug(data, CURLINFO_HEADER_OUT, (char *) "PWD\n", 4);
  2305. Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp));
  2306. /* this sends an FTP-like "header" to the header callback so that the
  2307. current directory can be read very similar to how it is read when
  2308. using ordinary FTP. */
  2309. result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
  2310. free(tmp);
  2311. if(result) {
  2312. state(data, SSH_SFTP_CLOSE);
  2313. sshc->nextstate = SSH_NO_STATE;
  2314. sshc->actualcode = result;
  2315. }
  2316. else
  2317. state(data, SSH_SFTP_NEXT_QUOTE);
  2318. return;
  2319. }
  2320. /*
  2321. * the arguments following the command must be separated from the
  2322. * command with a space so we can check for it unconditionally
  2323. */
  2324. cp = strchr(cmd, ' ');
  2325. if(!cp) {
  2326. failf(data, "Syntax error in SFTP command. Supply parameter(s)");
  2327. state(data, SSH_SFTP_CLOSE);
  2328. sshc->nextstate = SSH_NO_STATE;
  2329. sshc->actualcode = CURLE_QUOTE_ERROR;
  2330. return;
  2331. }
  2332. /*
  2333. * also, every command takes at least one argument so we get that
  2334. * first argument right now
  2335. */
  2336. result = Curl_get_pathname(&cp, &sshc->quote_path1, sshc->homedir);
  2337. if(result) {
  2338. if(result == CURLE_OUT_OF_MEMORY)
  2339. failf(data, "Out of memory");
  2340. else
  2341. failf(data, "Syntax error: Bad first parameter");
  2342. state(data, SSH_SFTP_CLOSE);
  2343. sshc->nextstate = SSH_NO_STATE;
  2344. sshc->actualcode = result;
  2345. return;
  2346. }
  2347. /*
  2348. * SFTP is a binary protocol, so we don't send text commands
  2349. * to the server. Instead, we scan for commands used by
  2350. * OpenSSH's sftp program and call the appropriate libssh
  2351. * functions.
  2352. */
  2353. if(strncasecompare(cmd, "chgrp ", 6) ||
  2354. strncasecompare(cmd, "chmod ", 6) ||
  2355. strncasecompare(cmd, "chown ", 6) ||
  2356. strncasecompare(cmd, "atime ", 6) ||
  2357. strncasecompare(cmd, "mtime ", 6)) {
  2358. /* attribute change */
  2359. /* sshc->quote_path1 contains the mode to set */
  2360. /* get the destination */
  2361. result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
  2362. if(result) {
  2363. if(result == CURLE_OUT_OF_MEMORY)
  2364. failf(data, "Out of memory");
  2365. else
  2366. failf(data, "Syntax error in chgrp/chmod/chown/atime/mtime: "
  2367. "Bad second parameter");
  2368. Curl_safefree(sshc->quote_path1);
  2369. state(data, SSH_SFTP_CLOSE);
  2370. sshc->nextstate = SSH_NO_STATE;
  2371. sshc->actualcode = result;
  2372. return;
  2373. }
  2374. sshc->quote_attrs = NULL;
  2375. state(data, SSH_SFTP_QUOTE_STAT);
  2376. return;
  2377. }
  2378. if(strncasecompare(cmd, "ln ", 3) ||
  2379. strncasecompare(cmd, "symlink ", 8)) {
  2380. /* symbolic linking */
  2381. /* sshc->quote_path1 is the source */
  2382. /* get the destination */
  2383. result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
  2384. if(result) {
  2385. if(result == CURLE_OUT_OF_MEMORY)
  2386. failf(data, "Out of memory");
  2387. else
  2388. failf(data, "Syntax error in ln/symlink: Bad second parameter");
  2389. Curl_safefree(sshc->quote_path1);
  2390. state(data, SSH_SFTP_CLOSE);
  2391. sshc->nextstate = SSH_NO_STATE;
  2392. sshc->actualcode = result;
  2393. return;
  2394. }
  2395. state(data, SSH_SFTP_QUOTE_SYMLINK);
  2396. return;
  2397. }
  2398. else if(strncasecompare(cmd, "mkdir ", 6)) {
  2399. /* create dir */
  2400. state(data, SSH_SFTP_QUOTE_MKDIR);
  2401. return;
  2402. }
  2403. else if(strncasecompare(cmd, "rename ", 7)) {
  2404. /* rename file */
  2405. /* first param is the source path */
  2406. /* second param is the dest. path */
  2407. result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
  2408. if(result) {
  2409. if(result == CURLE_OUT_OF_MEMORY)
  2410. failf(data, "Out of memory");
  2411. else
  2412. failf(data, "Syntax error in rename: Bad second parameter");
  2413. Curl_safefree(sshc->quote_path1);
  2414. state(data, SSH_SFTP_CLOSE);
  2415. sshc->nextstate = SSH_NO_STATE;
  2416. sshc->actualcode = result;
  2417. return;
  2418. }
  2419. state(data, SSH_SFTP_QUOTE_RENAME);
  2420. return;
  2421. }
  2422. else if(strncasecompare(cmd, "rmdir ", 6)) {
  2423. /* delete dir */
  2424. state(data, SSH_SFTP_QUOTE_RMDIR);
  2425. return;
  2426. }
  2427. else if(strncasecompare(cmd, "rm ", 3)) {
  2428. state(data, SSH_SFTP_QUOTE_UNLINK);
  2429. return;
  2430. }
  2431. #ifdef HAS_STATVFS_SUPPORT
  2432. else if(strncasecompare(cmd, "statvfs ", 8)) {
  2433. state(data, SSH_SFTP_QUOTE_STATVFS);
  2434. return;
  2435. }
  2436. #endif
  2437. failf(data, "Unknown SFTP command");
  2438. Curl_safefree(sshc->quote_path1);
  2439. Curl_safefree(sshc->quote_path2);
  2440. state(data, SSH_SFTP_CLOSE);
  2441. sshc->nextstate = SSH_NO_STATE;
  2442. sshc->actualcode = CURLE_QUOTE_ERROR;
  2443. }
  2444. static void sftp_quote_stat(struct Curl_easy *data)
  2445. {
  2446. struct connectdata *conn = data->conn;
  2447. struct ssh_conn *sshc = &conn->proto.sshc;
  2448. char *cmd = sshc->quote_item->data;
  2449. sshc->acceptfail = FALSE;
  2450. /* if a command starts with an asterisk, which a legal SFTP command never
  2451. can, the command will be allowed to fail without it causing any
  2452. aborts or cancels etc. It will cause libcurl to act as if the command
  2453. is successful, whatever the server reponds. */
  2454. if(cmd[0] == '*') {
  2455. cmd++;
  2456. sshc->acceptfail = TRUE;
  2457. }
  2458. /* We read the file attributes, store them in sshc->quote_attrs
  2459. * and modify them accordingly to command. Then we switch to
  2460. * QUOTE_SETSTAT state to write new ones.
  2461. */
  2462. if(sshc->quote_attrs)
  2463. sftp_attributes_free(sshc->quote_attrs);
  2464. sshc->quote_attrs = sftp_stat(sshc->sftp_session, sshc->quote_path2);
  2465. if(!sshc->quote_attrs) {
  2466. Curl_safefree(sshc->quote_path1);
  2467. Curl_safefree(sshc->quote_path2);
  2468. failf(data, "Attempt to get SFTP stats failed: %d",
  2469. sftp_get_error(sshc->sftp_session));
  2470. state(data, SSH_SFTP_CLOSE);
  2471. sshc->nextstate = SSH_NO_STATE;
  2472. sshc->actualcode = CURLE_QUOTE_ERROR;
  2473. return;
  2474. }
  2475. /* Now set the new attributes... */
  2476. if(strncasecompare(cmd, "chgrp", 5)) {
  2477. sshc->quote_attrs->gid = (uint32_t)strtoul(sshc->quote_path1, NULL, 10);
  2478. if(sshc->quote_attrs->gid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
  2479. !sshc->acceptfail) {
  2480. Curl_safefree(sshc->quote_path1);
  2481. Curl_safefree(sshc->quote_path2);
  2482. failf(data, "Syntax error: chgrp gid not a number");
  2483. state(data, SSH_SFTP_CLOSE);
  2484. sshc->nextstate = SSH_NO_STATE;
  2485. sshc->actualcode = CURLE_QUOTE_ERROR;
  2486. return;
  2487. }
  2488. sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_UIDGID;
  2489. }
  2490. else if(strncasecompare(cmd, "chmod", 5)) {
  2491. mode_t perms;
  2492. perms = (mode_t)strtoul(sshc->quote_path1, NULL, 8);
  2493. /* permissions are octal */
  2494. if(perms == 0 && !ISDIGIT(sshc->quote_path1[0])) {
  2495. Curl_safefree(sshc->quote_path1);
  2496. Curl_safefree(sshc->quote_path2);
  2497. failf(data, "Syntax error: chmod permissions not a number");
  2498. state(data, SSH_SFTP_CLOSE);
  2499. sshc->nextstate = SSH_NO_STATE;
  2500. sshc->actualcode = CURLE_QUOTE_ERROR;
  2501. return;
  2502. }
  2503. sshc->quote_attrs->permissions = perms;
  2504. sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_PERMISSIONS;
  2505. }
  2506. else if(strncasecompare(cmd, "chown", 5)) {
  2507. sshc->quote_attrs->uid = (uint32_t)strtoul(sshc->quote_path1, NULL, 10);
  2508. if(sshc->quote_attrs->uid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
  2509. !sshc->acceptfail) {
  2510. Curl_safefree(sshc->quote_path1);
  2511. Curl_safefree(sshc->quote_path2);
  2512. failf(data, "Syntax error: chown uid not a number");
  2513. state(data, SSH_SFTP_CLOSE);
  2514. sshc->nextstate = SSH_NO_STATE;
  2515. sshc->actualcode = CURLE_QUOTE_ERROR;
  2516. return;
  2517. }
  2518. sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_UIDGID;
  2519. }
  2520. else if(strncasecompare(cmd, "atime", 5) ||
  2521. strncasecompare(cmd, "mtime", 5)) {
  2522. time_t date = Curl_getdate_capped(sshc->quote_path1);
  2523. bool fail = FALSE;
  2524. if(date == -1) {
  2525. failf(data, "incorrect date format for %.*s", 5, cmd);
  2526. fail = TRUE;
  2527. }
  2528. #if SIZEOF_TIME_T > 4
  2529. else if(date > 0xffffffff) {
  2530. failf(data, "date overflow");
  2531. fail = TRUE; /* avoid setting a capped time */
  2532. }
  2533. #endif
  2534. if(fail) {
  2535. Curl_safefree(sshc->quote_path1);
  2536. Curl_safefree(sshc->quote_path2);
  2537. state(data, SSH_SFTP_CLOSE);
  2538. sshc->nextstate = SSH_NO_STATE;
  2539. sshc->actualcode = CURLE_QUOTE_ERROR;
  2540. return;
  2541. }
  2542. if(strncasecompare(cmd, "atime", 5))
  2543. sshc->quote_attrs->atime = (uint32_t)date;
  2544. else /* mtime */
  2545. sshc->quote_attrs->mtime = (uint32_t)date;
  2546. sshc->quote_attrs->flags |= SSH_FILEXFER_ATTR_ACMODTIME;
  2547. }
  2548. /* Now send the completed structure... */
  2549. state(data, SSH_SFTP_QUOTE_SETSTAT);
  2550. return;
  2551. }
  2552. CURLcode Curl_ssh_init(void)
  2553. {
  2554. if(ssh_init()) {
  2555. DEBUGF(fprintf(stderr, "Error: libssh_init failed\n"));
  2556. return CURLE_FAILED_INIT;
  2557. }
  2558. return CURLE_OK;
  2559. }
  2560. void Curl_ssh_cleanup(void)
  2561. {
  2562. (void)ssh_finalize();
  2563. }
  2564. void Curl_ssh_version(char *buffer, size_t buflen)
  2565. {
  2566. (void)msnprintf(buffer, buflen, "libssh/%s", ssh_version(0));
  2567. }
  2568. #endif /* USE_LIBSSH */