libssh.c 86 KB

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