ssh-libssh.c 80 KB

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