ssh.c 97 KB

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