libssh.c 87 KB

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