libssh.c 86 KB

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