libssh.c 87 KB

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