libssh.c 83 KB

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