2
0

libssh.c 86 KB

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