ssh.c 92 KB

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