libssh2.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* #define CURL_LIBSSH2_DEBUG */
  23. #include "curl_setup.h"
  24. #ifdef USE_LIBSSH2
  25. #include <limits.h>
  26. #include <libssh2.h>
  27. #include <libssh2_sftp.h>
  28. #ifdef HAVE_FCNTL_H
  29. #include <fcntl.h>
  30. #endif
  31. #ifdef HAVE_NETINET_IN_H
  32. #include <netinet/in.h>
  33. #endif
  34. #ifdef HAVE_ARPA_INET_H
  35. #include <arpa/inet.h>
  36. #endif
  37. #ifdef HAVE_UTSNAME_H
  38. #include <sys/utsname.h>
  39. #endif
  40. #ifdef HAVE_NETDB_H
  41. #include <netdb.h>
  42. #endif
  43. #ifdef __VMS
  44. #include <in.h>
  45. #include <inet.h>
  46. #endif
  47. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  48. #undef in_addr_t
  49. #define in_addr_t unsigned long
  50. #endif
  51. #include <curl/curl.h>
  52. #include "urldata.h"
  53. #include "sendf.h"
  54. #include "hostip.h"
  55. #include "progress.h"
  56. #include "transfer.h"
  57. #include "escape.h"
  58. #include "http.h" /* for HTTP proxy tunnel stuff */
  59. #include "ssh.h"
  60. #include "url.h"
  61. #include "speedcheck.h"
  62. #include "getinfo.h"
  63. #include "strdup.h"
  64. #include "strcase.h"
  65. #include "vtls/vtls.h"
  66. #include "connect.h"
  67. #include "strerror.h"
  68. #include "inet_ntop.h"
  69. #include "parsedate.h" /* for the week day and month names */
  70. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  71. #include "strtoofft.h"
  72. #include "multiif.h"
  73. #include "select.h"
  74. #include "warnless.h"
  75. #include "curl_path.h"
  76. /* The last 3 #include files should be in this order */
  77. #include "curl_printf.h"
  78. #include "curl_memory.h"
  79. #include "memdebug.h"
  80. #if LIBSSH2_VERSION_NUM >= 0x010206
  81. /* libssh2_sftp_statvfs and friends were added in 1.2.6 */
  82. #define HAS_STATVFS_SUPPORT 1
  83. #endif
  84. #define sftp_libssh2_realpath(s,p,t,m) \
  85. libssh2_sftp_symlink_ex((s), (p), curlx_uztoui(strlen(p)), \
  86. (t), (m), LIBSSH2_SFTP_REALPATH)
  87. /* Local functions: */
  88. static const char *sftp_libssh2_strerror(unsigned long err);
  89. static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc);
  90. static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc);
  91. static LIBSSH2_FREE_FUNC(my_libssh2_free);
  92. static CURLcode ssh_force_knownhost_key_type(struct connectdata *conn);
  93. static CURLcode ssh_connect(struct connectdata *conn, bool *done);
  94. static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done);
  95. static CURLcode ssh_do(struct connectdata *conn, bool *done);
  96. static CURLcode scp_done(struct connectdata *conn,
  97. CURLcode, bool premature);
  98. static CURLcode scp_doing(struct connectdata *conn,
  99. bool *dophase_done);
  100. static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection);
  101. static CURLcode sftp_done(struct connectdata *conn,
  102. CURLcode, bool premature);
  103. static CURLcode sftp_doing(struct connectdata *conn,
  104. bool *dophase_done);
  105. static CURLcode sftp_disconnect(struct connectdata *conn, bool dead);
  106. static
  107. CURLcode sftp_perform(struct connectdata *conn,
  108. bool *connected,
  109. bool *dophase_done);
  110. static int ssh_getsock(struct connectdata *conn, curl_socket_t *sock);
  111. static int ssh_perform_getsock(const struct connectdata *conn,
  112. curl_socket_t *sock);
  113. static CURLcode ssh_setup_connection(struct connectdata *conn);
  114. /*
  115. * SCP protocol handler.
  116. */
  117. const struct Curl_handler Curl_handler_scp = {
  118. "SCP", /* scheme */
  119. ssh_setup_connection, /* setup_connection */
  120. ssh_do, /* do_it */
  121. scp_done, /* done */
  122. ZERO_NULL, /* do_more */
  123. ssh_connect, /* connect_it */
  124. ssh_multi_statemach, /* connecting */
  125. scp_doing, /* doing */
  126. ssh_getsock, /* proto_getsock */
  127. ssh_getsock, /* doing_getsock */
  128. ZERO_NULL, /* domore_getsock */
  129. ssh_perform_getsock, /* perform_getsock */
  130. scp_disconnect, /* disconnect */
  131. ZERO_NULL, /* readwrite */
  132. ZERO_NULL, /* connection_check */
  133. PORT_SSH, /* defport */
  134. CURLPROTO_SCP, /* protocol */
  135. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
  136. | PROTOPT_NOURLQUERY /* flags */
  137. };
  138. /*
  139. * SFTP protocol handler.
  140. */
  141. const struct Curl_handler Curl_handler_sftp = {
  142. "SFTP", /* scheme */
  143. ssh_setup_connection, /* setup_connection */
  144. ssh_do, /* do_it */
  145. sftp_done, /* done */
  146. ZERO_NULL, /* do_more */
  147. ssh_connect, /* connect_it */
  148. ssh_multi_statemach, /* connecting */
  149. sftp_doing, /* doing */
  150. ssh_getsock, /* proto_getsock */
  151. ssh_getsock, /* doing_getsock */
  152. ZERO_NULL, /* domore_getsock */
  153. ssh_perform_getsock, /* perform_getsock */
  154. sftp_disconnect, /* disconnect */
  155. ZERO_NULL, /* readwrite */
  156. ZERO_NULL, /* connection_check */
  157. PORT_SSH, /* defport */
  158. CURLPROTO_SFTP, /* protocol */
  159. PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
  160. | PROTOPT_NOURLQUERY /* flags */
  161. };
  162. static void
  163. kbd_callback(const char *name, int name_len, const char *instruction,
  164. int instruction_len, int num_prompts,
  165. const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
  166. LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
  167. void **abstract)
  168. {
  169. struct connectdata *conn = (struct connectdata *)*abstract;
  170. #ifdef CURL_LIBSSH2_DEBUG
  171. fprintf(stderr, "name=%s\n", name);
  172. fprintf(stderr, "name_len=%d\n", name_len);
  173. fprintf(stderr, "instruction=%s\n", instruction);
  174. fprintf(stderr, "instruction_len=%d\n", instruction_len);
  175. fprintf(stderr, "num_prompts=%d\n", num_prompts);
  176. #else
  177. (void)name;
  178. (void)name_len;
  179. (void)instruction;
  180. (void)instruction_len;
  181. #endif /* CURL_LIBSSH2_DEBUG */
  182. if(num_prompts == 1) {
  183. responses[0].text = strdup(conn->passwd);
  184. responses[0].length = curlx_uztoui(strlen(conn->passwd));
  185. }
  186. (void)prompts;
  187. (void)abstract;
  188. } /* kbd_callback */
  189. static CURLcode sftp_libssh2_error_to_CURLE(unsigned long err)
  190. {
  191. switch(err) {
  192. case LIBSSH2_FX_OK:
  193. return CURLE_OK;
  194. case LIBSSH2_FX_NO_SUCH_FILE:
  195. case LIBSSH2_FX_NO_SUCH_PATH:
  196. return CURLE_REMOTE_FILE_NOT_FOUND;
  197. case LIBSSH2_FX_PERMISSION_DENIED:
  198. case LIBSSH2_FX_WRITE_PROTECT:
  199. case LIBSSH2_FX_LOCK_CONFlICT:
  200. return CURLE_REMOTE_ACCESS_DENIED;
  201. case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
  202. case LIBSSH2_FX_QUOTA_EXCEEDED:
  203. return CURLE_REMOTE_DISK_FULL;
  204. case LIBSSH2_FX_FILE_ALREADY_EXISTS:
  205. return CURLE_REMOTE_FILE_EXISTS;
  206. case LIBSSH2_FX_DIR_NOT_EMPTY:
  207. return CURLE_QUOTE_ERROR;
  208. default:
  209. break;
  210. }
  211. return CURLE_SSH;
  212. }
  213. static CURLcode libssh2_session_error_to_CURLE(int err)
  214. {
  215. switch(err) {
  216. /* Ordered by order of appearance in libssh2.h */
  217. case LIBSSH2_ERROR_NONE:
  218. return CURLE_OK;
  219. /* This is the error returned by libssh2_scp_recv2
  220. * on unknown file */
  221. case LIBSSH2_ERROR_SCP_PROTOCOL:
  222. return CURLE_REMOTE_FILE_NOT_FOUND;
  223. case LIBSSH2_ERROR_SOCKET_NONE:
  224. return CURLE_COULDNT_CONNECT;
  225. case LIBSSH2_ERROR_ALLOC:
  226. return CURLE_OUT_OF_MEMORY;
  227. case LIBSSH2_ERROR_SOCKET_SEND:
  228. return CURLE_SEND_ERROR;
  229. case LIBSSH2_ERROR_HOSTKEY_INIT:
  230. case LIBSSH2_ERROR_HOSTKEY_SIGN:
  231. case LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED:
  232. case LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED:
  233. return CURLE_PEER_FAILED_VERIFICATION;
  234. case LIBSSH2_ERROR_PASSWORD_EXPIRED:
  235. return CURLE_LOGIN_DENIED;
  236. case LIBSSH2_ERROR_SOCKET_TIMEOUT:
  237. case LIBSSH2_ERROR_TIMEOUT:
  238. return CURLE_OPERATION_TIMEDOUT;
  239. case LIBSSH2_ERROR_EAGAIN:
  240. return CURLE_AGAIN;
  241. }
  242. return CURLE_SSH;
  243. }
  244. static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc)
  245. {
  246. (void)abstract; /* arg not used */
  247. return malloc(count);
  248. }
  249. static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc)
  250. {
  251. (void)abstract; /* arg not used */
  252. return realloc(ptr, count);
  253. }
  254. static LIBSSH2_FREE_FUNC(my_libssh2_free)
  255. {
  256. (void)abstract; /* arg not used */
  257. if(ptr) /* ssh2 agent sometimes call free with null ptr */
  258. free(ptr);
  259. }
  260. /*
  261. * SSH State machine related code
  262. */
  263. /* This is the ONLY way to change SSH state! */
  264. static void state(struct connectdata *conn, sshstate nowstate)
  265. {
  266. struct ssh_conn *sshc = &conn->proto.sshc;
  267. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  268. /* for debug purposes */
  269. static const char * const names[] = {
  270. "SSH_STOP",
  271. "SSH_INIT",
  272. "SSH_S_STARTUP",
  273. "SSH_HOSTKEY",
  274. "SSH_AUTHLIST",
  275. "SSH_AUTH_PKEY_INIT",
  276. "SSH_AUTH_PKEY",
  277. "SSH_AUTH_PASS_INIT",
  278. "SSH_AUTH_PASS",
  279. "SSH_AUTH_AGENT_INIT",
  280. "SSH_AUTH_AGENT_LIST",
  281. "SSH_AUTH_AGENT",
  282. "SSH_AUTH_HOST_INIT",
  283. "SSH_AUTH_HOST",
  284. "SSH_AUTH_KEY_INIT",
  285. "SSH_AUTH_KEY",
  286. "SSH_AUTH_GSSAPI",
  287. "SSH_AUTH_DONE",
  288. "SSH_SFTP_INIT",
  289. "SSH_SFTP_REALPATH",
  290. "SSH_SFTP_QUOTE_INIT",
  291. "SSH_SFTP_POSTQUOTE_INIT",
  292. "SSH_SFTP_QUOTE",
  293. "SSH_SFTP_NEXT_QUOTE",
  294. "SSH_SFTP_QUOTE_STAT",
  295. "SSH_SFTP_QUOTE_SETSTAT",
  296. "SSH_SFTP_QUOTE_SYMLINK",
  297. "SSH_SFTP_QUOTE_MKDIR",
  298. "SSH_SFTP_QUOTE_RENAME",
  299. "SSH_SFTP_QUOTE_RMDIR",
  300. "SSH_SFTP_QUOTE_UNLINK",
  301. "SSH_SFTP_QUOTE_STATVFS",
  302. "SSH_SFTP_GETINFO",
  303. "SSH_SFTP_FILETIME",
  304. "SSH_SFTP_TRANS_INIT",
  305. "SSH_SFTP_UPLOAD_INIT",
  306. "SSH_SFTP_CREATE_DIRS_INIT",
  307. "SSH_SFTP_CREATE_DIRS",
  308. "SSH_SFTP_CREATE_DIRS_MKDIR",
  309. "SSH_SFTP_READDIR_INIT",
  310. "SSH_SFTP_READDIR",
  311. "SSH_SFTP_READDIR_LINK",
  312. "SSH_SFTP_READDIR_BOTTOM",
  313. "SSH_SFTP_READDIR_DONE",
  314. "SSH_SFTP_DOWNLOAD_INIT",
  315. "SSH_SFTP_DOWNLOAD_STAT",
  316. "SSH_SFTP_CLOSE",
  317. "SSH_SFTP_SHUTDOWN",
  318. "SSH_SCP_TRANS_INIT",
  319. "SSH_SCP_UPLOAD_INIT",
  320. "SSH_SCP_DOWNLOAD_INIT",
  321. "SSH_SCP_DOWNLOAD",
  322. "SSH_SCP_DONE",
  323. "SSH_SCP_SEND_EOF",
  324. "SSH_SCP_WAIT_EOF",
  325. "SSH_SCP_WAIT_CLOSE",
  326. "SSH_SCP_CHANNEL_FREE",
  327. "SSH_SESSION_DISCONNECT",
  328. "SSH_SESSION_FREE",
  329. "QUIT"
  330. };
  331. /* a precaution to make sure the lists are in sync */
  332. DEBUGASSERT(sizeof(names)/sizeof(names[0]) == SSH_LAST);
  333. if(sshc->state != nowstate) {
  334. infof(conn->data, "SFTP %p state change from %s to %s\n",
  335. (void *)sshc, names[sshc->state], names[nowstate]);
  336. }
  337. #endif
  338. sshc->state = nowstate;
  339. }
  340. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  341. static int sshkeycallback(struct Curl_easy *easy,
  342. const struct curl_khkey *knownkey, /* known */
  343. const struct curl_khkey *foundkey, /* found */
  344. enum curl_khmatch match,
  345. void *clientp)
  346. {
  347. (void)easy;
  348. (void)knownkey;
  349. (void)foundkey;
  350. (void)clientp;
  351. /* we only allow perfect matches, and we reject everything else */
  352. return (match != CURLKHMATCH_OK)?CURLKHSTAT_REJECT:CURLKHSTAT_FINE;
  353. }
  354. #endif
  355. /*
  356. * Earlier libssh2 versions didn't have the ability to seek to 64bit positions
  357. * with 32bit size_t.
  358. */
  359. #ifdef HAVE_LIBSSH2_SFTP_SEEK64
  360. #define SFTP_SEEK(x,y) libssh2_sftp_seek64(x, (libssh2_uint64_t)y)
  361. #else
  362. #define SFTP_SEEK(x,y) libssh2_sftp_seek(x, (size_t)y)
  363. #endif
  364. /*
  365. * Earlier libssh2 versions didn't do SCP properly beyond 32bit sizes on 32bit
  366. * architectures so we check of the necessary function is present.
  367. */
  368. #ifndef HAVE_LIBSSH2_SCP_SEND64
  369. #define SCP_SEND(a,b,c,d) libssh2_scp_send_ex(a, b, (int)(c), (size_t)d, 0, 0)
  370. #else
  371. #define SCP_SEND(a,b,c,d) libssh2_scp_send64(a, b, (int)(c), \
  372. (libssh2_uint64_t)d, 0, 0)
  373. #endif
  374. /*
  375. * libssh2 1.2.8 fixed the problem with 32bit ints used for sockets on win64.
  376. */
  377. #ifdef HAVE_LIBSSH2_SESSION_HANDSHAKE
  378. #define libssh2_session_startup(x,y) libssh2_session_handshake(x,y)
  379. #endif
  380. static CURLcode ssh_knownhost(struct connectdata *conn)
  381. {
  382. CURLcode result = CURLE_OK;
  383. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  384. struct Curl_easy *data = conn->data;
  385. if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
  386. /* we're asked to verify the host against a file */
  387. struct ssh_conn *sshc = &conn->proto.sshc;
  388. int rc;
  389. int keytype;
  390. size_t keylen;
  391. const char *remotekey = libssh2_session_hostkey(sshc->ssh_session,
  392. &keylen, &keytype);
  393. int keycheck = LIBSSH2_KNOWNHOST_CHECK_FAILURE;
  394. int keybit = 0;
  395. if(remotekey) {
  396. /*
  397. * A subject to figure out is what host name we need to pass in here.
  398. * What host name does OpenSSH store in its file if an IDN name is
  399. * used?
  400. */
  401. struct libssh2_knownhost *host;
  402. enum curl_khmatch keymatch;
  403. curl_sshkeycallback func =
  404. data->set.ssh_keyfunc?data->set.ssh_keyfunc:sshkeycallback;
  405. struct curl_khkey knownkey;
  406. struct curl_khkey *knownkeyp = NULL;
  407. struct curl_khkey foundkey;
  408. switch(keytype) {
  409. case LIBSSH2_HOSTKEY_TYPE_RSA:
  410. keybit = LIBSSH2_KNOWNHOST_KEY_SSHRSA;
  411. break;
  412. case LIBSSH2_HOSTKEY_TYPE_DSS:
  413. keybit = LIBSSH2_KNOWNHOST_KEY_SSHDSS;
  414. break;
  415. #ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_256
  416. case LIBSSH2_HOSTKEY_TYPE_ECDSA_256:
  417. keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_256;
  418. break;
  419. #endif
  420. #ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_384
  421. case LIBSSH2_HOSTKEY_TYPE_ECDSA_384:
  422. keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_384;
  423. break;
  424. #endif
  425. #ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_521
  426. case LIBSSH2_HOSTKEY_TYPE_ECDSA_521:
  427. keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_521;
  428. break;
  429. #endif
  430. #ifdef LIBSSH2_HOSTKEY_TYPE_ED25519
  431. case LIBSSH2_HOSTKEY_TYPE_ED25519:
  432. keybit = LIBSSH2_KNOWNHOST_KEY_ED25519;
  433. break;
  434. #endif
  435. default:
  436. infof(data, "unsupported key type, can't check knownhosts!\n");
  437. keybit = 0;
  438. break;
  439. }
  440. if(!keybit)
  441. /* no check means failure! */
  442. rc = CURLKHSTAT_REJECT;
  443. else {
  444. #ifdef HAVE_LIBSSH2_KNOWNHOST_CHECKP
  445. keycheck = libssh2_knownhost_checkp(sshc->kh,
  446. conn->host.name,
  447. (conn->remote_port != PORT_SSH)?
  448. conn->remote_port:-1,
  449. remotekey, keylen,
  450. LIBSSH2_KNOWNHOST_TYPE_PLAIN|
  451. LIBSSH2_KNOWNHOST_KEYENC_RAW|
  452. keybit,
  453. &host);
  454. #else
  455. keycheck = libssh2_knownhost_check(sshc->kh,
  456. conn->host.name,
  457. remotekey, keylen,
  458. LIBSSH2_KNOWNHOST_TYPE_PLAIN|
  459. LIBSSH2_KNOWNHOST_KEYENC_RAW|
  460. keybit,
  461. &host);
  462. #endif
  463. infof(data, "SSH host check: %d, key: %s\n", keycheck,
  464. (keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)?
  465. host->key:"<none>");
  466. /* setup 'knownkey' */
  467. if(keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH) {
  468. knownkey.key = host->key;
  469. knownkey.len = 0;
  470. knownkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
  471. CURLKHTYPE_RSA : CURLKHTYPE_DSS;
  472. knownkeyp = &knownkey;
  473. }
  474. /* setup 'foundkey' */
  475. foundkey.key = remotekey;
  476. foundkey.len = keylen;
  477. foundkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
  478. CURLKHTYPE_RSA : CURLKHTYPE_DSS;
  479. /*
  480. * if any of the LIBSSH2_KNOWNHOST_CHECK_* defines and the
  481. * curl_khmatch enum are ever modified, we need to introduce a
  482. * translation table here!
  483. */
  484. keymatch = (enum curl_khmatch)keycheck;
  485. /* Ask the callback how to behave */
  486. Curl_set_in_callback(data, true);
  487. rc = func(data, knownkeyp, /* from the knownhosts file */
  488. &foundkey, /* from the remote host */
  489. keymatch, data->set.ssh_keyfunc_userp);
  490. Curl_set_in_callback(data, false);
  491. }
  492. }
  493. else
  494. /* no remotekey means failure! */
  495. rc = CURLKHSTAT_REJECT;
  496. switch(rc) {
  497. default: /* unknown return codes will equal reject */
  498. /* FALLTHROUGH */
  499. case CURLKHSTAT_REJECT:
  500. state(conn, SSH_SESSION_FREE);
  501. /* FALLTHROUGH */
  502. case CURLKHSTAT_DEFER:
  503. /* DEFER means bail out but keep the SSH_HOSTKEY state */
  504. result = sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
  505. break;
  506. case CURLKHSTAT_FINE:
  507. case CURLKHSTAT_FINE_ADD_TO_FILE:
  508. /* proceed */
  509. if(keycheck != LIBSSH2_KNOWNHOST_CHECK_MATCH) {
  510. /* the found host+key didn't match but has been told to be fine
  511. anyway so we add it in memory */
  512. int addrc = libssh2_knownhost_add(sshc->kh,
  513. conn->host.name, NULL,
  514. remotekey, keylen,
  515. LIBSSH2_KNOWNHOST_TYPE_PLAIN|
  516. LIBSSH2_KNOWNHOST_KEYENC_RAW|
  517. keybit, NULL);
  518. if(addrc)
  519. infof(data, "Warning adding the known host %s failed!\n",
  520. conn->host.name);
  521. else if(rc == CURLKHSTAT_FINE_ADD_TO_FILE) {
  522. /* now we write the entire in-memory list of known hosts to the
  523. known_hosts file */
  524. int wrc =
  525. libssh2_knownhost_writefile(sshc->kh,
  526. data->set.str[STRING_SSH_KNOWNHOSTS],
  527. LIBSSH2_KNOWNHOST_FILE_OPENSSH);
  528. if(wrc) {
  529. infof(data, "Warning, writing %s failed!\n",
  530. data->set.str[STRING_SSH_KNOWNHOSTS]);
  531. }
  532. }
  533. }
  534. break;
  535. }
  536. }
  537. #else /* HAVE_LIBSSH2_KNOWNHOST_API */
  538. (void)conn;
  539. #endif
  540. return result;
  541. }
  542. static CURLcode ssh_check_fingerprint(struct connectdata *conn)
  543. {
  544. struct ssh_conn *sshc = &conn->proto.sshc;
  545. struct Curl_easy *data = conn->data;
  546. const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5];
  547. char md5buffer[33];
  548. const char *fingerprint = libssh2_hostkey_hash(sshc->ssh_session,
  549. LIBSSH2_HOSTKEY_HASH_MD5);
  550. if(fingerprint) {
  551. /* The fingerprint points to static storage (!), don't free() it. */
  552. int i;
  553. for(i = 0; i < 16; i++)
  554. msnprintf(&md5buffer[i*2], 3, "%02x", (unsigned char) fingerprint[i]);
  555. infof(data, "SSH MD5 fingerprint: %s\n", md5buffer);
  556. }
  557. /* Before we authenticate we check the hostkey's MD5 fingerprint
  558. * against a known fingerprint, if available.
  559. */
  560. if(pubkey_md5 && strlen(pubkey_md5) == 32) {
  561. if(!fingerprint || !strcasecompare(md5buffer, pubkey_md5)) {
  562. if(fingerprint)
  563. failf(data,
  564. "Denied establishing ssh session: mismatch md5 fingerprint. "
  565. "Remote %s is not equal to %s", md5buffer, pubkey_md5);
  566. else
  567. failf(data,
  568. "Denied establishing ssh session: md5 fingerprint not available");
  569. state(conn, SSH_SESSION_FREE);
  570. sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
  571. return sshc->actualcode;
  572. }
  573. infof(data, "MD5 checksum match!\n");
  574. /* as we already matched, we skip the check for known hosts */
  575. return CURLE_OK;
  576. }
  577. return ssh_knownhost(conn);
  578. }
  579. /*
  580. * ssh_force_knownhost_key_type() will check the known hosts file and try to
  581. * force a specific public key type from the server if an entry is found.
  582. */
  583. static CURLcode ssh_force_knownhost_key_type(struct connectdata *conn)
  584. {
  585. CURLcode result = CURLE_OK;
  586. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  587. #ifdef LIBSSH2_KNOWNHOST_KEY_ED25519
  588. static const char * const hostkey_method_ssh_ed25519
  589. = "ssh-ed25519";
  590. #endif
  591. #ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_521
  592. static const char * const hostkey_method_ssh_ecdsa_521
  593. = "ecdsa-sha2-nistp521";
  594. #endif
  595. #ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_384
  596. static const char * const hostkey_method_ssh_ecdsa_384
  597. = "ecdsa-sha2-nistp384";
  598. #endif
  599. #ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_256
  600. static const char * const hostkey_method_ssh_ecdsa_256
  601. = "ecdsa-sha2-nistp256";
  602. #endif
  603. static const char * const hostkey_method_ssh_rsa
  604. = "ssh-rsa";
  605. static const char * const hostkey_method_ssh_dss
  606. = "ssh-dss";
  607. const char *hostkey_method = NULL;
  608. struct ssh_conn *sshc = &conn->proto.sshc;
  609. struct Curl_easy *data = conn->data;
  610. struct libssh2_knownhost* store = NULL;
  611. const char *kh_name_end = NULL;
  612. size_t kh_name_size = 0;
  613. int port = 0;
  614. bool found = false;
  615. if(sshc->kh && !data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]) {
  616. /* lets try to find our host in the known hosts file */
  617. while(!libssh2_knownhost_get(sshc->kh, &store, store)) {
  618. /* For non-standard ports, the name will be enclosed in */
  619. /* square brackets, followed by a colon and the port */
  620. if(store) {
  621. if(store->name) {
  622. if(store->name[0] == '[') {
  623. kh_name_end = strstr(store->name, "]:");
  624. if(!kh_name_end) {
  625. infof(data, "Invalid host pattern %s in %s\n",
  626. store->name, data->set.str[STRING_SSH_KNOWNHOSTS]);
  627. continue;
  628. }
  629. port = atoi(kh_name_end + 2);
  630. if(kh_name_end && (port == conn->remote_port)) {
  631. kh_name_size = strlen(store->name) - 1 - strlen(kh_name_end);
  632. if(strncmp(store->name + 1,
  633. conn->host.name, kh_name_size) == 0) {
  634. found = true;
  635. break;
  636. }
  637. }
  638. }
  639. else if(strcmp(store->name, conn->host.name) == 0) {
  640. found = true;
  641. break;
  642. }
  643. }
  644. else {
  645. found = true;
  646. break;
  647. }
  648. }
  649. }
  650. if(found) {
  651. infof(data, "Found host %s in %s\n",
  652. conn->host.name, data->set.str[STRING_SSH_KNOWNHOSTS]);
  653. switch(store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) {
  654. #ifdef LIBSSH2_KNOWNHOST_KEY_ED25519
  655. case LIBSSH2_KNOWNHOST_KEY_ED25519:
  656. hostkey_method = hostkey_method_ssh_ed25519;
  657. break;
  658. #endif
  659. #ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_521
  660. case LIBSSH2_KNOWNHOST_KEY_ECDSA_521:
  661. hostkey_method = hostkey_method_ssh_ecdsa_521;
  662. break;
  663. #endif
  664. #ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_384
  665. case LIBSSH2_KNOWNHOST_KEY_ECDSA_384:
  666. hostkey_method = hostkey_method_ssh_ecdsa_384;
  667. break;
  668. #endif
  669. #ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_256
  670. case LIBSSH2_KNOWNHOST_KEY_ECDSA_256:
  671. hostkey_method = hostkey_method_ssh_ecdsa_256;
  672. break;
  673. #endif
  674. case LIBSSH2_KNOWNHOST_KEY_SSHRSA:
  675. hostkey_method = hostkey_method_ssh_rsa;
  676. break;
  677. case LIBSSH2_KNOWNHOST_KEY_SSHDSS:
  678. hostkey_method = hostkey_method_ssh_dss;
  679. break;
  680. case LIBSSH2_KNOWNHOST_KEY_RSA1:
  681. failf(data, "Found host key type RSA1 which is not supported\n");
  682. return CURLE_SSH;
  683. default:
  684. failf(data, "Unknown host key type: %i\n",
  685. (store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK));
  686. return CURLE_SSH;
  687. }
  688. infof(data, "Set \"%s\" as SSH hostkey type\n", hostkey_method);
  689. result = libssh2_session_error_to_CURLE(
  690. libssh2_session_method_pref(
  691. sshc->ssh_session, LIBSSH2_METHOD_HOSTKEY, hostkey_method));
  692. }
  693. else {
  694. infof(data, "Did not find host %s in %s\n",
  695. conn->host.name, data->set.str[STRING_SSH_KNOWNHOSTS]);
  696. }
  697. }
  698. #endif /* HAVE_LIBSSH2_KNOWNHOST_API */
  699. return result;
  700. }
  701. /*
  702. * ssh_statemach_act() runs the SSH state machine as far as it can without
  703. * blocking and without reaching the end. The data the pointer 'block' points
  704. * to will be set to TRUE if the libssh2 function returns LIBSSH2_ERROR_EAGAIN
  705. * meaning it wants to be called again when the socket is ready
  706. */
  707. static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
  708. {
  709. CURLcode result = CURLE_OK;
  710. struct Curl_easy *data = conn->data;
  711. struct SSHPROTO *sftp_scp = data->req.protop;
  712. struct ssh_conn *sshc = &conn->proto.sshc;
  713. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  714. int rc = LIBSSH2_ERROR_NONE;
  715. int ssherr;
  716. unsigned long sftperr;
  717. int seekerr = CURL_SEEKFUNC_OK;
  718. size_t readdir_len;
  719. *block = 0; /* we're not blocking by default */
  720. do {
  721. switch(sshc->state) {
  722. case SSH_INIT:
  723. sshc->secondCreateDirs = 0;
  724. sshc->nextstate = SSH_NO_STATE;
  725. sshc->actualcode = CURLE_OK;
  726. /* Set libssh2 to non-blocking, since everything internally is
  727. non-blocking */
  728. libssh2_session_set_blocking(sshc->ssh_session, 0);
  729. result = ssh_force_knownhost_key_type(conn);
  730. if(result) {
  731. state(conn, SSH_SESSION_FREE);
  732. break;
  733. }
  734. state(conn, SSH_S_STARTUP);
  735. /* FALLTHROUGH */
  736. case SSH_S_STARTUP:
  737. rc = libssh2_session_startup(sshc->ssh_session, (int)sock);
  738. if(rc == LIBSSH2_ERROR_EAGAIN) {
  739. break;
  740. }
  741. if(rc) {
  742. char *err_msg = NULL;
  743. (void)libssh2_session_last_error(sshc->ssh_session, &err_msg, NULL, 0);
  744. failf(data, "Failure establishing ssh session: %d, %s", rc, err_msg);
  745. state(conn, SSH_SESSION_FREE);
  746. sshc->actualcode = CURLE_FAILED_INIT;
  747. break;
  748. }
  749. state(conn, SSH_HOSTKEY);
  750. /* FALLTHROUGH */
  751. case SSH_HOSTKEY:
  752. /*
  753. * Before we authenticate we should check the hostkey's fingerprint
  754. * against our known hosts. How that is handled (reading from file,
  755. * whatever) is up to us.
  756. */
  757. result = ssh_check_fingerprint(conn);
  758. if(!result)
  759. state(conn, SSH_AUTHLIST);
  760. /* ssh_check_fingerprint sets state appropriately on error */
  761. break;
  762. case SSH_AUTHLIST:
  763. /*
  764. * Figure out authentication methods
  765. * NB: As soon as we have provided a username to an openssh server we
  766. * must never change it later. Thus, always specify the correct username
  767. * here, even though the libssh2 docs kind of indicate that it should be
  768. * possible to get a 'generic' list (not user-specific) of authentication
  769. * methods, presumably with a blank username. That won't work in my
  770. * experience.
  771. * So always specify it here.
  772. */
  773. sshc->authlist = libssh2_userauth_list(sshc->ssh_session,
  774. conn->user,
  775. curlx_uztoui(strlen(conn->user)));
  776. if(!sshc->authlist) {
  777. if(libssh2_userauth_authenticated(sshc->ssh_session)) {
  778. sshc->authed = TRUE;
  779. infof(data, "SSH user accepted with no authentication\n");
  780. state(conn, SSH_AUTH_DONE);
  781. break;
  782. }
  783. ssherr = libssh2_session_last_errno(sshc->ssh_session);
  784. if(ssherr == LIBSSH2_ERROR_EAGAIN)
  785. rc = LIBSSH2_ERROR_EAGAIN;
  786. else {
  787. state(conn, SSH_SESSION_FREE);
  788. sshc->actualcode = libssh2_session_error_to_CURLE(ssherr);
  789. }
  790. break;
  791. }
  792. infof(data, "SSH authentication methods available: %s\n",
  793. sshc->authlist);
  794. state(conn, SSH_AUTH_PKEY_INIT);
  795. break;
  796. case SSH_AUTH_PKEY_INIT:
  797. /*
  798. * Check the supported auth types in the order I feel is most secure
  799. * with the requested type of authentication
  800. */
  801. sshc->authed = FALSE;
  802. if((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) &&
  803. (strstr(sshc->authlist, "publickey") != NULL)) {
  804. bool out_of_memory = FALSE;
  805. sshc->rsa_pub = sshc->rsa = NULL;
  806. if(data->set.str[STRING_SSH_PRIVATE_KEY])
  807. sshc->rsa = strdup(data->set.str[STRING_SSH_PRIVATE_KEY]);
  808. else {
  809. /* To ponder about: should really the lib be messing about with the
  810. HOME environment variable etc? */
  811. char *home = curl_getenv("HOME");
  812. /* If no private key file is specified, try some common paths. */
  813. if(home) {
  814. /* Try ~/.ssh first. */
  815. sshc->rsa = aprintf("%s/.ssh/id_rsa", home);
  816. if(!sshc->rsa)
  817. out_of_memory = TRUE;
  818. else if(access(sshc->rsa, R_OK) != 0) {
  819. Curl_safefree(sshc->rsa);
  820. sshc->rsa = aprintf("%s/.ssh/id_dsa", home);
  821. if(!sshc->rsa)
  822. out_of_memory = TRUE;
  823. else if(access(sshc->rsa, R_OK) != 0) {
  824. Curl_safefree(sshc->rsa);
  825. }
  826. }
  827. free(home);
  828. }
  829. if(!out_of_memory && !sshc->rsa) {
  830. /* Nothing found; try the current dir. */
  831. sshc->rsa = strdup("id_rsa");
  832. if(sshc->rsa && access(sshc->rsa, R_OK) != 0) {
  833. Curl_safefree(sshc->rsa);
  834. sshc->rsa = strdup("id_dsa");
  835. if(sshc->rsa && access(sshc->rsa, R_OK) != 0) {
  836. Curl_safefree(sshc->rsa);
  837. /* Out of guesses. Set to the empty string to avoid
  838. * surprising info messages. */
  839. sshc->rsa = strdup("");
  840. }
  841. }
  842. }
  843. }
  844. /*
  845. * Unless the user explicitly specifies a public key file, let
  846. * libssh2 extract the public key from the private key file.
  847. * This is done by simply passing sshc->rsa_pub = NULL.
  848. */
  849. if(data->set.str[STRING_SSH_PUBLIC_KEY]
  850. /* treat empty string the same way as NULL */
  851. && data->set.str[STRING_SSH_PUBLIC_KEY][0]) {
  852. sshc->rsa_pub = strdup(data->set.str[STRING_SSH_PUBLIC_KEY]);
  853. if(!sshc->rsa_pub)
  854. out_of_memory = TRUE;
  855. }
  856. if(out_of_memory || sshc->rsa == NULL) {
  857. Curl_safefree(sshc->rsa);
  858. Curl_safefree(sshc->rsa_pub);
  859. state(conn, SSH_SESSION_FREE);
  860. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  861. break;
  862. }
  863. sshc->passphrase = data->set.ssl.key_passwd;
  864. if(!sshc->passphrase)
  865. sshc->passphrase = "";
  866. if(sshc->rsa_pub)
  867. infof(data, "Using SSH public key file '%s'\n", sshc->rsa_pub);
  868. infof(data, "Using SSH private key file '%s'\n", sshc->rsa);
  869. state(conn, SSH_AUTH_PKEY);
  870. }
  871. else {
  872. state(conn, SSH_AUTH_PASS_INIT);
  873. }
  874. break;
  875. case SSH_AUTH_PKEY:
  876. /* The function below checks if the files exists, no need to stat() here.
  877. */
  878. rc = libssh2_userauth_publickey_fromfile_ex(sshc->ssh_session,
  879. conn->user,
  880. curlx_uztoui(
  881. strlen(conn->user)),
  882. sshc->rsa_pub,
  883. sshc->rsa, sshc->passphrase);
  884. if(rc == LIBSSH2_ERROR_EAGAIN) {
  885. break;
  886. }
  887. Curl_safefree(sshc->rsa_pub);
  888. Curl_safefree(sshc->rsa);
  889. if(rc == 0) {
  890. sshc->authed = TRUE;
  891. infof(data, "Initialized SSH public key authentication\n");
  892. state(conn, SSH_AUTH_DONE);
  893. }
  894. else {
  895. char *err_msg = NULL;
  896. (void)libssh2_session_last_error(sshc->ssh_session,
  897. &err_msg, NULL, 0);
  898. infof(data, "SSH public key authentication failed: %s\n", err_msg);
  899. state(conn, SSH_AUTH_PASS_INIT);
  900. rc = 0; /* clear rc and continue */
  901. }
  902. break;
  903. case SSH_AUTH_PASS_INIT:
  904. if((data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) &&
  905. (strstr(sshc->authlist, "password") != NULL)) {
  906. state(conn, SSH_AUTH_PASS);
  907. }
  908. else {
  909. state(conn, SSH_AUTH_HOST_INIT);
  910. rc = 0; /* clear rc and continue */
  911. }
  912. break;
  913. case SSH_AUTH_PASS:
  914. rc = libssh2_userauth_password_ex(sshc->ssh_session, conn->user,
  915. curlx_uztoui(strlen(conn->user)),
  916. conn->passwd,
  917. curlx_uztoui(strlen(conn->passwd)),
  918. NULL);
  919. if(rc == LIBSSH2_ERROR_EAGAIN) {
  920. break;
  921. }
  922. if(rc == 0) {
  923. sshc->authed = TRUE;
  924. infof(data, "Initialized password authentication\n");
  925. state(conn, SSH_AUTH_DONE);
  926. }
  927. else {
  928. state(conn, SSH_AUTH_HOST_INIT);
  929. rc = 0; /* clear rc and continue */
  930. }
  931. break;
  932. case SSH_AUTH_HOST_INIT:
  933. if((data->set.ssh_auth_types & CURLSSH_AUTH_HOST) &&
  934. (strstr(sshc->authlist, "hostbased") != NULL)) {
  935. state(conn, SSH_AUTH_HOST);
  936. }
  937. else {
  938. state(conn, SSH_AUTH_AGENT_INIT);
  939. }
  940. break;
  941. case SSH_AUTH_HOST:
  942. state(conn, SSH_AUTH_AGENT_INIT);
  943. break;
  944. case SSH_AUTH_AGENT_INIT:
  945. #ifdef HAVE_LIBSSH2_AGENT_API
  946. if((data->set.ssh_auth_types & CURLSSH_AUTH_AGENT)
  947. && (strstr(sshc->authlist, "publickey") != NULL)) {
  948. /* Connect to the ssh-agent */
  949. /* The agent could be shared by a curl thread i believe
  950. but nothing obvious as keys can be added/removed at any time */
  951. if(!sshc->ssh_agent) {
  952. sshc->ssh_agent = libssh2_agent_init(sshc->ssh_session);
  953. if(!sshc->ssh_agent) {
  954. infof(data, "Could not create agent object\n");
  955. state(conn, SSH_AUTH_KEY_INIT);
  956. break;
  957. }
  958. }
  959. rc = libssh2_agent_connect(sshc->ssh_agent);
  960. if(rc == LIBSSH2_ERROR_EAGAIN)
  961. break;
  962. if(rc < 0) {
  963. infof(data, "Failure connecting to agent\n");
  964. state(conn, SSH_AUTH_KEY_INIT);
  965. rc = 0; /* clear rc and continue */
  966. }
  967. else {
  968. state(conn, SSH_AUTH_AGENT_LIST);
  969. }
  970. }
  971. else
  972. #endif /* HAVE_LIBSSH2_AGENT_API */
  973. state(conn, SSH_AUTH_KEY_INIT);
  974. break;
  975. case SSH_AUTH_AGENT_LIST:
  976. #ifdef HAVE_LIBSSH2_AGENT_API
  977. rc = libssh2_agent_list_identities(sshc->ssh_agent);
  978. if(rc == LIBSSH2_ERROR_EAGAIN)
  979. break;
  980. if(rc < 0) {
  981. infof(data, "Failure requesting identities to agent\n");
  982. state(conn, SSH_AUTH_KEY_INIT);
  983. rc = 0; /* clear rc and continue */
  984. }
  985. else {
  986. state(conn, SSH_AUTH_AGENT);
  987. sshc->sshagent_prev_identity = NULL;
  988. }
  989. #endif
  990. break;
  991. case SSH_AUTH_AGENT:
  992. #ifdef HAVE_LIBSSH2_AGENT_API
  993. /* as prev_identity evolves only after an identity user auth finished we
  994. can safely request it again as long as EAGAIN is returned here or by
  995. libssh2_agent_userauth */
  996. rc = libssh2_agent_get_identity(sshc->ssh_agent,
  997. &sshc->sshagent_identity,
  998. sshc->sshagent_prev_identity);
  999. if(rc == LIBSSH2_ERROR_EAGAIN)
  1000. break;
  1001. if(rc == 0) {
  1002. rc = libssh2_agent_userauth(sshc->ssh_agent, conn->user,
  1003. sshc->sshagent_identity);
  1004. if(rc < 0) {
  1005. if(rc != LIBSSH2_ERROR_EAGAIN) {
  1006. /* tried and failed? go to next identity */
  1007. sshc->sshagent_prev_identity = sshc->sshagent_identity;
  1008. }
  1009. break;
  1010. }
  1011. }
  1012. if(rc < 0)
  1013. infof(data, "Failure requesting identities to agent\n");
  1014. else if(rc == 1)
  1015. infof(data, "No identity would match\n");
  1016. if(rc == LIBSSH2_ERROR_NONE) {
  1017. sshc->authed = TRUE;
  1018. infof(data, "Agent based authentication successful\n");
  1019. state(conn, SSH_AUTH_DONE);
  1020. }
  1021. else {
  1022. state(conn, SSH_AUTH_KEY_INIT);
  1023. rc = 0; /* clear rc and continue */
  1024. }
  1025. #endif
  1026. break;
  1027. case SSH_AUTH_KEY_INIT:
  1028. if((data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD)
  1029. && (strstr(sshc->authlist, "keyboard-interactive") != NULL)) {
  1030. state(conn, SSH_AUTH_KEY);
  1031. }
  1032. else {
  1033. state(conn, SSH_AUTH_DONE);
  1034. }
  1035. break;
  1036. case SSH_AUTH_KEY:
  1037. /* Authentication failed. Continue with keyboard-interactive now. */
  1038. rc = libssh2_userauth_keyboard_interactive_ex(sshc->ssh_session,
  1039. conn->user,
  1040. curlx_uztoui(
  1041. strlen(conn->user)),
  1042. &kbd_callback);
  1043. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1044. break;
  1045. }
  1046. if(rc == 0) {
  1047. sshc->authed = TRUE;
  1048. infof(data, "Initialized keyboard interactive authentication\n");
  1049. }
  1050. state(conn, SSH_AUTH_DONE);
  1051. break;
  1052. case SSH_AUTH_DONE:
  1053. if(!sshc->authed) {
  1054. failf(data, "Authentication failure");
  1055. state(conn, SSH_SESSION_FREE);
  1056. sshc->actualcode = CURLE_LOGIN_DENIED;
  1057. break;
  1058. }
  1059. /*
  1060. * At this point we have an authenticated ssh session.
  1061. */
  1062. infof(data, "Authentication complete\n");
  1063. Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSH is connected */
  1064. conn->sockfd = sock;
  1065. conn->writesockfd = CURL_SOCKET_BAD;
  1066. if(conn->handler->protocol == CURLPROTO_SFTP) {
  1067. state(conn, SSH_SFTP_INIT);
  1068. break;
  1069. }
  1070. infof(data, "SSH CONNECT phase done\n");
  1071. state(conn, SSH_STOP);
  1072. break;
  1073. case SSH_SFTP_INIT:
  1074. /*
  1075. * Start the libssh2 sftp session
  1076. */
  1077. sshc->sftp_session = libssh2_sftp_init(sshc->ssh_session);
  1078. if(!sshc->sftp_session) {
  1079. char *err_msg = NULL;
  1080. if(libssh2_session_last_errno(sshc->ssh_session) ==
  1081. LIBSSH2_ERROR_EAGAIN) {
  1082. rc = LIBSSH2_ERROR_EAGAIN;
  1083. break;
  1084. }
  1085. (void)libssh2_session_last_error(sshc->ssh_session,
  1086. &err_msg, NULL, 0);
  1087. failf(data, "Failure initializing sftp session: %s", err_msg);
  1088. state(conn, SSH_SESSION_FREE);
  1089. sshc->actualcode = CURLE_FAILED_INIT;
  1090. break;
  1091. }
  1092. state(conn, SSH_SFTP_REALPATH);
  1093. break;
  1094. case SSH_SFTP_REALPATH:
  1095. {
  1096. char tempHome[PATH_MAX];
  1097. /*
  1098. * Get the "home" directory
  1099. */
  1100. rc = sftp_libssh2_realpath(sshc->sftp_session, ".",
  1101. tempHome, PATH_MAX-1);
  1102. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1103. break;
  1104. }
  1105. if(rc > 0) {
  1106. /* It seems that this string is not always NULL terminated */
  1107. tempHome[rc] = '\0';
  1108. sshc->homedir = strdup(tempHome);
  1109. if(!sshc->homedir) {
  1110. state(conn, SSH_SFTP_CLOSE);
  1111. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  1112. break;
  1113. }
  1114. conn->data->state.most_recent_ftp_entrypath = sshc->homedir;
  1115. }
  1116. else {
  1117. /* Return the error type */
  1118. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1119. if(sftperr)
  1120. result = sftp_libssh2_error_to_CURLE(sftperr);
  1121. else
  1122. /* in this case, the error wasn't in the SFTP level but for example
  1123. a time-out or similar */
  1124. result = CURLE_SSH;
  1125. sshc->actualcode = result;
  1126. DEBUGF(infof(data, "error = %d makes libcurl = %d\n",
  1127. sftperr, (int)result));
  1128. state(conn, SSH_STOP);
  1129. break;
  1130. }
  1131. }
  1132. /* This is the last step in the SFTP connect phase. Do note that while
  1133. we get the homedir here, we get the "workingpath" in the DO action
  1134. since the homedir will remain the same between request but the
  1135. working path will not. */
  1136. DEBUGF(infof(data, "SSH CONNECT phase done\n"));
  1137. state(conn, SSH_STOP);
  1138. break;
  1139. case SSH_SFTP_QUOTE_INIT:
  1140. result = Curl_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
  1141. if(result) {
  1142. sshc->actualcode = result;
  1143. state(conn, SSH_STOP);
  1144. break;
  1145. }
  1146. if(data->set.quote) {
  1147. infof(data, "Sending quote commands\n");
  1148. sshc->quote_item = data->set.quote;
  1149. state(conn, SSH_SFTP_QUOTE);
  1150. }
  1151. else {
  1152. state(conn, SSH_SFTP_GETINFO);
  1153. }
  1154. break;
  1155. case SSH_SFTP_POSTQUOTE_INIT:
  1156. if(data->set.postquote) {
  1157. infof(data, "Sending quote commands\n");
  1158. sshc->quote_item = data->set.postquote;
  1159. state(conn, SSH_SFTP_QUOTE);
  1160. }
  1161. else {
  1162. state(conn, SSH_STOP);
  1163. }
  1164. break;
  1165. case SSH_SFTP_QUOTE:
  1166. /* Send any quote commands */
  1167. {
  1168. const char *cp;
  1169. /*
  1170. * Support some of the "FTP" commands
  1171. *
  1172. * 'sshc->quote_item' is already verified to be non-NULL before it
  1173. * switched to this state.
  1174. */
  1175. char *cmd = sshc->quote_item->data;
  1176. sshc->acceptfail = FALSE;
  1177. /* if a command starts with an asterisk, which a legal SFTP command never
  1178. can, the command will be allowed to fail without it causing any
  1179. aborts or cancels etc. It will cause libcurl to act as if the command
  1180. is successful, whatever the server reponds. */
  1181. if(cmd[0] == '*') {
  1182. cmd++;
  1183. sshc->acceptfail = TRUE;
  1184. }
  1185. if(strcasecompare("pwd", cmd)) {
  1186. /* output debug output if that is requested */
  1187. char *tmp = aprintf("257 \"%s\" is current directory.\n",
  1188. sftp_scp->path);
  1189. if(!tmp) {
  1190. result = CURLE_OUT_OF_MEMORY;
  1191. state(conn, SSH_SFTP_CLOSE);
  1192. sshc->nextstate = SSH_NO_STATE;
  1193. break;
  1194. }
  1195. if(data->set.verbose) {
  1196. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4);
  1197. Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp));
  1198. }
  1199. /* this sends an FTP-like "header" to the header callback so that the
  1200. current directory can be read very similar to how it is read when
  1201. using ordinary FTP. */
  1202. result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp));
  1203. free(tmp);
  1204. if(result) {
  1205. state(conn, SSH_SFTP_CLOSE);
  1206. sshc->nextstate = SSH_NO_STATE;
  1207. sshc->actualcode = result;
  1208. }
  1209. else
  1210. state(conn, SSH_SFTP_NEXT_QUOTE);
  1211. break;
  1212. }
  1213. {
  1214. /*
  1215. * the arguments following the command must be separated from the
  1216. * command with a space so we can check for it unconditionally
  1217. */
  1218. cp = strchr(cmd, ' ');
  1219. if(cp == NULL) {
  1220. failf(data, "Syntax error command '%s'. Missing parameter!",
  1221. cmd);
  1222. state(conn, SSH_SFTP_CLOSE);
  1223. sshc->nextstate = SSH_NO_STATE;
  1224. sshc->actualcode = CURLE_QUOTE_ERROR;
  1225. break;
  1226. }
  1227. /*
  1228. * also, every command takes at least one argument so we get that
  1229. * first argument right now
  1230. */
  1231. result = Curl_get_pathname(&cp, &sshc->quote_path1, sshc->homedir);
  1232. if(result) {
  1233. if(result == CURLE_OUT_OF_MEMORY)
  1234. failf(data, "Out of memory");
  1235. else
  1236. failf(data, "Syntax error: Bad first parameter to '%s'", cmd);
  1237. state(conn, SSH_SFTP_CLOSE);
  1238. sshc->nextstate = SSH_NO_STATE;
  1239. sshc->actualcode = result;
  1240. break;
  1241. }
  1242. /*
  1243. * SFTP is a binary protocol, so we don't send text commands
  1244. * to the server. Instead, we scan for commands used by
  1245. * OpenSSH's sftp program and call the appropriate libssh2
  1246. * functions.
  1247. */
  1248. if(strncasecompare(cmd, "chgrp ", 6) ||
  1249. strncasecompare(cmd, "chmod ", 6) ||
  1250. strncasecompare(cmd, "chown ", 6) ) {
  1251. /* attribute change */
  1252. /* sshc->quote_path1 contains the mode to set */
  1253. /* get the destination */
  1254. result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
  1255. if(result) {
  1256. if(result == CURLE_OUT_OF_MEMORY)
  1257. failf(data, "Out of memory");
  1258. else
  1259. failf(data, "Syntax error in %s: Bad second parameter", cmd);
  1260. Curl_safefree(sshc->quote_path1);
  1261. state(conn, SSH_SFTP_CLOSE);
  1262. sshc->nextstate = SSH_NO_STATE;
  1263. sshc->actualcode = result;
  1264. break;
  1265. }
  1266. memset(&sshc->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
  1267. state(conn, SSH_SFTP_QUOTE_STAT);
  1268. break;
  1269. }
  1270. if(strncasecompare(cmd, "ln ", 3) ||
  1271. strncasecompare(cmd, "symlink ", 8)) {
  1272. /* symbolic linking */
  1273. /* sshc->quote_path1 is the source */
  1274. /* get the destination */
  1275. result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
  1276. if(result) {
  1277. if(result == CURLE_OUT_OF_MEMORY)
  1278. failf(data, "Out of memory");
  1279. else
  1280. failf(data,
  1281. "Syntax error in ln/symlink: Bad second parameter");
  1282. Curl_safefree(sshc->quote_path1);
  1283. state(conn, SSH_SFTP_CLOSE);
  1284. sshc->nextstate = SSH_NO_STATE;
  1285. sshc->actualcode = result;
  1286. break;
  1287. }
  1288. state(conn, SSH_SFTP_QUOTE_SYMLINK);
  1289. break;
  1290. }
  1291. else if(strncasecompare(cmd, "mkdir ", 6)) {
  1292. /* create dir */
  1293. state(conn, SSH_SFTP_QUOTE_MKDIR);
  1294. break;
  1295. }
  1296. else if(strncasecompare(cmd, "rename ", 7)) {
  1297. /* rename file */
  1298. /* first param is the source path */
  1299. /* second param is the dest. path */
  1300. result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
  1301. if(result) {
  1302. if(result == CURLE_OUT_OF_MEMORY)
  1303. failf(data, "Out of memory");
  1304. else
  1305. failf(data, "Syntax error in rename: Bad second parameter");
  1306. Curl_safefree(sshc->quote_path1);
  1307. state(conn, SSH_SFTP_CLOSE);
  1308. sshc->nextstate = SSH_NO_STATE;
  1309. sshc->actualcode = result;
  1310. break;
  1311. }
  1312. state(conn, SSH_SFTP_QUOTE_RENAME);
  1313. break;
  1314. }
  1315. else if(strncasecompare(cmd, "rmdir ", 6)) {
  1316. /* delete dir */
  1317. state(conn, SSH_SFTP_QUOTE_RMDIR);
  1318. break;
  1319. }
  1320. else if(strncasecompare(cmd, "rm ", 3)) {
  1321. state(conn, SSH_SFTP_QUOTE_UNLINK);
  1322. break;
  1323. }
  1324. #ifdef HAS_STATVFS_SUPPORT
  1325. else if(strncasecompare(cmd, "statvfs ", 8)) {
  1326. state(conn, SSH_SFTP_QUOTE_STATVFS);
  1327. break;
  1328. }
  1329. #endif
  1330. failf(data, "Unknown SFTP command");
  1331. Curl_safefree(sshc->quote_path1);
  1332. Curl_safefree(sshc->quote_path2);
  1333. state(conn, SSH_SFTP_CLOSE);
  1334. sshc->nextstate = SSH_NO_STATE;
  1335. sshc->actualcode = CURLE_QUOTE_ERROR;
  1336. break;
  1337. }
  1338. }
  1339. break;
  1340. case SSH_SFTP_NEXT_QUOTE:
  1341. Curl_safefree(sshc->quote_path1);
  1342. Curl_safefree(sshc->quote_path2);
  1343. sshc->quote_item = sshc->quote_item->next;
  1344. if(sshc->quote_item) {
  1345. state(conn, SSH_SFTP_QUOTE);
  1346. }
  1347. else {
  1348. if(sshc->nextstate != SSH_NO_STATE) {
  1349. state(conn, sshc->nextstate);
  1350. sshc->nextstate = SSH_NO_STATE;
  1351. }
  1352. else {
  1353. state(conn, SSH_SFTP_GETINFO);
  1354. }
  1355. }
  1356. break;
  1357. case SSH_SFTP_QUOTE_STAT:
  1358. {
  1359. char *cmd = sshc->quote_item->data;
  1360. sshc->acceptfail = FALSE;
  1361. /* if a command starts with an asterisk, which a legal SFTP command never
  1362. can, the command will be allowed to fail without it causing any
  1363. aborts or cancels etc. It will cause libcurl to act as if the command
  1364. is successful, whatever the server reponds. */
  1365. if(cmd[0] == '*') {
  1366. cmd++;
  1367. sshc->acceptfail = TRUE;
  1368. }
  1369. if(!strncasecompare(cmd, "chmod", 5)) {
  1370. /* Since chown and chgrp only set owner OR group but libssh2 wants to
  1371. * set them both at once, we need to obtain the current ownership
  1372. * first. This takes an extra protocol round trip.
  1373. */
  1374. rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
  1375. curlx_uztoui(strlen(sshc->quote_path2)),
  1376. LIBSSH2_SFTP_STAT,
  1377. &sshc->quote_attrs);
  1378. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1379. break;
  1380. }
  1381. if(rc != 0 && !sshc->acceptfail) { /* get those attributes */
  1382. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1383. Curl_safefree(sshc->quote_path1);
  1384. Curl_safefree(sshc->quote_path2);
  1385. failf(data, "Attempt to get SFTP stats failed: %s",
  1386. sftp_libssh2_strerror(sftperr));
  1387. state(conn, SSH_SFTP_CLOSE);
  1388. sshc->nextstate = SSH_NO_STATE;
  1389. sshc->actualcode = CURLE_QUOTE_ERROR;
  1390. break;
  1391. }
  1392. }
  1393. /* Now set the new attributes... */
  1394. if(strncasecompare(cmd, "chgrp", 5)) {
  1395. sshc->quote_attrs.gid = strtoul(sshc->quote_path1, NULL, 10);
  1396. sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
  1397. if(sshc->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
  1398. !sshc->acceptfail) {
  1399. Curl_safefree(sshc->quote_path1);
  1400. Curl_safefree(sshc->quote_path2);
  1401. failf(data, "Syntax error: chgrp gid not a number");
  1402. state(conn, SSH_SFTP_CLOSE);
  1403. sshc->nextstate = SSH_NO_STATE;
  1404. sshc->actualcode = CURLE_QUOTE_ERROR;
  1405. break;
  1406. }
  1407. }
  1408. else if(strncasecompare(cmd, "chmod", 5)) {
  1409. sshc->quote_attrs.permissions = strtoul(sshc->quote_path1, NULL, 8);
  1410. sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS;
  1411. /* permissions are octal */
  1412. if(sshc->quote_attrs.permissions == 0 &&
  1413. !ISDIGIT(sshc->quote_path1[0])) {
  1414. Curl_safefree(sshc->quote_path1);
  1415. Curl_safefree(sshc->quote_path2);
  1416. failf(data, "Syntax error: chmod permissions not a number");
  1417. state(conn, SSH_SFTP_CLOSE);
  1418. sshc->nextstate = SSH_NO_STATE;
  1419. sshc->actualcode = CURLE_QUOTE_ERROR;
  1420. break;
  1421. }
  1422. }
  1423. else if(strncasecompare(cmd, "chown", 5)) {
  1424. sshc->quote_attrs.uid = strtoul(sshc->quote_path1, NULL, 10);
  1425. sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
  1426. if(sshc->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
  1427. !sshc->acceptfail) {
  1428. Curl_safefree(sshc->quote_path1);
  1429. Curl_safefree(sshc->quote_path2);
  1430. failf(data, "Syntax error: chown uid not a number");
  1431. state(conn, SSH_SFTP_CLOSE);
  1432. sshc->nextstate = SSH_NO_STATE;
  1433. sshc->actualcode = CURLE_QUOTE_ERROR;
  1434. break;
  1435. }
  1436. }
  1437. /* Now send the completed structure... */
  1438. state(conn, SSH_SFTP_QUOTE_SETSTAT);
  1439. break;
  1440. }
  1441. case SSH_SFTP_QUOTE_SETSTAT:
  1442. rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
  1443. curlx_uztoui(strlen(sshc->quote_path2)),
  1444. LIBSSH2_SFTP_SETSTAT,
  1445. &sshc->quote_attrs);
  1446. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1447. break;
  1448. }
  1449. if(rc != 0 && !sshc->acceptfail) {
  1450. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1451. Curl_safefree(sshc->quote_path1);
  1452. Curl_safefree(sshc->quote_path2);
  1453. failf(data, "Attempt to set SFTP stats failed: %s",
  1454. sftp_libssh2_strerror(sftperr));
  1455. state(conn, SSH_SFTP_CLOSE);
  1456. sshc->nextstate = SSH_NO_STATE;
  1457. sshc->actualcode = CURLE_QUOTE_ERROR;
  1458. break;
  1459. }
  1460. state(conn, SSH_SFTP_NEXT_QUOTE);
  1461. break;
  1462. case SSH_SFTP_QUOTE_SYMLINK:
  1463. rc = libssh2_sftp_symlink_ex(sshc->sftp_session, sshc->quote_path1,
  1464. curlx_uztoui(strlen(sshc->quote_path1)),
  1465. sshc->quote_path2,
  1466. curlx_uztoui(strlen(sshc->quote_path2)),
  1467. LIBSSH2_SFTP_SYMLINK);
  1468. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1469. break;
  1470. }
  1471. if(rc != 0 && !sshc->acceptfail) {
  1472. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1473. Curl_safefree(sshc->quote_path1);
  1474. Curl_safefree(sshc->quote_path2);
  1475. failf(data, "symlink command failed: %s",
  1476. sftp_libssh2_strerror(sftperr));
  1477. state(conn, SSH_SFTP_CLOSE);
  1478. sshc->nextstate = SSH_NO_STATE;
  1479. sshc->actualcode = CURLE_QUOTE_ERROR;
  1480. break;
  1481. }
  1482. state(conn, SSH_SFTP_NEXT_QUOTE);
  1483. break;
  1484. case SSH_SFTP_QUOTE_MKDIR:
  1485. rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshc->quote_path1,
  1486. curlx_uztoui(strlen(sshc->quote_path1)),
  1487. data->set.new_directory_perms);
  1488. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1489. break;
  1490. }
  1491. if(rc != 0 && !sshc->acceptfail) {
  1492. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1493. Curl_safefree(sshc->quote_path1);
  1494. failf(data, "mkdir command failed: %s",
  1495. sftp_libssh2_strerror(sftperr));
  1496. state(conn, SSH_SFTP_CLOSE);
  1497. sshc->nextstate = SSH_NO_STATE;
  1498. sshc->actualcode = CURLE_QUOTE_ERROR;
  1499. break;
  1500. }
  1501. state(conn, SSH_SFTP_NEXT_QUOTE);
  1502. break;
  1503. case SSH_SFTP_QUOTE_RENAME:
  1504. rc = libssh2_sftp_rename_ex(sshc->sftp_session, sshc->quote_path1,
  1505. curlx_uztoui(strlen(sshc->quote_path1)),
  1506. sshc->quote_path2,
  1507. curlx_uztoui(strlen(sshc->quote_path2)),
  1508. LIBSSH2_SFTP_RENAME_OVERWRITE |
  1509. LIBSSH2_SFTP_RENAME_ATOMIC |
  1510. LIBSSH2_SFTP_RENAME_NATIVE);
  1511. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1512. break;
  1513. }
  1514. if(rc != 0 && !sshc->acceptfail) {
  1515. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1516. Curl_safefree(sshc->quote_path1);
  1517. Curl_safefree(sshc->quote_path2);
  1518. failf(data, "rename command failed: %s",
  1519. sftp_libssh2_strerror(sftperr));
  1520. state(conn, SSH_SFTP_CLOSE);
  1521. sshc->nextstate = SSH_NO_STATE;
  1522. sshc->actualcode = CURLE_QUOTE_ERROR;
  1523. break;
  1524. }
  1525. state(conn, SSH_SFTP_NEXT_QUOTE);
  1526. break;
  1527. case SSH_SFTP_QUOTE_RMDIR:
  1528. rc = libssh2_sftp_rmdir_ex(sshc->sftp_session, sshc->quote_path1,
  1529. curlx_uztoui(strlen(sshc->quote_path1)));
  1530. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1531. break;
  1532. }
  1533. if(rc != 0 && !sshc->acceptfail) {
  1534. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1535. Curl_safefree(sshc->quote_path1);
  1536. failf(data, "rmdir command failed: %s",
  1537. sftp_libssh2_strerror(sftperr));
  1538. state(conn, SSH_SFTP_CLOSE);
  1539. sshc->nextstate = SSH_NO_STATE;
  1540. sshc->actualcode = CURLE_QUOTE_ERROR;
  1541. break;
  1542. }
  1543. state(conn, SSH_SFTP_NEXT_QUOTE);
  1544. break;
  1545. case SSH_SFTP_QUOTE_UNLINK:
  1546. rc = libssh2_sftp_unlink_ex(sshc->sftp_session, sshc->quote_path1,
  1547. curlx_uztoui(strlen(sshc->quote_path1)));
  1548. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1549. break;
  1550. }
  1551. if(rc != 0 && !sshc->acceptfail) {
  1552. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1553. Curl_safefree(sshc->quote_path1);
  1554. failf(data, "rm command failed: %s", sftp_libssh2_strerror(sftperr));
  1555. state(conn, SSH_SFTP_CLOSE);
  1556. sshc->nextstate = SSH_NO_STATE;
  1557. sshc->actualcode = CURLE_QUOTE_ERROR;
  1558. break;
  1559. }
  1560. state(conn, SSH_SFTP_NEXT_QUOTE);
  1561. break;
  1562. #ifdef HAS_STATVFS_SUPPORT
  1563. case SSH_SFTP_QUOTE_STATVFS:
  1564. {
  1565. LIBSSH2_SFTP_STATVFS statvfs;
  1566. rc = libssh2_sftp_statvfs(sshc->sftp_session, sshc->quote_path1,
  1567. curlx_uztoui(strlen(sshc->quote_path1)),
  1568. &statvfs);
  1569. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1570. break;
  1571. }
  1572. if(rc != 0 && !sshc->acceptfail) {
  1573. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1574. Curl_safefree(sshc->quote_path1);
  1575. failf(data, "statvfs command failed: %s",
  1576. sftp_libssh2_strerror(sftperr));
  1577. state(conn, SSH_SFTP_CLOSE);
  1578. sshc->nextstate = SSH_NO_STATE;
  1579. sshc->actualcode = CURLE_QUOTE_ERROR;
  1580. break;
  1581. }
  1582. else if(rc == 0) {
  1583. char *tmp = aprintf("statvfs:\n"
  1584. "f_bsize: %llu\n" "f_frsize: %llu\n"
  1585. "f_blocks: %llu\n" "f_bfree: %llu\n"
  1586. "f_bavail: %llu\n" "f_files: %llu\n"
  1587. "f_ffree: %llu\n" "f_favail: %llu\n"
  1588. "f_fsid: %llu\n" "f_flag: %llu\n"
  1589. "f_namemax: %llu\n",
  1590. statvfs.f_bsize, statvfs.f_frsize,
  1591. statvfs.f_blocks, statvfs.f_bfree,
  1592. statvfs.f_bavail, statvfs.f_files,
  1593. statvfs.f_ffree, statvfs.f_favail,
  1594. statvfs.f_fsid, statvfs.f_flag,
  1595. statvfs.f_namemax);
  1596. if(!tmp) {
  1597. result = CURLE_OUT_OF_MEMORY;
  1598. state(conn, SSH_SFTP_CLOSE);
  1599. sshc->nextstate = SSH_NO_STATE;
  1600. break;
  1601. }
  1602. result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp));
  1603. free(tmp);
  1604. if(result) {
  1605. state(conn, SSH_SFTP_CLOSE);
  1606. sshc->nextstate = SSH_NO_STATE;
  1607. sshc->actualcode = result;
  1608. }
  1609. }
  1610. state(conn, SSH_SFTP_NEXT_QUOTE);
  1611. break;
  1612. }
  1613. #endif
  1614. case SSH_SFTP_GETINFO:
  1615. {
  1616. if(data->set.get_filetime) {
  1617. state(conn, SSH_SFTP_FILETIME);
  1618. }
  1619. else {
  1620. state(conn, SSH_SFTP_TRANS_INIT);
  1621. }
  1622. break;
  1623. }
  1624. case SSH_SFTP_FILETIME:
  1625. {
  1626. LIBSSH2_SFTP_ATTRIBUTES attrs;
  1627. rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path,
  1628. curlx_uztoui(strlen(sftp_scp->path)),
  1629. LIBSSH2_SFTP_STAT, &attrs);
  1630. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1631. break;
  1632. }
  1633. if(rc == 0) {
  1634. data->info.filetime = attrs.mtime;
  1635. }
  1636. state(conn, SSH_SFTP_TRANS_INIT);
  1637. break;
  1638. }
  1639. case SSH_SFTP_TRANS_INIT:
  1640. if(data->set.upload)
  1641. state(conn, SSH_SFTP_UPLOAD_INIT);
  1642. else {
  1643. if(sftp_scp->path[strlen(sftp_scp->path)-1] == '/')
  1644. state(conn, SSH_SFTP_READDIR_INIT);
  1645. else
  1646. state(conn, SSH_SFTP_DOWNLOAD_INIT);
  1647. }
  1648. break;
  1649. case SSH_SFTP_UPLOAD_INIT:
  1650. {
  1651. unsigned long flags;
  1652. /*
  1653. * NOTE!!! libssh2 requires that the destination path is a full path
  1654. * that includes the destination file and name OR ends in a "/"
  1655. * If this is not done the destination file will be named the
  1656. * same name as the last directory in the path.
  1657. */
  1658. if(data->state.resume_from != 0) {
  1659. LIBSSH2_SFTP_ATTRIBUTES attrs;
  1660. if(data->state.resume_from < 0) {
  1661. rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path,
  1662. curlx_uztoui(strlen(sftp_scp->path)),
  1663. LIBSSH2_SFTP_STAT, &attrs);
  1664. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1665. break;
  1666. }
  1667. if(rc) {
  1668. data->state.resume_from = 0;
  1669. }
  1670. else {
  1671. curl_off_t size = attrs.filesize;
  1672. if(size < 0) {
  1673. failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
  1674. return CURLE_BAD_DOWNLOAD_RESUME;
  1675. }
  1676. data->state.resume_from = attrs.filesize;
  1677. }
  1678. }
  1679. }
  1680. if(data->set.ftp_append)
  1681. /* Try to open for append, but create if nonexisting */
  1682. flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_APPEND;
  1683. else if(data->state.resume_from > 0)
  1684. /* If we have restart position then open for append */
  1685. flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_APPEND;
  1686. else
  1687. /* Clear file before writing (normal behaviour) */
  1688. flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC;
  1689. sshc->sftp_handle =
  1690. libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path,
  1691. curlx_uztoui(strlen(sftp_scp->path)),
  1692. flags, data->set.new_file_perms,
  1693. LIBSSH2_SFTP_OPENFILE);
  1694. if(!sshc->sftp_handle) {
  1695. rc = libssh2_session_last_errno(sshc->ssh_session);
  1696. if(LIBSSH2_ERROR_EAGAIN == rc)
  1697. break;
  1698. if(LIBSSH2_ERROR_SFTP_PROTOCOL == rc)
  1699. /* only when there was an SFTP protocol error can we extract
  1700. the sftp error! */
  1701. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1702. else
  1703. sftperr = LIBSSH2_FX_OK; /* not an sftp error at all */
  1704. if(sshc->secondCreateDirs) {
  1705. state(conn, SSH_SFTP_CLOSE);
  1706. sshc->actualcode = sftperr != LIBSSH2_FX_OK ?
  1707. sftp_libssh2_error_to_CURLE(sftperr):CURLE_SSH;
  1708. failf(data, "Creating the dir/file failed: %s",
  1709. sftp_libssh2_strerror(sftperr));
  1710. break;
  1711. }
  1712. if(((sftperr == LIBSSH2_FX_NO_SUCH_FILE) ||
  1713. (sftperr == LIBSSH2_FX_FAILURE) ||
  1714. (sftperr == LIBSSH2_FX_NO_SUCH_PATH)) &&
  1715. (data->set.ftp_create_missing_dirs &&
  1716. (strlen(sftp_scp->path) > 1))) {
  1717. /* try to create the path remotely */
  1718. rc = 0; /* clear rc and continue */
  1719. sshc->secondCreateDirs = 1;
  1720. state(conn, SSH_SFTP_CREATE_DIRS_INIT);
  1721. break;
  1722. }
  1723. state(conn, SSH_SFTP_CLOSE);
  1724. sshc->actualcode = sftperr != LIBSSH2_FX_OK ?
  1725. sftp_libssh2_error_to_CURLE(sftperr):CURLE_SSH;
  1726. if(!sshc->actualcode) {
  1727. /* Sometimes, for some reason libssh2_sftp_last_error() returns zero
  1728. even though libssh2_sftp_open() failed previously! We need to
  1729. work around that! */
  1730. sshc->actualcode = CURLE_SSH;
  1731. sftperr = LIBSSH2_FX_OK;
  1732. }
  1733. failf(data, "Upload failed: %s (%d/%d)",
  1734. sftperr != LIBSSH2_FX_OK ?
  1735. sftp_libssh2_strerror(sftperr):"ssh error",
  1736. sftperr, rc);
  1737. break;
  1738. }
  1739. /* If we have a restart point then we need to seek to the correct
  1740. position. */
  1741. if(data->state.resume_from > 0) {
  1742. /* Let's read off the proper amount of bytes from the input. */
  1743. if(conn->seek_func) {
  1744. Curl_set_in_callback(data, true);
  1745. seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
  1746. SEEK_SET);
  1747. Curl_set_in_callback(data, false);
  1748. }
  1749. if(seekerr != CURL_SEEKFUNC_OK) {
  1750. curl_off_t passed = 0;
  1751. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  1752. failf(data, "Could not seek stream");
  1753. return CURLE_FTP_COULDNT_USE_REST;
  1754. }
  1755. /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  1756. do {
  1757. size_t readthisamountnow =
  1758. (data->state.resume_from - passed > data->set.buffer_size) ?
  1759. (size_t)data->set.buffer_size :
  1760. curlx_sotouz(data->state.resume_from - passed);
  1761. size_t actuallyread;
  1762. Curl_set_in_callback(data, true);
  1763. actuallyread = data->state.fread_func(data->state.buffer, 1,
  1764. readthisamountnow,
  1765. data->state.in);
  1766. Curl_set_in_callback(data, false);
  1767. passed += actuallyread;
  1768. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  1769. /* this checks for greater-than only to make sure that the
  1770. CURL_READFUNC_ABORT return code still aborts */
  1771. failf(data, "Failed to read data");
  1772. return CURLE_FTP_COULDNT_USE_REST;
  1773. }
  1774. } while(passed < data->state.resume_from);
  1775. }
  1776. /* now, decrease the size of the read */
  1777. if(data->state.infilesize > 0) {
  1778. data->state.infilesize -= data->state.resume_from;
  1779. data->req.size = data->state.infilesize;
  1780. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  1781. }
  1782. SFTP_SEEK(sshc->sftp_handle, data->state.resume_from);
  1783. }
  1784. if(data->state.infilesize > 0) {
  1785. data->req.size = data->state.infilesize;
  1786. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  1787. }
  1788. /* upload data */
  1789. Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
  1790. /* not set by Curl_setup_transfer to preserve keepon bits */
  1791. conn->sockfd = conn->writesockfd;
  1792. if(result) {
  1793. state(conn, SSH_SFTP_CLOSE);
  1794. sshc->actualcode = result;
  1795. }
  1796. else {
  1797. /* store this original bitmask setup to use later on if we can't
  1798. figure out a "real" bitmask */
  1799. sshc->orig_waitfor = data->req.keepon;
  1800. /* we want to use the _sending_ function even when the socket turns
  1801. out readable as the underlying libssh2 sftp send function will deal
  1802. with both accordingly */
  1803. conn->cselect_bits = CURL_CSELECT_OUT;
  1804. /* since we don't really wait for anything at this point, we want the
  1805. state machine to move on as soon as possible so we set a very short
  1806. timeout here */
  1807. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1808. state(conn, SSH_STOP);
  1809. }
  1810. break;
  1811. }
  1812. case SSH_SFTP_CREATE_DIRS_INIT:
  1813. if(strlen(sftp_scp->path) > 1) {
  1814. sshc->slash_pos = sftp_scp->path + 1; /* ignore the leading '/' */
  1815. state(conn, SSH_SFTP_CREATE_DIRS);
  1816. }
  1817. else {
  1818. state(conn, SSH_SFTP_UPLOAD_INIT);
  1819. }
  1820. break;
  1821. case SSH_SFTP_CREATE_DIRS:
  1822. sshc->slash_pos = strchr(sshc->slash_pos, '/');
  1823. if(sshc->slash_pos) {
  1824. *sshc->slash_pos = 0;
  1825. infof(data, "Creating directory '%s'\n", sftp_scp->path);
  1826. state(conn, SSH_SFTP_CREATE_DIRS_MKDIR);
  1827. break;
  1828. }
  1829. state(conn, SSH_SFTP_UPLOAD_INIT);
  1830. break;
  1831. case SSH_SFTP_CREATE_DIRS_MKDIR:
  1832. /* 'mode' - parameter is preliminary - default to 0644 */
  1833. rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sftp_scp->path,
  1834. curlx_uztoui(strlen(sftp_scp->path)),
  1835. data->set.new_directory_perms);
  1836. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1837. break;
  1838. }
  1839. *sshc->slash_pos = '/';
  1840. ++sshc->slash_pos;
  1841. if(rc < 0) {
  1842. /*
  1843. * Abort if failure wasn't that the dir already exists or the
  1844. * permission was denied (creation might succeed further down the
  1845. * path) - retry on unspecific FAILURE also
  1846. */
  1847. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1848. if((sftperr != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
  1849. (sftperr != LIBSSH2_FX_FAILURE) &&
  1850. (sftperr != LIBSSH2_FX_PERMISSION_DENIED)) {
  1851. result = sftp_libssh2_error_to_CURLE(sftperr);
  1852. state(conn, SSH_SFTP_CLOSE);
  1853. sshc->actualcode = result?result:CURLE_SSH;
  1854. break;
  1855. }
  1856. rc = 0; /* clear rc and continue */
  1857. }
  1858. state(conn, SSH_SFTP_CREATE_DIRS);
  1859. break;
  1860. case SSH_SFTP_READDIR_INIT:
  1861. Curl_pgrsSetDownloadSize(data, -1);
  1862. if(data->set.opt_no_body) {
  1863. state(conn, SSH_STOP);
  1864. break;
  1865. }
  1866. /*
  1867. * This is a directory that we are trying to get, so produce a directory
  1868. * listing
  1869. */
  1870. sshc->sftp_handle = libssh2_sftp_open_ex(sshc->sftp_session,
  1871. sftp_scp->path,
  1872. curlx_uztoui(
  1873. strlen(sftp_scp->path)),
  1874. 0, 0, LIBSSH2_SFTP_OPENDIR);
  1875. if(!sshc->sftp_handle) {
  1876. if(libssh2_session_last_errno(sshc->ssh_session) ==
  1877. LIBSSH2_ERROR_EAGAIN) {
  1878. rc = LIBSSH2_ERROR_EAGAIN;
  1879. break;
  1880. }
  1881. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1882. failf(data, "Could not open directory for reading: %s",
  1883. sftp_libssh2_strerror(sftperr));
  1884. state(conn, SSH_SFTP_CLOSE);
  1885. result = sftp_libssh2_error_to_CURLE(sftperr);
  1886. sshc->actualcode = result?result:CURLE_SSH;
  1887. break;
  1888. }
  1889. sshc->readdir_filename = malloc(PATH_MAX + 1);
  1890. if(!sshc->readdir_filename) {
  1891. state(conn, SSH_SFTP_CLOSE);
  1892. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  1893. break;
  1894. }
  1895. sshc->readdir_longentry = malloc(PATH_MAX + 1);
  1896. if(!sshc->readdir_longentry) {
  1897. Curl_safefree(sshc->readdir_filename);
  1898. state(conn, SSH_SFTP_CLOSE);
  1899. sshc->actualcode = CURLE_OUT_OF_MEMORY;
  1900. break;
  1901. }
  1902. Curl_dyn_init(&sshc->readdir, PATH_MAX * 2);
  1903. state(conn, SSH_SFTP_READDIR);
  1904. break;
  1905. case SSH_SFTP_READDIR:
  1906. rc = libssh2_sftp_readdir_ex(sshc->sftp_handle,
  1907. sshc->readdir_filename,
  1908. PATH_MAX,
  1909. sshc->readdir_longentry,
  1910. PATH_MAX,
  1911. &sshc->readdir_attrs);
  1912. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1913. break;
  1914. }
  1915. if(rc > 0) {
  1916. readdir_len = (size_t) rc;
  1917. sshc->readdir_filename[readdir_len] = '\0';
  1918. if(data->set.ftp_list_only) {
  1919. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  1920. sshc->readdir_filename,
  1921. readdir_len);
  1922. if(!result)
  1923. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  1924. (char *)"\n", 1);
  1925. if(result) {
  1926. state(conn, SSH_STOP);
  1927. break;
  1928. }
  1929. /* since this counts what we send to the client, we include the
  1930. newline in this counter */
  1931. data->req.bytecount += readdir_len + 1;
  1932. /* output debug output if that is requested */
  1933. if(data->set.verbose) {
  1934. Curl_debug(data, CURLINFO_DATA_IN, sshc->readdir_filename,
  1935. readdir_len);
  1936. Curl_debug(data, CURLINFO_DATA_IN, (char *)"\n", 1);
  1937. }
  1938. }
  1939. else {
  1940. result = Curl_dyn_add(&sshc->readdir, sshc->readdir_longentry);
  1941. if(!result) {
  1942. if((sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
  1943. ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
  1944. LIBSSH2_SFTP_S_IFLNK)) {
  1945. Curl_dyn_init(&sshc->readdir_link, PATH_MAX);
  1946. result = Curl_dyn_add(&sshc->readdir_link, sftp_scp->path);
  1947. state(conn, SSH_SFTP_READDIR_LINK);
  1948. if(!result)
  1949. break;
  1950. }
  1951. else {
  1952. state(conn, SSH_SFTP_READDIR_BOTTOM);
  1953. break;
  1954. }
  1955. }
  1956. sshc->actualcode = result;
  1957. state(conn, SSH_SFTP_CLOSE);
  1958. break;
  1959. }
  1960. }
  1961. else if(rc == 0) {
  1962. Curl_safefree(sshc->readdir_filename);
  1963. Curl_safefree(sshc->readdir_longentry);
  1964. state(conn, SSH_SFTP_READDIR_DONE);
  1965. break;
  1966. }
  1967. else if(rc < 0) {
  1968. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  1969. result = sftp_libssh2_error_to_CURLE(sftperr);
  1970. sshc->actualcode = result?result:CURLE_SSH;
  1971. failf(data, "Could not open remote file for reading: %s :: %d",
  1972. sftp_libssh2_strerror(sftperr),
  1973. libssh2_session_last_errno(sshc->ssh_session));
  1974. Curl_safefree(sshc->readdir_filename);
  1975. Curl_safefree(sshc->readdir_longentry);
  1976. state(conn, SSH_SFTP_CLOSE);
  1977. break;
  1978. }
  1979. break;
  1980. case SSH_SFTP_READDIR_LINK:
  1981. rc =
  1982. libssh2_sftp_symlink_ex(sshc->sftp_session,
  1983. Curl_dyn_ptr(&sshc->readdir_link),
  1984. (int)Curl_dyn_len(&sshc->readdir_link),
  1985. sshc->readdir_filename,
  1986. PATH_MAX, LIBSSH2_SFTP_READLINK);
  1987. if(rc == LIBSSH2_ERROR_EAGAIN) {
  1988. break;
  1989. }
  1990. readdir_len = (size_t) rc;
  1991. Curl_dyn_free(&sshc->readdir_link);
  1992. /* append filename and extra output */
  1993. result = Curl_dyn_addf(&sshc->readdir, " -> %s", sshc->readdir_filename);
  1994. if(result) {
  1995. sshc->readdir_line = NULL;
  1996. Curl_safefree(sshc->readdir_filename);
  1997. Curl_safefree(sshc->readdir_longentry);
  1998. state(conn, SSH_SFTP_CLOSE);
  1999. sshc->actualcode = result;
  2000. break;
  2001. }
  2002. state(conn, SSH_SFTP_READDIR_BOTTOM);
  2003. break;
  2004. case SSH_SFTP_READDIR_BOTTOM:
  2005. result = Curl_dyn_addn(&sshc->readdir, "\n", 1);
  2006. if(!result)
  2007. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  2008. Curl_dyn_ptr(&sshc->readdir),
  2009. Curl_dyn_len(&sshc->readdir));
  2010. if(!result) {
  2011. /* output debug output if that is requested */
  2012. if(data->set.verbose) {
  2013. Curl_debug(data, CURLINFO_DATA_IN,
  2014. Curl_dyn_ptr(&sshc->readdir),
  2015. Curl_dyn_len(&sshc->readdir));
  2016. }
  2017. data->req.bytecount += Curl_dyn_len(&sshc->readdir);
  2018. }
  2019. if(result) {
  2020. Curl_dyn_free(&sshc->readdir);
  2021. state(conn, SSH_STOP);
  2022. }
  2023. else {
  2024. Curl_dyn_reset(&sshc->readdir);
  2025. state(conn, SSH_SFTP_READDIR);
  2026. }
  2027. break;
  2028. case SSH_SFTP_READDIR_DONE:
  2029. if(libssh2_sftp_closedir(sshc->sftp_handle) ==
  2030. LIBSSH2_ERROR_EAGAIN) {
  2031. rc = LIBSSH2_ERROR_EAGAIN;
  2032. break;
  2033. }
  2034. sshc->sftp_handle = NULL;
  2035. Curl_safefree(sshc->readdir_filename);
  2036. Curl_safefree(sshc->readdir_longentry);
  2037. /* no data to transfer */
  2038. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  2039. state(conn, SSH_STOP);
  2040. break;
  2041. case SSH_SFTP_DOWNLOAD_INIT:
  2042. /*
  2043. * Work on getting the specified file
  2044. */
  2045. sshc->sftp_handle =
  2046. libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path,
  2047. curlx_uztoui(strlen(sftp_scp->path)),
  2048. LIBSSH2_FXF_READ, data->set.new_file_perms,
  2049. LIBSSH2_SFTP_OPENFILE);
  2050. if(!sshc->sftp_handle) {
  2051. if(libssh2_session_last_errno(sshc->ssh_session) ==
  2052. LIBSSH2_ERROR_EAGAIN) {
  2053. rc = LIBSSH2_ERROR_EAGAIN;
  2054. break;
  2055. }
  2056. sftperr = libssh2_sftp_last_error(sshc->sftp_session);
  2057. failf(data, "Could not open remote file for reading: %s",
  2058. sftp_libssh2_strerror(sftperr));
  2059. state(conn, SSH_SFTP_CLOSE);
  2060. result = sftp_libssh2_error_to_CURLE(sftperr);
  2061. sshc->actualcode = result?result:CURLE_SSH;
  2062. break;
  2063. }
  2064. state(conn, SSH_SFTP_DOWNLOAD_STAT);
  2065. break;
  2066. case SSH_SFTP_DOWNLOAD_STAT:
  2067. {
  2068. LIBSSH2_SFTP_ATTRIBUTES attrs;
  2069. rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path,
  2070. curlx_uztoui(strlen(sftp_scp->path)),
  2071. LIBSSH2_SFTP_STAT, &attrs);
  2072. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2073. break;
  2074. }
  2075. if(rc ||
  2076. !(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) ||
  2077. (attrs.filesize == 0)) {
  2078. /*
  2079. * libssh2_sftp_open() didn't return an error, so maybe the server
  2080. * just doesn't support stat()
  2081. * OR the server doesn't return a file size with a stat()
  2082. * OR file size is 0
  2083. */
  2084. data->req.size = -1;
  2085. data->req.maxdownload = -1;
  2086. Curl_pgrsSetDownloadSize(data, -1);
  2087. }
  2088. else {
  2089. curl_off_t size = attrs.filesize;
  2090. if(size < 0) {
  2091. failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
  2092. return CURLE_BAD_DOWNLOAD_RESUME;
  2093. }
  2094. if(conn->data->state.use_range) {
  2095. curl_off_t from, to;
  2096. char *ptr;
  2097. char *ptr2;
  2098. CURLofft to_t;
  2099. CURLofft from_t;
  2100. from_t = curlx_strtoofft(conn->data->state.range, &ptr, 0, &from);
  2101. if(from_t == CURL_OFFT_FLOW)
  2102. return CURLE_RANGE_ERROR;
  2103. while(*ptr && (ISSPACE(*ptr) || (*ptr == '-')))
  2104. ptr++;
  2105. to_t = curlx_strtoofft(ptr, &ptr2, 0, &to);
  2106. if(to_t == CURL_OFFT_FLOW)
  2107. return CURLE_RANGE_ERROR;
  2108. if((to_t == CURL_OFFT_INVAL) /* no "to" value given */
  2109. || (to >= size)) {
  2110. to = size - 1;
  2111. }
  2112. if(from_t) {
  2113. /* from is relative to end of file */
  2114. from = size - to;
  2115. to = size - 1;
  2116. }
  2117. if(from > size) {
  2118. failf(data, "Offset (%"
  2119. CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
  2120. CURL_FORMAT_CURL_OFF_T ")", from, attrs.filesize);
  2121. return CURLE_BAD_DOWNLOAD_RESUME;
  2122. }
  2123. if(from > to) {
  2124. from = to;
  2125. size = 0;
  2126. }
  2127. else {
  2128. size = to - from + 1;
  2129. }
  2130. SFTP_SEEK(conn->proto.sshc.sftp_handle, from);
  2131. }
  2132. data->req.size = size;
  2133. data->req.maxdownload = size;
  2134. Curl_pgrsSetDownloadSize(data, size);
  2135. }
  2136. /* We can resume if we can seek to the resume position */
  2137. if(data->state.resume_from) {
  2138. if(data->state.resume_from < 0) {
  2139. /* We're supposed to download the last abs(from) bytes */
  2140. if((curl_off_t)attrs.filesize < -data->state.resume_from) {
  2141. failf(data, "Offset (%"
  2142. CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
  2143. CURL_FORMAT_CURL_OFF_T ")",
  2144. data->state.resume_from, attrs.filesize);
  2145. return CURLE_BAD_DOWNLOAD_RESUME;
  2146. }
  2147. /* download from where? */
  2148. data->state.resume_from += attrs.filesize;
  2149. }
  2150. else {
  2151. if((curl_off_t)attrs.filesize < data->state.resume_from) {
  2152. failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
  2153. ") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
  2154. data->state.resume_from, attrs.filesize);
  2155. return CURLE_BAD_DOWNLOAD_RESUME;
  2156. }
  2157. }
  2158. /* Now store the number of bytes we are expected to download */
  2159. data->req.size = attrs.filesize - data->state.resume_from;
  2160. data->req.maxdownload = attrs.filesize - data->state.resume_from;
  2161. Curl_pgrsSetDownloadSize(data,
  2162. attrs.filesize - data->state.resume_from);
  2163. SFTP_SEEK(sshc->sftp_handle, data->state.resume_from);
  2164. }
  2165. }
  2166. /* Setup the actual download */
  2167. if(data->req.size == 0) {
  2168. /* no data to transfer */
  2169. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  2170. infof(data, "File already completely downloaded\n");
  2171. state(conn, SSH_STOP);
  2172. break;
  2173. }
  2174. Curl_setup_transfer(data, FIRSTSOCKET, data->req.size, FALSE, -1);
  2175. /* not set by Curl_setup_transfer to preserve keepon bits */
  2176. conn->writesockfd = conn->sockfd;
  2177. /* we want to use the _receiving_ function even when the socket turns
  2178. out writableable as the underlying libssh2 recv function will deal
  2179. with both accordingly */
  2180. conn->cselect_bits = CURL_CSELECT_IN;
  2181. if(result) {
  2182. /* this should never occur; the close state should be entered
  2183. at the time the error occurs */
  2184. state(conn, SSH_SFTP_CLOSE);
  2185. sshc->actualcode = result;
  2186. }
  2187. else {
  2188. state(conn, SSH_STOP);
  2189. }
  2190. break;
  2191. case SSH_SFTP_CLOSE:
  2192. if(sshc->sftp_handle) {
  2193. rc = libssh2_sftp_close(sshc->sftp_handle);
  2194. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2195. break;
  2196. }
  2197. if(rc < 0) {
  2198. char *err_msg = NULL;
  2199. (void)libssh2_session_last_error(sshc->ssh_session,
  2200. &err_msg, NULL, 0);
  2201. infof(data, "Failed to close libssh2 file: %d %s\n", rc, err_msg);
  2202. }
  2203. sshc->sftp_handle = NULL;
  2204. }
  2205. Curl_safefree(sftp_scp->path);
  2206. DEBUGF(infof(data, "SFTP DONE done\n"));
  2207. /* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT
  2208. After nextstate is executed, the control should come back to
  2209. SSH_SFTP_CLOSE to pass the correct result back */
  2210. if(sshc->nextstate != SSH_NO_STATE &&
  2211. sshc->nextstate != SSH_SFTP_CLOSE) {
  2212. state(conn, sshc->nextstate);
  2213. sshc->nextstate = SSH_SFTP_CLOSE;
  2214. }
  2215. else {
  2216. state(conn, SSH_STOP);
  2217. result = sshc->actualcode;
  2218. }
  2219. break;
  2220. case SSH_SFTP_SHUTDOWN:
  2221. /* during times we get here due to a broken transfer and then the
  2222. sftp_handle might not have been taken down so make sure that is done
  2223. before we proceed */
  2224. if(sshc->sftp_handle) {
  2225. rc = libssh2_sftp_close(sshc->sftp_handle);
  2226. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2227. break;
  2228. }
  2229. if(rc < 0) {
  2230. char *err_msg = NULL;
  2231. (void)libssh2_session_last_error(sshc->ssh_session, &err_msg,
  2232. NULL, 0);
  2233. infof(data, "Failed to close libssh2 file: %d %s\n", rc, err_msg);
  2234. }
  2235. sshc->sftp_handle = NULL;
  2236. }
  2237. if(sshc->sftp_session) {
  2238. rc = libssh2_sftp_shutdown(sshc->sftp_session);
  2239. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2240. break;
  2241. }
  2242. if(rc < 0) {
  2243. infof(data, "Failed to stop libssh2 sftp subsystem\n");
  2244. }
  2245. sshc->sftp_session = NULL;
  2246. }
  2247. Curl_safefree(sshc->homedir);
  2248. conn->data->state.most_recent_ftp_entrypath = NULL;
  2249. state(conn, SSH_SESSION_DISCONNECT);
  2250. break;
  2251. case SSH_SCP_TRANS_INIT:
  2252. result = Curl_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
  2253. if(result) {
  2254. sshc->actualcode = result;
  2255. state(conn, SSH_STOP);
  2256. break;
  2257. }
  2258. if(data->set.upload) {
  2259. if(data->state.infilesize < 0) {
  2260. failf(data, "SCP requires a known file size for upload");
  2261. sshc->actualcode = CURLE_UPLOAD_FAILED;
  2262. state(conn, SSH_SCP_CHANNEL_FREE);
  2263. break;
  2264. }
  2265. state(conn, SSH_SCP_UPLOAD_INIT);
  2266. }
  2267. else {
  2268. state(conn, SSH_SCP_DOWNLOAD_INIT);
  2269. }
  2270. break;
  2271. case SSH_SCP_UPLOAD_INIT:
  2272. /*
  2273. * libssh2 requires that the destination path is a full path that
  2274. * includes the destination file and name OR ends in a "/" . If this is
  2275. * not done the destination file will be named the same name as the last
  2276. * directory in the path.
  2277. */
  2278. sshc->ssh_channel =
  2279. SCP_SEND(sshc->ssh_session, sftp_scp->path, data->set.new_file_perms,
  2280. data->state.infilesize);
  2281. if(!sshc->ssh_channel) {
  2282. int ssh_err;
  2283. char *err_msg = NULL;
  2284. if(libssh2_session_last_errno(sshc->ssh_session) ==
  2285. LIBSSH2_ERROR_EAGAIN) {
  2286. rc = LIBSSH2_ERROR_EAGAIN;
  2287. break;
  2288. }
  2289. ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
  2290. &err_msg, NULL, 0));
  2291. failf(conn->data, "%s", err_msg);
  2292. state(conn, SSH_SCP_CHANNEL_FREE);
  2293. sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
  2294. /* Map generic errors to upload failed */
  2295. if(sshc->actualcode == CURLE_SSH ||
  2296. sshc->actualcode == CURLE_REMOTE_FILE_NOT_FOUND)
  2297. sshc->actualcode = CURLE_UPLOAD_FAILED;
  2298. break;
  2299. }
  2300. /* upload data */
  2301. data->req.size = data->state.infilesize;
  2302. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  2303. Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
  2304. /* not set by Curl_setup_transfer to preserve keepon bits */
  2305. conn->sockfd = conn->writesockfd;
  2306. if(result) {
  2307. state(conn, SSH_SCP_CHANNEL_FREE);
  2308. sshc->actualcode = result;
  2309. }
  2310. else {
  2311. /* store this original bitmask setup to use later on if we can't
  2312. figure out a "real" bitmask */
  2313. sshc->orig_waitfor = data->req.keepon;
  2314. /* we want to use the _sending_ function even when the socket turns
  2315. out readable as the underlying libssh2 scp send function will deal
  2316. with both accordingly */
  2317. conn->cselect_bits = CURL_CSELECT_OUT;
  2318. state(conn, SSH_STOP);
  2319. }
  2320. break;
  2321. case SSH_SCP_DOWNLOAD_INIT:
  2322. {
  2323. curl_off_t bytecount;
  2324. /*
  2325. * We must check the remote file; if it is a directory no values will
  2326. * be set in sb
  2327. */
  2328. /*
  2329. * If support for >2GB files exists, use it.
  2330. */
  2331. /* get a fresh new channel from the ssh layer */
  2332. #if LIBSSH2_VERSION_NUM < 0x010700
  2333. struct stat sb;
  2334. memset(&sb, 0, sizeof(struct stat));
  2335. sshc->ssh_channel = libssh2_scp_recv(sshc->ssh_session,
  2336. sftp_scp->path, &sb);
  2337. #else
  2338. libssh2_struct_stat sb;
  2339. memset(&sb, 0, sizeof(libssh2_struct_stat));
  2340. sshc->ssh_channel = libssh2_scp_recv2(sshc->ssh_session,
  2341. sftp_scp->path, &sb);
  2342. #endif
  2343. if(!sshc->ssh_channel) {
  2344. int ssh_err;
  2345. char *err_msg = NULL;
  2346. if(libssh2_session_last_errno(sshc->ssh_session) ==
  2347. LIBSSH2_ERROR_EAGAIN) {
  2348. rc = LIBSSH2_ERROR_EAGAIN;
  2349. break;
  2350. }
  2351. ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
  2352. &err_msg, NULL, 0));
  2353. failf(conn->data, "%s", err_msg);
  2354. state(conn, SSH_SCP_CHANNEL_FREE);
  2355. sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
  2356. break;
  2357. }
  2358. /* download data */
  2359. bytecount = (curl_off_t)sb.st_size;
  2360. data->req.maxdownload = (curl_off_t)sb.st_size;
  2361. Curl_setup_transfer(data, FIRSTSOCKET, bytecount, FALSE, -1);
  2362. /* not set by Curl_setup_transfer to preserve keepon bits */
  2363. conn->writesockfd = conn->sockfd;
  2364. /* we want to use the _receiving_ function even when the socket turns
  2365. out writableable as the underlying libssh2 recv function will deal
  2366. with both accordingly */
  2367. conn->cselect_bits = CURL_CSELECT_IN;
  2368. if(result) {
  2369. state(conn, SSH_SCP_CHANNEL_FREE);
  2370. sshc->actualcode = result;
  2371. }
  2372. else
  2373. state(conn, SSH_STOP);
  2374. }
  2375. break;
  2376. case SSH_SCP_DONE:
  2377. if(data->set.upload)
  2378. state(conn, SSH_SCP_SEND_EOF);
  2379. else
  2380. state(conn, SSH_SCP_CHANNEL_FREE);
  2381. break;
  2382. case SSH_SCP_SEND_EOF:
  2383. if(sshc->ssh_channel) {
  2384. rc = libssh2_channel_send_eof(sshc->ssh_channel);
  2385. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2386. break;
  2387. }
  2388. if(rc) {
  2389. char *err_msg = NULL;
  2390. (void)libssh2_session_last_error(sshc->ssh_session,
  2391. &err_msg, NULL, 0);
  2392. infof(data, "Failed to send libssh2 channel EOF: %d %s\n",
  2393. rc, err_msg);
  2394. }
  2395. }
  2396. state(conn, SSH_SCP_WAIT_EOF);
  2397. break;
  2398. case SSH_SCP_WAIT_EOF:
  2399. if(sshc->ssh_channel) {
  2400. rc = libssh2_channel_wait_eof(sshc->ssh_channel);
  2401. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2402. break;
  2403. }
  2404. if(rc) {
  2405. char *err_msg = NULL;
  2406. (void)libssh2_session_last_error(sshc->ssh_session,
  2407. &err_msg, NULL, 0);
  2408. infof(data, "Failed to get channel EOF: %d %s\n", rc, err_msg);
  2409. }
  2410. }
  2411. state(conn, SSH_SCP_WAIT_CLOSE);
  2412. break;
  2413. case SSH_SCP_WAIT_CLOSE:
  2414. if(sshc->ssh_channel) {
  2415. rc = libssh2_channel_wait_closed(sshc->ssh_channel);
  2416. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2417. break;
  2418. }
  2419. if(rc) {
  2420. char *err_msg = NULL;
  2421. (void)libssh2_session_last_error(sshc->ssh_session,
  2422. &err_msg, NULL, 0);
  2423. infof(data, "Channel failed to close: %d %s\n", rc, err_msg);
  2424. }
  2425. }
  2426. state(conn, SSH_SCP_CHANNEL_FREE);
  2427. break;
  2428. case SSH_SCP_CHANNEL_FREE:
  2429. if(sshc->ssh_channel) {
  2430. rc = libssh2_channel_free(sshc->ssh_channel);
  2431. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2432. break;
  2433. }
  2434. if(rc < 0) {
  2435. char *err_msg = NULL;
  2436. (void)libssh2_session_last_error(sshc->ssh_session,
  2437. &err_msg, NULL, 0);
  2438. infof(data, "Failed to free libssh2 scp subsystem: %d %s\n",
  2439. rc, err_msg);
  2440. }
  2441. sshc->ssh_channel = NULL;
  2442. }
  2443. DEBUGF(infof(data, "SCP DONE phase complete\n"));
  2444. #if 0 /* PREV */
  2445. state(conn, SSH_SESSION_DISCONNECT);
  2446. #endif
  2447. state(conn, SSH_STOP);
  2448. result = sshc->actualcode;
  2449. break;
  2450. case SSH_SESSION_DISCONNECT:
  2451. /* during weird times when we've been prematurely aborted, the channel
  2452. is still alive when we reach this state and we MUST kill the channel
  2453. properly first */
  2454. if(sshc->ssh_channel) {
  2455. rc = libssh2_channel_free(sshc->ssh_channel);
  2456. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2457. break;
  2458. }
  2459. if(rc < 0) {
  2460. char *err_msg = NULL;
  2461. (void)libssh2_session_last_error(sshc->ssh_session,
  2462. &err_msg, NULL, 0);
  2463. infof(data, "Failed to free libssh2 scp subsystem: %d %s\n",
  2464. rc, err_msg);
  2465. }
  2466. sshc->ssh_channel = NULL;
  2467. }
  2468. if(sshc->ssh_session) {
  2469. rc = libssh2_session_disconnect(sshc->ssh_session, "Shutdown");
  2470. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2471. break;
  2472. }
  2473. if(rc < 0) {
  2474. char *err_msg = NULL;
  2475. (void)libssh2_session_last_error(sshc->ssh_session,
  2476. &err_msg, NULL, 0);
  2477. infof(data, "Failed to disconnect libssh2 session: %d %s\n",
  2478. rc, err_msg);
  2479. }
  2480. }
  2481. Curl_safefree(sshc->homedir);
  2482. conn->data->state.most_recent_ftp_entrypath = NULL;
  2483. state(conn, SSH_SESSION_FREE);
  2484. break;
  2485. case SSH_SESSION_FREE:
  2486. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  2487. if(sshc->kh) {
  2488. libssh2_knownhost_free(sshc->kh);
  2489. sshc->kh = NULL;
  2490. }
  2491. #endif
  2492. #ifdef HAVE_LIBSSH2_AGENT_API
  2493. if(sshc->ssh_agent) {
  2494. rc = libssh2_agent_disconnect(sshc->ssh_agent);
  2495. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2496. break;
  2497. }
  2498. if(rc < 0) {
  2499. char *err_msg = NULL;
  2500. (void)libssh2_session_last_error(sshc->ssh_session,
  2501. &err_msg, NULL, 0);
  2502. infof(data, "Failed to disconnect from libssh2 agent: %d %s\n",
  2503. rc, err_msg);
  2504. }
  2505. libssh2_agent_free(sshc->ssh_agent);
  2506. sshc->ssh_agent = NULL;
  2507. /* NB: there is no need to free identities, they are part of internal
  2508. agent stuff */
  2509. sshc->sshagent_identity = NULL;
  2510. sshc->sshagent_prev_identity = NULL;
  2511. }
  2512. #endif
  2513. if(sshc->ssh_session) {
  2514. rc = libssh2_session_free(sshc->ssh_session);
  2515. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2516. break;
  2517. }
  2518. if(rc < 0) {
  2519. char *err_msg = NULL;
  2520. (void)libssh2_session_last_error(sshc->ssh_session,
  2521. &err_msg, NULL, 0);
  2522. infof(data, "Failed to free libssh2 session: %d %s\n", rc, err_msg);
  2523. }
  2524. sshc->ssh_session = NULL;
  2525. }
  2526. /* worst-case scenario cleanup */
  2527. DEBUGASSERT(sshc->ssh_session == NULL);
  2528. DEBUGASSERT(sshc->ssh_channel == NULL);
  2529. DEBUGASSERT(sshc->sftp_session == NULL);
  2530. DEBUGASSERT(sshc->sftp_handle == NULL);
  2531. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  2532. DEBUGASSERT(sshc->kh == NULL);
  2533. #endif
  2534. #ifdef HAVE_LIBSSH2_AGENT_API
  2535. DEBUGASSERT(sshc->ssh_agent == NULL);
  2536. #endif
  2537. Curl_safefree(sshc->rsa_pub);
  2538. Curl_safefree(sshc->rsa);
  2539. Curl_safefree(sshc->quote_path1);
  2540. Curl_safefree(sshc->quote_path2);
  2541. Curl_safefree(sshc->homedir);
  2542. Curl_safefree(sshc->readdir_filename);
  2543. Curl_safefree(sshc->readdir_longentry);
  2544. Curl_safefree(sshc->readdir_line);
  2545. Curl_dyn_free(&sshc->readdir);
  2546. /* the code we are about to return */
  2547. result = sshc->actualcode;
  2548. memset(sshc, 0, sizeof(struct ssh_conn));
  2549. connclose(conn, "SSH session free");
  2550. sshc->state = SSH_SESSION_FREE; /* current */
  2551. sshc->nextstate = SSH_NO_STATE;
  2552. state(conn, SSH_STOP);
  2553. break;
  2554. case SSH_QUIT:
  2555. /* fallthrough, just stop! */
  2556. default:
  2557. /* internal error */
  2558. sshc->nextstate = SSH_NO_STATE;
  2559. state(conn, SSH_STOP);
  2560. break;
  2561. }
  2562. } while(!rc && (sshc->state != SSH_STOP));
  2563. if(rc == LIBSSH2_ERROR_EAGAIN) {
  2564. /* we would block, we need to wait for the socket to be ready (in the
  2565. right direction too)! */
  2566. *block = TRUE;
  2567. }
  2568. return result;
  2569. }
  2570. /* called by the multi interface to figure out what socket(s) to wait for and
  2571. for what actions in the DO_DONE, PERFORM and WAITPERFORM states */
  2572. static int ssh_perform_getsock(const struct connectdata *conn,
  2573. curl_socket_t *sock)
  2574. {
  2575. #ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
  2576. int bitmap = GETSOCK_BLANK;
  2577. sock[0] = conn->sock[FIRSTSOCKET];
  2578. if(conn->waitfor & KEEP_RECV)
  2579. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  2580. if(conn->waitfor & KEEP_SEND)
  2581. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  2582. return bitmap;
  2583. #else
  2584. /* if we don't know the direction we can use the generic *_getsock()
  2585. function even for the protocol_connect and doing states */
  2586. return Curl_single_getsock(conn, sock);
  2587. #endif
  2588. }
  2589. /* Generic function called by the multi interface to figure out what socket(s)
  2590. to wait for and for what actions during the DOING and PROTOCONNECT states*/
  2591. static int ssh_getsock(struct connectdata *conn,
  2592. curl_socket_t *sock)
  2593. {
  2594. #ifndef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
  2595. (void)conn;
  2596. (void)sock;
  2597. /* if we don't know any direction we can just play along as we used to and
  2598. not provide any sensible info */
  2599. return GETSOCK_BLANK;
  2600. #else
  2601. /* if we know the direction we can use the generic *_getsock() function even
  2602. for the protocol_connect and doing states */
  2603. return ssh_perform_getsock(conn, sock);
  2604. #endif
  2605. }
  2606. #ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
  2607. /*
  2608. * When one of the libssh2 functions has returned LIBSSH2_ERROR_EAGAIN this
  2609. * function is used to figure out in what direction and stores this info so
  2610. * that the multi interface can take advantage of it. Make sure to call this
  2611. * function in all cases so that when it _doesn't_ return EAGAIN we can
  2612. * restore the default wait bits.
  2613. */
  2614. static void ssh_block2waitfor(struct connectdata *conn, bool block)
  2615. {
  2616. struct ssh_conn *sshc = &conn->proto.sshc;
  2617. int dir = 0;
  2618. if(block) {
  2619. dir = libssh2_session_block_directions(sshc->ssh_session);
  2620. if(dir) {
  2621. /* translate the libssh2 define bits into our own bit defines */
  2622. conn->waitfor = ((dir&LIBSSH2_SESSION_BLOCK_INBOUND)?KEEP_RECV:0) |
  2623. ((dir&LIBSSH2_SESSION_BLOCK_OUTBOUND)?KEEP_SEND:0);
  2624. }
  2625. }
  2626. if(!dir)
  2627. /* It didn't block or libssh2 didn't reveal in which direction, put back
  2628. the original set */
  2629. conn->waitfor = sshc->orig_waitfor;
  2630. }
  2631. #else
  2632. /* no libssh2 directional support so we simply don't know */
  2633. #define ssh_block2waitfor(x,y) Curl_nop_stmt
  2634. #endif
  2635. /* called repeatedly until done from multi.c */
  2636. static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done)
  2637. {
  2638. struct ssh_conn *sshc = &conn->proto.sshc;
  2639. CURLcode result = CURLE_OK;
  2640. bool block; /* we store the status and use that to provide a ssh_getsock()
  2641. implementation */
  2642. do {
  2643. result = ssh_statemach_act(conn, &block);
  2644. *done = (sshc->state == SSH_STOP) ? TRUE : FALSE;
  2645. /* if there's no error, it isn't done and it didn't EWOULDBLOCK, then
  2646. try again */
  2647. } while(!result && !*done && !block);
  2648. ssh_block2waitfor(conn, block);
  2649. return result;
  2650. }
  2651. static CURLcode ssh_block_statemach(struct connectdata *conn,
  2652. bool duringconnect)
  2653. {
  2654. struct ssh_conn *sshc = &conn->proto.sshc;
  2655. CURLcode result = CURLE_OK;
  2656. struct Curl_easy *data = conn->data;
  2657. while((sshc->state != SSH_STOP) && !result) {
  2658. bool block;
  2659. timediff_t left = 1000;
  2660. struct curltime now = Curl_now();
  2661. result = ssh_statemach_act(conn, &block);
  2662. if(result)
  2663. break;
  2664. if(Curl_pgrsUpdate(conn))
  2665. return CURLE_ABORTED_BY_CALLBACK;
  2666. result = Curl_speedcheck(data, now);
  2667. if(result)
  2668. break;
  2669. left = Curl_timeleft(data, NULL, duringconnect);
  2670. if(left < 0) {
  2671. failf(data, "Operation timed out");
  2672. return CURLE_OPERATION_TIMEDOUT;
  2673. }
  2674. #ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
  2675. if(block) {
  2676. int dir = libssh2_session_block_directions(sshc->ssh_session);
  2677. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  2678. curl_socket_t fd_read = CURL_SOCKET_BAD;
  2679. curl_socket_t fd_write = CURL_SOCKET_BAD;
  2680. if(LIBSSH2_SESSION_BLOCK_INBOUND & dir)
  2681. fd_read = sock;
  2682. if(LIBSSH2_SESSION_BLOCK_OUTBOUND & dir)
  2683. fd_write = sock;
  2684. /* wait for the socket to become ready */
  2685. (void)Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write,
  2686. left>1000?1000:left);
  2687. }
  2688. #endif
  2689. }
  2690. return result;
  2691. }
  2692. /*
  2693. * SSH setup and connection
  2694. */
  2695. static CURLcode ssh_setup_connection(struct connectdata *conn)
  2696. {
  2697. struct SSHPROTO *ssh;
  2698. conn->data->req.protop = ssh = calloc(1, sizeof(struct SSHPROTO));
  2699. if(!ssh)
  2700. return CURLE_OUT_OF_MEMORY;
  2701. return CURLE_OK;
  2702. }
  2703. static Curl_recv scp_recv, sftp_recv;
  2704. static Curl_send scp_send, sftp_send;
  2705. /*
  2706. * Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
  2707. * do protocol-specific actions at connect-time.
  2708. */
  2709. static CURLcode ssh_connect(struct connectdata *conn, bool *done)
  2710. {
  2711. #ifdef CURL_LIBSSH2_DEBUG
  2712. curl_socket_t sock;
  2713. #endif
  2714. struct ssh_conn *ssh;
  2715. CURLcode result;
  2716. struct Curl_easy *data = conn->data;
  2717. /* initialize per-handle data if not already */
  2718. if(!data->req.protop)
  2719. ssh_setup_connection(conn);
  2720. /* We default to persistent connections. We set this already in this connect
  2721. function to make the re-use checks properly be able to check this bit. */
  2722. connkeep(conn, "SSH default");
  2723. if(conn->handler->protocol & CURLPROTO_SCP) {
  2724. conn->recv[FIRSTSOCKET] = scp_recv;
  2725. conn->send[FIRSTSOCKET] = scp_send;
  2726. }
  2727. else {
  2728. conn->recv[FIRSTSOCKET] = sftp_recv;
  2729. conn->send[FIRSTSOCKET] = sftp_send;
  2730. }
  2731. ssh = &conn->proto.sshc;
  2732. #ifdef CURL_LIBSSH2_DEBUG
  2733. if(conn->user) {
  2734. infof(data, "User: %s\n", conn->user);
  2735. }
  2736. if(conn->passwd) {
  2737. infof(data, "Password: %s\n", conn->passwd);
  2738. }
  2739. sock = conn->sock[FIRSTSOCKET];
  2740. #endif /* CURL_LIBSSH2_DEBUG */
  2741. ssh->ssh_session = libssh2_session_init_ex(my_libssh2_malloc,
  2742. my_libssh2_free,
  2743. my_libssh2_realloc, conn);
  2744. if(ssh->ssh_session == NULL) {
  2745. failf(data, "Failure initialising ssh session");
  2746. return CURLE_FAILED_INIT;
  2747. }
  2748. if(data->set.ssh_compression) {
  2749. #if LIBSSH2_VERSION_NUM >= 0x010208
  2750. if(libssh2_session_flag(ssh->ssh_session, LIBSSH2_FLAG_COMPRESS, 1) < 0)
  2751. #endif
  2752. infof(data, "Failed to enable compression for ssh session\n");
  2753. }
  2754. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  2755. if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
  2756. int rc;
  2757. ssh->kh = libssh2_knownhost_init(ssh->ssh_session);
  2758. if(!ssh->kh) {
  2759. libssh2_session_free(ssh->ssh_session);
  2760. return CURLE_FAILED_INIT;
  2761. }
  2762. /* read all known hosts from there */
  2763. rc = libssh2_knownhost_readfile(ssh->kh,
  2764. data->set.str[STRING_SSH_KNOWNHOSTS],
  2765. LIBSSH2_KNOWNHOST_FILE_OPENSSH);
  2766. if(rc < 0)
  2767. infof(data, "Failed to read known hosts from %s\n",
  2768. data->set.str[STRING_SSH_KNOWNHOSTS]);
  2769. }
  2770. #endif /* HAVE_LIBSSH2_KNOWNHOST_API */
  2771. #ifdef CURL_LIBSSH2_DEBUG
  2772. libssh2_trace(ssh->ssh_session, ~0);
  2773. infof(data, "SSH socket: %d\n", (int)sock);
  2774. #endif /* CURL_LIBSSH2_DEBUG */
  2775. state(conn, SSH_INIT);
  2776. result = ssh_multi_statemach(conn, done);
  2777. return result;
  2778. }
  2779. /*
  2780. ***********************************************************************
  2781. *
  2782. * scp_perform()
  2783. *
  2784. * This is the actual DO function for SCP. Get a file according to
  2785. * the options previously setup.
  2786. */
  2787. static
  2788. CURLcode scp_perform(struct connectdata *conn,
  2789. bool *connected,
  2790. bool *dophase_done)
  2791. {
  2792. CURLcode result = CURLE_OK;
  2793. DEBUGF(infof(conn->data, "DO phase starts\n"));
  2794. *dophase_done = FALSE; /* not done yet */
  2795. /* start the first command in the DO phase */
  2796. state(conn, SSH_SCP_TRANS_INIT);
  2797. /* run the state-machine */
  2798. result = ssh_multi_statemach(conn, dophase_done);
  2799. *connected = conn->bits.tcpconnect[FIRSTSOCKET];
  2800. if(*dophase_done) {
  2801. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  2802. }
  2803. return result;
  2804. }
  2805. /* called from multi.c while DOing */
  2806. static CURLcode scp_doing(struct connectdata *conn,
  2807. bool *dophase_done)
  2808. {
  2809. CURLcode result;
  2810. result = ssh_multi_statemach(conn, dophase_done);
  2811. if(*dophase_done) {
  2812. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  2813. }
  2814. return result;
  2815. }
  2816. /*
  2817. * The DO function is generic for both protocols. There was previously two
  2818. * separate ones but this way means less duplicated code.
  2819. */
  2820. static CURLcode ssh_do(struct connectdata *conn, bool *done)
  2821. {
  2822. CURLcode result;
  2823. bool connected = 0;
  2824. struct Curl_easy *data = conn->data;
  2825. struct ssh_conn *sshc = &conn->proto.sshc;
  2826. *done = FALSE; /* default to false */
  2827. data->req.size = -1; /* make sure this is unknown at this point */
  2828. sshc->actualcode = CURLE_OK; /* reset error code */
  2829. sshc->secondCreateDirs = 0; /* reset the create dir attempt state
  2830. variable */
  2831. Curl_pgrsSetUploadCounter(data, 0);
  2832. Curl_pgrsSetDownloadCounter(data, 0);
  2833. Curl_pgrsSetUploadSize(data, -1);
  2834. Curl_pgrsSetDownloadSize(data, -1);
  2835. if(conn->handler->protocol & CURLPROTO_SCP)
  2836. result = scp_perform(conn, &connected, done);
  2837. else
  2838. result = sftp_perform(conn, &connected, done);
  2839. return result;
  2840. }
  2841. /* BLOCKING, but the function is using the state machine so the only reason
  2842. this is still blocking is that the multi interface code has no support for
  2843. disconnecting operations that takes a while */
  2844. static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection)
  2845. {
  2846. CURLcode result = CURLE_OK;
  2847. struct ssh_conn *ssh = &conn->proto.sshc;
  2848. (void) dead_connection;
  2849. if(ssh->ssh_session) {
  2850. /* only if there's a session still around to use! */
  2851. state(conn, SSH_SESSION_DISCONNECT);
  2852. result = ssh_block_statemach(conn, FALSE);
  2853. }
  2854. return result;
  2855. }
  2856. /* generic done function for both SCP and SFTP called from their specific
  2857. done functions */
  2858. static CURLcode ssh_done(struct connectdata *conn, CURLcode status)
  2859. {
  2860. CURLcode result = CURLE_OK;
  2861. struct SSHPROTO *sftp_scp = conn->data->req.protop;
  2862. if(!status) {
  2863. /* run the state-machine */
  2864. result = ssh_block_statemach(conn, FALSE);
  2865. }
  2866. else
  2867. result = status;
  2868. if(sftp_scp)
  2869. Curl_safefree(sftp_scp->path);
  2870. if(Curl_pgrsDone(conn))
  2871. return CURLE_ABORTED_BY_CALLBACK;
  2872. conn->data->req.keepon = 0; /* clear all bits */
  2873. return result;
  2874. }
  2875. static CURLcode scp_done(struct connectdata *conn, CURLcode status,
  2876. bool premature)
  2877. {
  2878. (void)premature; /* not used */
  2879. if(!status)
  2880. state(conn, SSH_SCP_DONE);
  2881. return ssh_done(conn, status);
  2882. }
  2883. static ssize_t scp_send(struct connectdata *conn, int sockindex,
  2884. const void *mem, size_t len, CURLcode *err)
  2885. {
  2886. ssize_t nwrite;
  2887. (void)sockindex; /* we only support SCP on the fixed known primary socket */
  2888. /* libssh2_channel_write() returns int! */
  2889. nwrite = (ssize_t)
  2890. libssh2_channel_write(conn->proto.sshc.ssh_channel, mem, len);
  2891. ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
  2892. if(nwrite == LIBSSH2_ERROR_EAGAIN) {
  2893. *err = CURLE_AGAIN;
  2894. nwrite = 0;
  2895. }
  2896. else if(nwrite < LIBSSH2_ERROR_NONE) {
  2897. *err = libssh2_session_error_to_CURLE((int)nwrite);
  2898. nwrite = -1;
  2899. }
  2900. return nwrite;
  2901. }
  2902. static ssize_t scp_recv(struct connectdata *conn, int sockindex,
  2903. char *mem, size_t len, CURLcode *err)
  2904. {
  2905. ssize_t nread;
  2906. (void)sockindex; /* we only support SCP on the fixed known primary socket */
  2907. /* libssh2_channel_read() returns int */
  2908. nread = (ssize_t)
  2909. libssh2_channel_read(conn->proto.sshc.ssh_channel, mem, len);
  2910. ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
  2911. if(nread == LIBSSH2_ERROR_EAGAIN) {
  2912. *err = CURLE_AGAIN;
  2913. nread = -1;
  2914. }
  2915. return nread;
  2916. }
  2917. /*
  2918. * =============== SFTP ===============
  2919. */
  2920. /*
  2921. ***********************************************************************
  2922. *
  2923. * sftp_perform()
  2924. *
  2925. * This is the actual DO function for SFTP. Get a file/directory according to
  2926. * the options previously setup.
  2927. */
  2928. static
  2929. CURLcode sftp_perform(struct connectdata *conn,
  2930. bool *connected,
  2931. bool *dophase_done)
  2932. {
  2933. CURLcode result = CURLE_OK;
  2934. DEBUGF(infof(conn->data, "DO phase starts\n"));
  2935. *dophase_done = FALSE; /* not done yet */
  2936. /* start the first command in the DO phase */
  2937. state(conn, SSH_SFTP_QUOTE_INIT);
  2938. /* run the state-machine */
  2939. result = ssh_multi_statemach(conn, dophase_done);
  2940. *connected = conn->bits.tcpconnect[FIRSTSOCKET];
  2941. if(*dophase_done) {
  2942. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  2943. }
  2944. return result;
  2945. }
  2946. /* called from multi.c while DOing */
  2947. static CURLcode sftp_doing(struct connectdata *conn,
  2948. bool *dophase_done)
  2949. {
  2950. CURLcode result = ssh_multi_statemach(conn, dophase_done);
  2951. if(*dophase_done) {
  2952. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  2953. }
  2954. return result;
  2955. }
  2956. /* BLOCKING, but the function is using the state machine so the only reason
  2957. this is still blocking is that the multi interface code has no support for
  2958. disconnecting operations that takes a while */
  2959. static CURLcode sftp_disconnect(struct connectdata *conn, bool dead_connection)
  2960. {
  2961. CURLcode result = CURLE_OK;
  2962. (void) dead_connection;
  2963. DEBUGF(infof(conn->data, "SSH DISCONNECT starts now\n"));
  2964. if(conn->proto.sshc.ssh_session) {
  2965. /* only if there's a session still around to use! */
  2966. state(conn, SSH_SFTP_SHUTDOWN);
  2967. result = ssh_block_statemach(conn, FALSE);
  2968. }
  2969. DEBUGF(infof(conn->data, "SSH DISCONNECT is done\n"));
  2970. return result;
  2971. }
  2972. static CURLcode sftp_done(struct connectdata *conn, CURLcode status,
  2973. bool premature)
  2974. {
  2975. struct ssh_conn *sshc = &conn->proto.sshc;
  2976. if(!status) {
  2977. /* Post quote commands are executed after the SFTP_CLOSE state to avoid
  2978. errors that could happen due to open file handles during POSTQUOTE
  2979. operation */
  2980. if(!premature && conn->data->set.postquote && !conn->bits.retry)
  2981. sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT;
  2982. state(conn, SSH_SFTP_CLOSE);
  2983. }
  2984. return ssh_done(conn, status);
  2985. }
  2986. /* return number of sent bytes */
  2987. static ssize_t sftp_send(struct connectdata *conn, int sockindex,
  2988. const void *mem, size_t len, CURLcode *err)
  2989. {
  2990. ssize_t nwrite; /* libssh2_sftp_write() used to return size_t in 0.14
  2991. but is changed to ssize_t in 0.15. These days we don't
  2992. support libssh2 0.15*/
  2993. (void)sockindex;
  2994. nwrite = libssh2_sftp_write(conn->proto.sshc.sftp_handle, mem, len);
  2995. ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
  2996. if(nwrite == LIBSSH2_ERROR_EAGAIN) {
  2997. *err = CURLE_AGAIN;
  2998. nwrite = 0;
  2999. }
  3000. else if(nwrite < LIBSSH2_ERROR_NONE) {
  3001. *err = libssh2_session_error_to_CURLE((int)nwrite);
  3002. nwrite = -1;
  3003. }
  3004. return nwrite;
  3005. }
  3006. /*
  3007. * Return number of received (decrypted) bytes
  3008. * or <0 on error
  3009. */
  3010. static ssize_t sftp_recv(struct connectdata *conn, int sockindex,
  3011. char *mem, size_t len, CURLcode *err)
  3012. {
  3013. ssize_t nread;
  3014. (void)sockindex;
  3015. nread = libssh2_sftp_read(conn->proto.sshc.sftp_handle, mem, len);
  3016. ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
  3017. if(nread == LIBSSH2_ERROR_EAGAIN) {
  3018. *err = CURLE_AGAIN;
  3019. nread = -1;
  3020. }
  3021. else if(nread < 0) {
  3022. *err = libssh2_session_error_to_CURLE((int)nread);
  3023. }
  3024. return nread;
  3025. }
  3026. static const char *sftp_libssh2_strerror(unsigned long err)
  3027. {
  3028. switch(err) {
  3029. case LIBSSH2_FX_NO_SUCH_FILE:
  3030. return "No such file or directory";
  3031. case LIBSSH2_FX_PERMISSION_DENIED:
  3032. return "Permission denied";
  3033. case LIBSSH2_FX_FAILURE:
  3034. return "Operation failed";
  3035. case LIBSSH2_FX_BAD_MESSAGE:
  3036. return "Bad message from SFTP server";
  3037. case LIBSSH2_FX_NO_CONNECTION:
  3038. return "Not connected to SFTP server";
  3039. case LIBSSH2_FX_CONNECTION_LOST:
  3040. return "Connection to SFTP server lost";
  3041. case LIBSSH2_FX_OP_UNSUPPORTED:
  3042. return "Operation not supported by SFTP server";
  3043. case LIBSSH2_FX_INVALID_HANDLE:
  3044. return "Invalid handle";
  3045. case LIBSSH2_FX_NO_SUCH_PATH:
  3046. return "No such file or directory";
  3047. case LIBSSH2_FX_FILE_ALREADY_EXISTS:
  3048. return "File already exists";
  3049. case LIBSSH2_FX_WRITE_PROTECT:
  3050. return "File is write protected";
  3051. case LIBSSH2_FX_NO_MEDIA:
  3052. return "No media";
  3053. case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
  3054. return "Disk full";
  3055. case LIBSSH2_FX_QUOTA_EXCEEDED:
  3056. return "User quota exceeded";
  3057. case LIBSSH2_FX_UNKNOWN_PRINCIPLE:
  3058. return "Unknown principle";
  3059. case LIBSSH2_FX_LOCK_CONFlICT:
  3060. return "File lock conflict";
  3061. case LIBSSH2_FX_DIR_NOT_EMPTY:
  3062. return "Directory not empty";
  3063. case LIBSSH2_FX_NOT_A_DIRECTORY:
  3064. return "Not a directory";
  3065. case LIBSSH2_FX_INVALID_FILENAME:
  3066. return "Invalid filename";
  3067. case LIBSSH2_FX_LINK_LOOP:
  3068. return "Link points to itself";
  3069. }
  3070. return "Unknown error in libssh2";
  3071. }
  3072. CURLcode Curl_ssh_init(void)
  3073. {
  3074. #ifdef HAVE_LIBSSH2_INIT
  3075. if(libssh2_init(0)) {
  3076. DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
  3077. return CURLE_FAILED_INIT;
  3078. }
  3079. #endif
  3080. return CURLE_OK;
  3081. }
  3082. void Curl_ssh_cleanup(void)
  3083. {
  3084. #ifdef HAVE_LIBSSH2_EXIT
  3085. (void)libssh2_exit();
  3086. #endif
  3087. }
  3088. size_t Curl_ssh_version(char *buffer, size_t buflen)
  3089. {
  3090. return msnprintf(buffer, buflen, "libssh2/%s", LIBSSH2_VERSION);
  3091. }
  3092. #endif /* USE_LIBSSH2 */