libssh.c 86 KB

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