ssh.c 109 KB

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