libssh.c 95 KB

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