ftp.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifndef CURL_DISABLE_FTP
  24. #ifdef HAVE_NETINET_IN_H
  25. #include <netinet/in.h>
  26. #endif
  27. #ifdef HAVE_ARPA_INET_H
  28. #include <arpa/inet.h>
  29. #endif
  30. #ifdef HAVE_UTSNAME_H
  31. #include <sys/utsname.h>
  32. #endif
  33. #ifdef HAVE_NETDB_H
  34. #include <netdb.h>
  35. #endif
  36. #ifdef __VMS
  37. #include <in.h>
  38. #include <inet.h>
  39. #endif
  40. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  41. #undef in_addr_t
  42. #define in_addr_t unsigned long
  43. #endif
  44. #include <curl/curl.h>
  45. #include "urldata.h"
  46. #include "sendf.h"
  47. #include "if2ip.h"
  48. #include "hostip.h"
  49. #include "progress.h"
  50. #include "transfer.h"
  51. #include "escape.h"
  52. #include "http.h" /* for HTTP proxy tunnel stuff */
  53. #include "socks.h"
  54. #include "ftp.h"
  55. #include "fileinfo.h"
  56. #include "ftplistparser.h"
  57. #include "curl_sec.h"
  58. #include "strtoofft.h"
  59. #include "strcase.h"
  60. #include "vtls/vtls.h"
  61. #include "connect.h"
  62. #include "strerror.h"
  63. #include "inet_ntop.h"
  64. #include "inet_pton.h"
  65. #include "select.h"
  66. #include "parsedate.h" /* for the week day and month names */
  67. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  68. #include "multiif.h"
  69. #include "url.h"
  70. #include "strcase.h"
  71. #include "speedcheck.h"
  72. #include "warnless.h"
  73. #include "http_proxy.h"
  74. #include "non-ascii.h"
  75. /* The last 3 #include files should be in this order */
  76. #include "curl_printf.h"
  77. #include "curl_memory.h"
  78. #include "memdebug.h"
  79. #ifndef NI_MAXHOST
  80. #define NI_MAXHOST 1025
  81. #endif
  82. #ifndef INET_ADDRSTRLEN
  83. #define INET_ADDRSTRLEN 16
  84. #endif
  85. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  86. #define ftp_pasv_verbose(a,b,c,d) Curl_nop_stmt
  87. #endif
  88. /* Local API functions */
  89. #ifndef DEBUGBUILD
  90. static void _state(struct connectdata *conn,
  91. ftpstate newstate);
  92. #define state(x,y) _state(x,y)
  93. #else
  94. static void _state(struct connectdata *conn,
  95. ftpstate newstate,
  96. int lineno);
  97. #define state(x,y) _state(x,y,__LINE__)
  98. #endif
  99. static CURLcode ftp_sendquote(struct connectdata *conn,
  100. struct curl_slist *quote);
  101. static CURLcode ftp_quit(struct connectdata *conn);
  102. static CURLcode ftp_parse_url_path(struct connectdata *conn);
  103. static CURLcode ftp_regular_transfer(struct connectdata *conn, bool *done);
  104. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  105. static void ftp_pasv_verbose(struct connectdata *conn,
  106. Curl_addrinfo *ai,
  107. char *newhost, /* ascii version */
  108. int port);
  109. #endif
  110. static CURLcode ftp_state_prepare_transfer(struct connectdata *conn);
  111. static CURLcode ftp_state_mdtm(struct connectdata *conn);
  112. static CURLcode ftp_state_quote(struct connectdata *conn,
  113. bool init, ftpstate instate);
  114. static CURLcode ftp_nb_type(struct connectdata *conn,
  115. bool ascii, ftpstate newstate);
  116. static int ftp_need_type(struct connectdata *conn,
  117. bool ascii);
  118. static CURLcode ftp_do(struct connectdata *conn, bool *done);
  119. static CURLcode ftp_done(struct connectdata *conn,
  120. CURLcode, bool premature);
  121. static CURLcode ftp_connect(struct connectdata *conn, bool *done);
  122. static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection);
  123. static CURLcode ftp_do_more(struct connectdata *conn, int *completed);
  124. static CURLcode ftp_multi_statemach(struct connectdata *conn, bool *done);
  125. static int ftp_getsock(struct connectdata *conn, curl_socket_t *socks,
  126. int numsocks);
  127. static int ftp_domore_getsock(struct connectdata *conn, curl_socket_t *socks,
  128. int numsocks);
  129. static CURLcode ftp_doing(struct connectdata *conn,
  130. bool *dophase_done);
  131. static CURLcode ftp_setup_connection(struct connectdata * conn);
  132. static CURLcode init_wc_data(struct connectdata *conn);
  133. static CURLcode wc_statemach(struct connectdata *conn);
  134. static void wc_data_dtor(void *ptr);
  135. static CURLcode ftp_state_retr(struct connectdata *conn, curl_off_t filesize);
  136. static CURLcode ftp_readresp(curl_socket_t sockfd,
  137. struct pingpong *pp,
  138. int *ftpcode,
  139. size_t *size);
  140. static CURLcode ftp_dophase_done(struct connectdata *conn,
  141. bool connected);
  142. /* easy-to-use macro: */
  143. #define PPSENDF(x,y,z) result = Curl_pp_sendf(x,y,z); \
  144. if(result) \
  145. return result
  146. /*
  147. * FTP protocol handler.
  148. */
  149. const struct Curl_handler Curl_handler_ftp = {
  150. "FTP", /* scheme */
  151. ftp_setup_connection, /* setup_connection */
  152. ftp_do, /* do_it */
  153. ftp_done, /* done */
  154. ftp_do_more, /* do_more */
  155. ftp_connect, /* connect_it */
  156. ftp_multi_statemach, /* connecting */
  157. ftp_doing, /* doing */
  158. ftp_getsock, /* proto_getsock */
  159. ftp_getsock, /* doing_getsock */
  160. ftp_domore_getsock, /* domore_getsock */
  161. ZERO_NULL, /* perform_getsock */
  162. ftp_disconnect, /* disconnect */
  163. ZERO_NULL, /* readwrite */
  164. PORT_FTP, /* defport */
  165. CURLPROTO_FTP, /* protocol */
  166. PROTOPT_DUAL | PROTOPT_CLOSEACTION | PROTOPT_NEEDSPWD
  167. | PROTOPT_NOURLQUERY /* flags */
  168. };
  169. #ifdef USE_SSL
  170. /*
  171. * FTPS protocol handler.
  172. */
  173. const struct Curl_handler Curl_handler_ftps = {
  174. "FTPS", /* scheme */
  175. ftp_setup_connection, /* setup_connection */
  176. ftp_do, /* do_it */
  177. ftp_done, /* done */
  178. ftp_do_more, /* do_more */
  179. ftp_connect, /* connect_it */
  180. ftp_multi_statemach, /* connecting */
  181. ftp_doing, /* doing */
  182. ftp_getsock, /* proto_getsock */
  183. ftp_getsock, /* doing_getsock */
  184. ftp_domore_getsock, /* domore_getsock */
  185. ZERO_NULL, /* perform_getsock */
  186. ftp_disconnect, /* disconnect */
  187. ZERO_NULL, /* readwrite */
  188. PORT_FTPS, /* defport */
  189. CURLPROTO_FTPS, /* protocol */
  190. PROTOPT_SSL | PROTOPT_DUAL | PROTOPT_CLOSEACTION |
  191. PROTOPT_NEEDSPWD | PROTOPT_NOURLQUERY /* flags */
  192. };
  193. #endif
  194. #ifndef CURL_DISABLE_HTTP
  195. /*
  196. * HTTP-proxyed FTP protocol handler.
  197. */
  198. static const struct Curl_handler Curl_handler_ftp_proxy = {
  199. "FTP", /* scheme */
  200. Curl_http_setup_conn, /* setup_connection */
  201. Curl_http, /* do_it */
  202. Curl_http_done, /* done */
  203. ZERO_NULL, /* do_more */
  204. ZERO_NULL, /* connect_it */
  205. ZERO_NULL, /* connecting */
  206. ZERO_NULL, /* doing */
  207. ZERO_NULL, /* proto_getsock */
  208. ZERO_NULL, /* doing_getsock */
  209. ZERO_NULL, /* domore_getsock */
  210. ZERO_NULL, /* perform_getsock */
  211. ZERO_NULL, /* disconnect */
  212. ZERO_NULL, /* readwrite */
  213. PORT_FTP, /* defport */
  214. CURLPROTO_HTTP, /* protocol */
  215. PROTOPT_NONE /* flags */
  216. };
  217. #ifdef USE_SSL
  218. /*
  219. * HTTP-proxyed FTPS protocol handler.
  220. */
  221. static const struct Curl_handler Curl_handler_ftps_proxy = {
  222. "FTPS", /* scheme */
  223. Curl_http_setup_conn, /* setup_connection */
  224. Curl_http, /* do_it */
  225. Curl_http_done, /* done */
  226. ZERO_NULL, /* do_more */
  227. ZERO_NULL, /* connect_it */
  228. ZERO_NULL, /* connecting */
  229. ZERO_NULL, /* doing */
  230. ZERO_NULL, /* proto_getsock */
  231. ZERO_NULL, /* doing_getsock */
  232. ZERO_NULL, /* domore_getsock */
  233. ZERO_NULL, /* perform_getsock */
  234. ZERO_NULL, /* disconnect */
  235. ZERO_NULL, /* readwrite */
  236. PORT_FTPS, /* defport */
  237. CURLPROTO_HTTP, /* protocol */
  238. PROTOPT_NONE /* flags */
  239. };
  240. #endif
  241. #endif
  242. static void close_secondarysocket(struct connectdata *conn)
  243. {
  244. if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET]) {
  245. Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
  246. conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
  247. }
  248. conn->bits.tcpconnect[SECONDARYSOCKET] = FALSE;
  249. conn->tunnel_state[SECONDARYSOCKET] = TUNNEL_INIT;
  250. }
  251. /*
  252. * NOTE: back in the old days, we added code in the FTP code that made NOBODY
  253. * requests on files respond with headers passed to the client/stdout that
  254. * looked like HTTP ones.
  255. *
  256. * This approach is not very elegant, it causes confusion and is error-prone.
  257. * It is subject for removal at the next (or at least a future) soname bump.
  258. * Until then you can test the effects of the removal by undefining the
  259. * following define named CURL_FTP_HTTPSTYLE_HEAD.
  260. */
  261. #define CURL_FTP_HTTPSTYLE_HEAD 1
  262. static void freedirs(struct ftp_conn *ftpc)
  263. {
  264. int i;
  265. if(ftpc->dirs) {
  266. for(i=0; i < ftpc->dirdepth; i++) {
  267. free(ftpc->dirs[i]);
  268. ftpc->dirs[i]=NULL;
  269. }
  270. free(ftpc->dirs);
  271. ftpc->dirs = NULL;
  272. ftpc->dirdepth = 0;
  273. }
  274. Curl_safefree(ftpc->file);
  275. /* no longer of any use */
  276. Curl_safefree(ftpc->newhost);
  277. }
  278. /* Returns non-zero if the given string contains CR (\r) or LF (\n),
  279. which are not allowed within RFC 959 <string>.
  280. Note: The input string is in the client's encoding which might
  281. not be ASCII, so escape sequences \r & \n must be used instead
  282. of hex values 0x0d & 0x0a.
  283. */
  284. static bool isBadFtpString(const char *string)
  285. {
  286. return ((NULL != strchr(string, '\r')) ||
  287. (NULL != strchr(string, '\n'))) ? TRUE : FALSE;
  288. }
  289. /***********************************************************************
  290. *
  291. * AcceptServerConnect()
  292. *
  293. * After connection request is received from the server this function is
  294. * called to accept the connection and close the listening socket
  295. *
  296. */
  297. static CURLcode AcceptServerConnect(struct connectdata *conn)
  298. {
  299. struct Curl_easy *data = conn->data;
  300. curl_socket_t sock = conn->sock[SECONDARYSOCKET];
  301. curl_socket_t s = CURL_SOCKET_BAD;
  302. #ifdef ENABLE_IPV6
  303. struct Curl_sockaddr_storage add;
  304. #else
  305. struct sockaddr_in add;
  306. #endif
  307. curl_socklen_t size = (curl_socklen_t) sizeof(add);
  308. if(0 == getsockname(sock, (struct sockaddr *) &add, &size)) {
  309. size = sizeof(add);
  310. s=accept(sock, (struct sockaddr *) &add, &size);
  311. }
  312. Curl_closesocket(conn, sock); /* close the first socket */
  313. if(CURL_SOCKET_BAD == s) {
  314. failf(data, "Error accept()ing server connect");
  315. return CURLE_FTP_PORT_FAILED;
  316. }
  317. infof(data, "Connection accepted from server\n");
  318. /* when this happens within the DO state it is important that we mark us as
  319. not needing DO_MORE anymore */
  320. conn->bits.do_more = FALSE;
  321. conn->sock[SECONDARYSOCKET] = s;
  322. (void)curlx_nonblock(s, TRUE); /* enable non-blocking */
  323. conn->sock_accepted[SECONDARYSOCKET] = TRUE;
  324. if(data->set.fsockopt) {
  325. int error = 0;
  326. /* activate callback for setting socket options */
  327. error = data->set.fsockopt(data->set.sockopt_client,
  328. s,
  329. CURLSOCKTYPE_ACCEPT);
  330. if(error) {
  331. close_secondarysocket(conn);
  332. return CURLE_ABORTED_BY_CALLBACK;
  333. }
  334. }
  335. return CURLE_OK;
  336. }
  337. /*
  338. * ftp_timeleft_accept() returns the amount of milliseconds left allowed for
  339. * waiting server to connect. If the value is negative, the timeout time has
  340. * already elapsed.
  341. *
  342. * The start time is stored in progress.t_acceptdata - as set with
  343. * Curl_pgrsTime(..., TIMER_STARTACCEPT);
  344. *
  345. */
  346. static time_t ftp_timeleft_accept(struct Curl_easy *data)
  347. {
  348. time_t timeout_ms = DEFAULT_ACCEPT_TIMEOUT;
  349. time_t other;
  350. struct timeval now;
  351. if(data->set.accepttimeout > 0)
  352. timeout_ms = data->set.accepttimeout;
  353. now = Curl_tvnow();
  354. /* check if the generic timeout possibly is set shorter */
  355. other = Curl_timeleft(data, &now, FALSE);
  356. if(other && (other < timeout_ms))
  357. /* note that this also works fine for when other happens to be negative
  358. due to it already having elapsed */
  359. timeout_ms = other;
  360. else {
  361. /* subtract elapsed time */
  362. timeout_ms -= Curl_tvdiff(now, data->progress.t_acceptdata);
  363. if(!timeout_ms)
  364. /* avoid returning 0 as that means no timeout! */
  365. return -1;
  366. }
  367. return timeout_ms;
  368. }
  369. /***********************************************************************
  370. *
  371. * ReceivedServerConnect()
  372. *
  373. * After allowing server to connect to us from data port, this function
  374. * checks both data connection for connection establishment and ctrl
  375. * connection for a negative response regarding a failure in connecting
  376. *
  377. */
  378. static CURLcode ReceivedServerConnect(struct connectdata *conn, bool *received)
  379. {
  380. struct Curl_easy *data = conn->data;
  381. curl_socket_t ctrl_sock = conn->sock[FIRSTSOCKET];
  382. curl_socket_t data_sock = conn->sock[SECONDARYSOCKET];
  383. struct ftp_conn *ftpc = &conn->proto.ftpc;
  384. struct pingpong *pp = &ftpc->pp;
  385. int result;
  386. time_t timeout_ms;
  387. ssize_t nread;
  388. int ftpcode;
  389. *received = FALSE;
  390. timeout_ms = ftp_timeleft_accept(data);
  391. infof(data, "Checking for server connect\n");
  392. if(timeout_ms < 0) {
  393. /* if a timeout was already reached, bail out */
  394. failf(data, "Accept timeout occurred while waiting server connect");
  395. return CURLE_FTP_ACCEPT_TIMEOUT;
  396. }
  397. /* First check whether there is a cached response from server */
  398. if(pp->cache_size && pp->cache && pp->cache[0] > '3') {
  399. /* Data connection could not be established, let's return */
  400. infof(data, "There is negative response in cache while serv connect\n");
  401. Curl_GetFTPResponse(&nread, conn, &ftpcode);
  402. return CURLE_FTP_ACCEPT_FAILED;
  403. }
  404. result = Curl_socket_check(ctrl_sock, data_sock, CURL_SOCKET_BAD, 0);
  405. /* see if the connection request is already here */
  406. switch(result) {
  407. case -1: /* error */
  408. /* let's die here */
  409. failf(data, "Error while waiting for server connect");
  410. return CURLE_FTP_ACCEPT_FAILED;
  411. case 0: /* Server connect is not received yet */
  412. break; /* loop */
  413. default:
  414. if(result & CURL_CSELECT_IN2) {
  415. infof(data, "Ready to accept data connection from server\n");
  416. *received = TRUE;
  417. }
  418. else if(result & CURL_CSELECT_IN) {
  419. infof(data, "Ctrl conn has data while waiting for data conn\n");
  420. Curl_GetFTPResponse(&nread, conn, &ftpcode);
  421. if(ftpcode/100 > 3)
  422. return CURLE_FTP_ACCEPT_FAILED;
  423. return CURLE_WEIRD_SERVER_REPLY;
  424. }
  425. break;
  426. } /* switch() */
  427. return CURLE_OK;
  428. }
  429. /***********************************************************************
  430. *
  431. * InitiateTransfer()
  432. *
  433. * After connection from server is accepted this function is called to
  434. * setup transfer parameters and initiate the data transfer.
  435. *
  436. */
  437. static CURLcode InitiateTransfer(struct connectdata *conn)
  438. {
  439. struct Curl_easy *data = conn->data;
  440. struct FTP *ftp = data->req.protop;
  441. CURLcode result = CURLE_OK;
  442. if(conn->bits.ftp_use_data_ssl) {
  443. /* since we only have a plaintext TCP connection here, we must now
  444. * do the TLS stuff */
  445. infof(data, "Doing the SSL/TLS handshake on the data stream\n");
  446. result = Curl_ssl_connect(conn, SECONDARYSOCKET);
  447. if(result)
  448. return result;
  449. }
  450. if(conn->proto.ftpc.state_saved == FTP_STOR) {
  451. *(ftp->bytecountp)=0;
  452. /* When we know we're uploading a specified file, we can get the file
  453. size prior to the actual upload. */
  454. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  455. /* set the SO_SNDBUF for the secondary socket for those who need it */
  456. Curl_sndbufset(conn->sock[SECONDARYSOCKET]);
  457. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
  458. SECONDARYSOCKET, ftp->bytecountp);
  459. }
  460. else {
  461. /* FTP download: */
  462. Curl_setup_transfer(conn, SECONDARYSOCKET,
  463. conn->proto.ftpc.retr_size_saved, FALSE,
  464. ftp->bytecountp, -1, NULL); /* no upload here */
  465. }
  466. conn->proto.ftpc.pp.pending_resp = TRUE; /* expect server response */
  467. state(conn, FTP_STOP);
  468. return CURLE_OK;
  469. }
  470. /***********************************************************************
  471. *
  472. * AllowServerConnect()
  473. *
  474. * When we've issue the PORT command, we have told the server to connect to
  475. * us. This function checks whether data connection is established if so it is
  476. * accepted.
  477. *
  478. */
  479. static CURLcode AllowServerConnect(struct connectdata *conn, bool *connected)
  480. {
  481. struct Curl_easy *data = conn->data;
  482. time_t timeout_ms;
  483. CURLcode result = CURLE_OK;
  484. *connected = FALSE;
  485. infof(data, "Preparing for accepting server on data port\n");
  486. /* Save the time we start accepting server connect */
  487. Curl_pgrsTime(data, TIMER_STARTACCEPT);
  488. timeout_ms = ftp_timeleft_accept(data);
  489. if(timeout_ms < 0) {
  490. /* if a timeout was already reached, bail out */
  491. failf(data, "Accept timeout occurred while waiting server connect");
  492. return CURLE_FTP_ACCEPT_TIMEOUT;
  493. }
  494. /* see if the connection request is already here */
  495. result = ReceivedServerConnect(conn, connected);
  496. if(result)
  497. return result;
  498. if(*connected) {
  499. result = AcceptServerConnect(conn);
  500. if(result)
  501. return result;
  502. result = InitiateTransfer(conn);
  503. if(result)
  504. return result;
  505. }
  506. else {
  507. /* Add timeout to multi handle and break out of the loop */
  508. if(!result && *connected == FALSE) {
  509. if(data->set.accepttimeout > 0)
  510. Curl_expire(data, data->set.accepttimeout);
  511. else
  512. Curl_expire(data, DEFAULT_ACCEPT_TIMEOUT);
  513. }
  514. }
  515. return result;
  516. }
  517. /* macro to check for a three-digit ftp status code at the start of the
  518. given string */
  519. #define STATUSCODE(line) (ISDIGIT(line[0]) && ISDIGIT(line[1]) && \
  520. ISDIGIT(line[2]))
  521. /* macro to check for the last line in an FTP server response */
  522. #define LASTLINE(line) (STATUSCODE(line) && (' ' == line[3]))
  523. static bool ftp_endofresp(struct connectdata *conn, char *line, size_t len,
  524. int *code)
  525. {
  526. (void)conn;
  527. if((len > 3) && LASTLINE(line)) {
  528. *code = curlx_sltosi(strtol(line, NULL, 10));
  529. return TRUE;
  530. }
  531. return FALSE;
  532. }
  533. static CURLcode ftp_readresp(curl_socket_t sockfd,
  534. struct pingpong *pp,
  535. int *ftpcode, /* return the ftp-code if done */
  536. size_t *size) /* size of the response */
  537. {
  538. struct connectdata *conn = pp->conn;
  539. struct Curl_easy *data = conn->data;
  540. #ifdef HAVE_GSSAPI
  541. char * const buf = data->state.buffer;
  542. #endif
  543. CURLcode result = CURLE_OK;
  544. int code;
  545. result = Curl_pp_readresp(sockfd, pp, &code, size);
  546. #if defined(HAVE_GSSAPI)
  547. /* handle the security-oriented responses 6xx ***/
  548. /* FIXME: some errorchecking perhaps... ***/
  549. switch(code) {
  550. case 631:
  551. code = Curl_sec_read_msg(conn, buf, PROT_SAFE);
  552. break;
  553. case 632:
  554. code = Curl_sec_read_msg(conn, buf, PROT_PRIVATE);
  555. break;
  556. case 633:
  557. code = Curl_sec_read_msg(conn, buf, PROT_CONFIDENTIAL);
  558. break;
  559. default:
  560. /* normal ftp stuff we pass through! */
  561. break;
  562. }
  563. #endif
  564. /* store the latest code for later retrieval */
  565. data->info.httpcode=code;
  566. if(ftpcode)
  567. *ftpcode = code;
  568. if(421 == code) {
  569. /* 421 means "Service not available, closing control connection." and FTP
  570. * servers use it to signal that idle session timeout has been exceeded.
  571. * If we ignored the response, it could end up hanging in some cases.
  572. *
  573. * This response code can come at any point so having it treated
  574. * generically is a good idea.
  575. */
  576. infof(data, "We got a 421 - timeout!\n");
  577. state(conn, FTP_STOP);
  578. return CURLE_OPERATION_TIMEDOUT;
  579. }
  580. return result;
  581. }
  582. /* --- parse FTP server responses --- */
  583. /*
  584. * Curl_GetFTPResponse() is a BLOCKING function to read the full response
  585. * from a server after a command.
  586. *
  587. */
  588. CURLcode Curl_GetFTPResponse(ssize_t *nreadp, /* return number of bytes read */
  589. struct connectdata *conn,
  590. int *ftpcode) /* return the ftp-code */
  591. {
  592. /*
  593. * We cannot read just one byte per read() and then go back to select() as
  594. * the OpenSSL read() doesn't grok that properly.
  595. *
  596. * Alas, read as much as possible, split up into lines, use the ending
  597. * line in a response or continue reading. */
  598. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  599. time_t timeout; /* timeout in milliseconds */
  600. time_t interval_ms;
  601. struct Curl_easy *data = conn->data;
  602. CURLcode result = CURLE_OK;
  603. struct ftp_conn *ftpc = &conn->proto.ftpc;
  604. struct pingpong *pp = &ftpc->pp;
  605. size_t nread;
  606. int cache_skip=0;
  607. int value_to_be_ignored=0;
  608. if(ftpcode)
  609. *ftpcode = 0; /* 0 for errors */
  610. else
  611. /* make the pointer point to something for the rest of this function */
  612. ftpcode = &value_to_be_ignored;
  613. *nreadp=0;
  614. while(!*ftpcode && !result) {
  615. /* check and reset timeout value every lap */
  616. timeout = Curl_pp_state_timeout(pp);
  617. if(timeout <=0) {
  618. failf(data, "FTP response timeout");
  619. return CURLE_OPERATION_TIMEDOUT; /* already too little time */
  620. }
  621. interval_ms = 1000; /* use 1 second timeout intervals */
  622. if(timeout < interval_ms)
  623. interval_ms = timeout;
  624. /*
  625. * Since this function is blocking, we need to wait here for input on the
  626. * connection and only then we call the response reading function. We do
  627. * timeout at least every second to make the timeout check run.
  628. *
  629. * A caution here is that the ftp_readresp() function has a cache that may
  630. * contain pieces of a response from the previous invoke and we need to
  631. * make sure we don't just wait for input while there is unhandled data in
  632. * that cache. But also, if the cache is there, we call ftp_readresp() and
  633. * the cache wasn't good enough to continue we must not just busy-loop
  634. * around this function.
  635. *
  636. */
  637. if(pp->cache && (cache_skip < 2)) {
  638. /*
  639. * There's a cache left since before. We then skipping the wait for
  640. * socket action, unless this is the same cache like the previous round
  641. * as then the cache was deemed not enough to act on and we then need to
  642. * wait for more data anyway.
  643. */
  644. }
  645. else if(!Curl_conn_data_pending(conn, FIRSTSOCKET)) {
  646. switch(SOCKET_READABLE(sockfd, interval_ms)) {
  647. case -1: /* select() error, stop reading */
  648. failf(data, "FTP response aborted due to select/poll error: %d",
  649. SOCKERRNO);
  650. return CURLE_RECV_ERROR;
  651. case 0: /* timeout */
  652. if(Curl_pgrsUpdate(conn))
  653. return CURLE_ABORTED_BY_CALLBACK;
  654. continue; /* just continue in our loop for the timeout duration */
  655. default: /* for clarity */
  656. break;
  657. }
  658. }
  659. result = ftp_readresp(sockfd, pp, ftpcode, &nread);
  660. if(result)
  661. break;
  662. if(!nread && pp->cache)
  663. /* bump cache skip counter as on repeated skips we must wait for more
  664. data */
  665. cache_skip++;
  666. else
  667. /* when we got data or there is no cache left, we reset the cache skip
  668. counter */
  669. cache_skip=0;
  670. *nreadp += nread;
  671. } /* while there's buffer left and loop is requested */
  672. pp->pending_resp = FALSE;
  673. return result;
  674. }
  675. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  676. /* for debug purposes */
  677. static const char * const ftp_state_names[]={
  678. "STOP",
  679. "WAIT220",
  680. "AUTH",
  681. "USER",
  682. "PASS",
  683. "ACCT",
  684. "PBSZ",
  685. "PROT",
  686. "CCC",
  687. "PWD",
  688. "SYST",
  689. "NAMEFMT",
  690. "QUOTE",
  691. "RETR_PREQUOTE",
  692. "STOR_PREQUOTE",
  693. "POSTQUOTE",
  694. "CWD",
  695. "MKD",
  696. "MDTM",
  697. "TYPE",
  698. "LIST_TYPE",
  699. "RETR_TYPE",
  700. "STOR_TYPE",
  701. "SIZE",
  702. "RETR_SIZE",
  703. "STOR_SIZE",
  704. "REST",
  705. "RETR_REST",
  706. "PORT",
  707. "PRET",
  708. "PASV",
  709. "LIST",
  710. "RETR",
  711. "STOR",
  712. "QUIT"
  713. };
  714. #endif
  715. /* This is the ONLY way to change FTP state! */
  716. static void _state(struct connectdata *conn,
  717. ftpstate newstate
  718. #ifdef DEBUGBUILD
  719. , int lineno
  720. #endif
  721. )
  722. {
  723. struct ftp_conn *ftpc = &conn->proto.ftpc;
  724. #if defined(DEBUGBUILD)
  725. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  726. (void) lineno;
  727. #else
  728. if(ftpc->state != newstate)
  729. infof(conn->data, "FTP %p (line %d) state change from %s to %s\n",
  730. (void *)ftpc, lineno, ftp_state_names[ftpc->state],
  731. ftp_state_names[newstate]);
  732. #endif
  733. #endif
  734. ftpc->state = newstate;
  735. }
  736. static CURLcode ftp_state_user(struct connectdata *conn)
  737. {
  738. CURLcode result;
  739. struct FTP *ftp = conn->data->req.protop;
  740. /* send USER */
  741. PPSENDF(&conn->proto.ftpc.pp, "USER %s", ftp->user?ftp->user:"");
  742. state(conn, FTP_USER);
  743. conn->data->state.ftp_trying_alternative = FALSE;
  744. return CURLE_OK;
  745. }
  746. static CURLcode ftp_state_pwd(struct connectdata *conn)
  747. {
  748. CURLcode result;
  749. /* send PWD to discover our entry point */
  750. PPSENDF(&conn->proto.ftpc.pp, "%s", "PWD");
  751. state(conn, FTP_PWD);
  752. return CURLE_OK;
  753. }
  754. /* For the FTP "protocol connect" and "doing" phases only */
  755. static int ftp_getsock(struct connectdata *conn,
  756. curl_socket_t *socks,
  757. int numsocks)
  758. {
  759. return Curl_pp_getsock(&conn->proto.ftpc.pp, socks, numsocks);
  760. }
  761. /* For the FTP "DO_MORE" phase only */
  762. static int ftp_domore_getsock(struct connectdata *conn, curl_socket_t *socks,
  763. int numsocks)
  764. {
  765. struct ftp_conn *ftpc = &conn->proto.ftpc;
  766. if(!numsocks)
  767. return GETSOCK_BLANK;
  768. /* When in DO_MORE state, we could be either waiting for us to connect to a
  769. * remote site, or we could wait for that site to connect to us. Or just
  770. * handle ordinary commands.
  771. */
  772. if(FTP_STOP == ftpc->state) {
  773. int bits = GETSOCK_READSOCK(0);
  774. /* if stopped and still in this state, then we're also waiting for a
  775. connect on the secondary connection */
  776. socks[0] = conn->sock[FIRSTSOCKET];
  777. if(!conn->data->set.ftp_use_port) {
  778. int s;
  779. int i;
  780. /* PORT is used to tell the server to connect to us, and during that we
  781. don't do happy eyeballs, but we do if we connect to the server */
  782. for(s=1, i=0; i<2; i++) {
  783. if(conn->tempsock[i] != CURL_SOCKET_BAD) {
  784. socks[s] = conn->tempsock[i];
  785. bits |= GETSOCK_WRITESOCK(s++);
  786. }
  787. }
  788. }
  789. else {
  790. socks[1] = conn->sock[SECONDARYSOCKET];
  791. bits |= GETSOCK_WRITESOCK(1) | GETSOCK_READSOCK(1);
  792. }
  793. return bits;
  794. }
  795. else
  796. return Curl_pp_getsock(&conn->proto.ftpc.pp, socks, numsocks);
  797. }
  798. /* This is called after the FTP_QUOTE state is passed.
  799. ftp_state_cwd() sends the range of CWD commands to the server to change to
  800. the correct directory. It may also need to send MKD commands to create
  801. missing ones, if that option is enabled.
  802. */
  803. static CURLcode ftp_state_cwd(struct connectdata *conn)
  804. {
  805. CURLcode result = CURLE_OK;
  806. struct ftp_conn *ftpc = &conn->proto.ftpc;
  807. if(ftpc->cwddone)
  808. /* already done and fine */
  809. result = ftp_state_mdtm(conn);
  810. else {
  811. ftpc->count2 = 0; /* count2 counts failed CWDs */
  812. /* count3 is set to allow a MKD to fail once. In the case when first CWD
  813. fails and then MKD fails (due to another session raced it to create the
  814. dir) this then allows for a second try to CWD to it */
  815. ftpc->count3 = (conn->data->set.ftp_create_missing_dirs==2)?1:0;
  816. if(conn->bits.reuse && ftpc->entrypath) {
  817. /* This is a re-used connection. Since we change directory to where the
  818. transfer is taking place, we must first get back to the original dir
  819. where we ended up after login: */
  820. ftpc->count1 = 0; /* we count this as the first path, then we add one
  821. for all upcoming ones in the ftp->dirs[] array */
  822. PPSENDF(&conn->proto.ftpc.pp, "CWD %s", ftpc->entrypath);
  823. state(conn, FTP_CWD);
  824. }
  825. else {
  826. if(ftpc->dirdepth) {
  827. ftpc->count1 = 1;
  828. /* issue the first CWD, the rest is sent when the CWD responses are
  829. received... */
  830. PPSENDF(&conn->proto.ftpc.pp, "CWD %s", ftpc->dirs[ftpc->count1 -1]);
  831. state(conn, FTP_CWD);
  832. }
  833. else {
  834. /* No CWD necessary */
  835. result = ftp_state_mdtm(conn);
  836. }
  837. }
  838. }
  839. return result;
  840. }
  841. typedef enum {
  842. EPRT,
  843. PORT,
  844. DONE
  845. } ftpport;
  846. static CURLcode ftp_state_use_port(struct connectdata *conn,
  847. ftpport fcmd) /* start with this */
  848. {
  849. CURLcode result = CURLE_OK;
  850. struct ftp_conn *ftpc = &conn->proto.ftpc;
  851. struct Curl_easy *data=conn->data;
  852. curl_socket_t portsock= CURL_SOCKET_BAD;
  853. char myhost[256] = "";
  854. struct Curl_sockaddr_storage ss;
  855. Curl_addrinfo *res, *ai;
  856. curl_socklen_t sslen;
  857. char hbuf[NI_MAXHOST];
  858. struct sockaddr *sa=(struct sockaddr *)&ss;
  859. struct sockaddr_in * const sa4 = (void *)sa;
  860. #ifdef ENABLE_IPV6
  861. struct sockaddr_in6 * const sa6 = (void *)sa;
  862. #endif
  863. char tmp[1024];
  864. static const char mode[][5] = { "EPRT", "PORT" };
  865. int rc;
  866. int error;
  867. char *host = NULL;
  868. char *string_ftpport = data->set.str[STRING_FTPPORT];
  869. struct Curl_dns_entry *h=NULL;
  870. unsigned short port_min = 0;
  871. unsigned short port_max = 0;
  872. unsigned short port;
  873. bool possibly_non_local = TRUE;
  874. char *addr = NULL;
  875. /* Step 1, figure out what is requested,
  876. * accepted format :
  877. * (ipv4|ipv6|domain|interface)?(:port(-range)?)?
  878. */
  879. if(data->set.str[STRING_FTPPORT] &&
  880. (strlen(data->set.str[STRING_FTPPORT]) > 1)) {
  881. #ifdef ENABLE_IPV6
  882. size_t addrlen = INET6_ADDRSTRLEN > strlen(string_ftpport) ?
  883. INET6_ADDRSTRLEN : strlen(string_ftpport);
  884. #else
  885. size_t addrlen = INET_ADDRSTRLEN > strlen(string_ftpport) ?
  886. INET_ADDRSTRLEN : strlen(string_ftpport);
  887. #endif
  888. char *ip_start = string_ftpport;
  889. char *ip_end = NULL;
  890. char *port_start = NULL;
  891. char *port_sep = NULL;
  892. addr = calloc(addrlen+1, 1);
  893. if(!addr)
  894. return CURLE_OUT_OF_MEMORY;
  895. #ifdef ENABLE_IPV6
  896. if(*string_ftpport == '[') {
  897. /* [ipv6]:port(-range) */
  898. ip_start = string_ftpport + 1;
  899. ip_end = strchr(string_ftpport, ']');
  900. if(ip_end)
  901. strncpy(addr, ip_start, ip_end - ip_start);
  902. }
  903. else
  904. #endif
  905. if(*string_ftpport == ':') {
  906. /* :port */
  907. ip_end = string_ftpport;
  908. }
  909. else {
  910. ip_end = strchr(string_ftpport, ':');
  911. if(ip_end) {
  912. /* either ipv6 or (ipv4|domain|interface):port(-range) */
  913. #ifdef ENABLE_IPV6
  914. if(Curl_inet_pton(AF_INET6, string_ftpport, sa6) == 1) {
  915. /* ipv6 */
  916. port_min = port_max = 0;
  917. strcpy(addr, string_ftpport);
  918. ip_end = NULL; /* this got no port ! */
  919. }
  920. else
  921. #endif
  922. /* (ipv4|domain|interface):port(-range) */
  923. strncpy(addr, string_ftpport, ip_end - ip_start);
  924. }
  925. else
  926. /* ipv4|interface */
  927. strcpy(addr, string_ftpport);
  928. }
  929. /* parse the port */
  930. if(ip_end != NULL) {
  931. port_start = strchr(ip_end, ':');
  932. if(port_start) {
  933. port_min = curlx_ultous(strtoul(port_start+1, NULL, 10));
  934. port_sep = strchr(port_start, '-');
  935. if(port_sep) {
  936. port_max = curlx_ultous(strtoul(port_sep + 1, NULL, 10));
  937. }
  938. else
  939. port_max = port_min;
  940. }
  941. }
  942. /* correct errors like:
  943. * :1234-1230
  944. * :-4711, in this case port_min is (unsigned)-1,
  945. * therefore port_min > port_max for all cases
  946. * but port_max = (unsigned)-1
  947. */
  948. if(port_min > port_max)
  949. port_min = port_max = 0;
  950. if(*addr != '\0') {
  951. /* attempt to get the address of the given interface name */
  952. switch(Curl_if2ip(conn->ip_addr->ai_family,
  953. Curl_ipv6_scope(conn->ip_addr->ai_addr),
  954. conn->scope_id, addr, hbuf, sizeof(hbuf))) {
  955. case IF2IP_NOT_FOUND:
  956. /* not an interface, use the given string as host name instead */
  957. host = addr;
  958. break;
  959. case IF2IP_AF_NOT_SUPPORTED:
  960. return CURLE_FTP_PORT_FAILED;
  961. case IF2IP_FOUND:
  962. host = hbuf; /* use the hbuf for host name */
  963. }
  964. }
  965. else
  966. /* there was only a port(-range) given, default the host */
  967. host = NULL;
  968. } /* data->set.ftpport */
  969. if(!host) {
  970. /* not an interface and not a host name, get default by extracting
  971. the IP from the control connection */
  972. sslen = sizeof(ss);
  973. if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) {
  974. failf(data, "getsockname() failed: %s",
  975. Curl_strerror(conn, SOCKERRNO) );
  976. free(addr);
  977. return CURLE_FTP_PORT_FAILED;
  978. }
  979. switch(sa->sa_family) {
  980. #ifdef ENABLE_IPV6
  981. case AF_INET6:
  982. Curl_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, sizeof(hbuf));
  983. break;
  984. #endif
  985. default:
  986. Curl_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, sizeof(hbuf));
  987. break;
  988. }
  989. host = hbuf; /* use this host name */
  990. possibly_non_local = FALSE; /* we know it is local now */
  991. }
  992. /* resolv ip/host to ip */
  993. rc = Curl_resolv(conn, host, 0, &h);
  994. if(rc == CURLRESOLV_PENDING)
  995. (void)Curl_resolver_wait_resolv(conn, &h);
  996. if(h) {
  997. res = h->addr;
  998. /* when we return from this function, we can forget about this entry
  999. to we can unlock it now already */
  1000. Curl_resolv_unlock(data, h);
  1001. } /* (h) */
  1002. else
  1003. res = NULL; /* failure! */
  1004. if(res == NULL) {
  1005. failf(data, "failed to resolve the address provided to PORT: %s", host);
  1006. free(addr);
  1007. return CURLE_FTP_PORT_FAILED;
  1008. }
  1009. free(addr);
  1010. host = NULL;
  1011. /* step 2, create a socket for the requested address */
  1012. portsock = CURL_SOCKET_BAD;
  1013. error = 0;
  1014. for(ai = res; ai; ai = ai->ai_next) {
  1015. result = Curl_socket(conn, ai, NULL, &portsock);
  1016. if(result) {
  1017. error = SOCKERRNO;
  1018. continue;
  1019. }
  1020. break;
  1021. }
  1022. if(!ai) {
  1023. failf(data, "socket failure: %s", Curl_strerror(conn, error));
  1024. return CURLE_FTP_PORT_FAILED;
  1025. }
  1026. /* step 3, bind to a suitable local address */
  1027. memcpy(sa, ai->ai_addr, ai->ai_addrlen);
  1028. sslen = ai->ai_addrlen;
  1029. for(port = port_min; port <= port_max;) {
  1030. if(sa->sa_family == AF_INET)
  1031. sa4->sin_port = htons(port);
  1032. #ifdef ENABLE_IPV6
  1033. else
  1034. sa6->sin6_port = htons(port);
  1035. #endif
  1036. /* Try binding the given address. */
  1037. if(bind(portsock, sa, sslen) ) {
  1038. /* It failed. */
  1039. error = SOCKERRNO;
  1040. if(possibly_non_local && (error == EADDRNOTAVAIL)) {
  1041. /* The requested bind address is not local. Use the address used for
  1042. * the control connection instead and restart the port loop
  1043. */
  1044. infof(data, "bind(port=%hu) on non-local address failed: %s\n", port,
  1045. Curl_strerror(conn, error) );
  1046. sslen = sizeof(ss);
  1047. if(getsockname(conn->sock[FIRSTSOCKET], sa, &sslen)) {
  1048. failf(data, "getsockname() failed: %s",
  1049. Curl_strerror(conn, SOCKERRNO) );
  1050. Curl_closesocket(conn, portsock);
  1051. return CURLE_FTP_PORT_FAILED;
  1052. }
  1053. port = port_min;
  1054. possibly_non_local = FALSE; /* don't try this again */
  1055. continue;
  1056. }
  1057. else if(error != EADDRINUSE && error != EACCES) {
  1058. failf(data, "bind(port=%hu) failed: %s", port,
  1059. Curl_strerror(conn, error) );
  1060. Curl_closesocket(conn, portsock);
  1061. return CURLE_FTP_PORT_FAILED;
  1062. }
  1063. }
  1064. else
  1065. break;
  1066. port++;
  1067. }
  1068. /* maybe all ports were in use already*/
  1069. if(port > port_max) {
  1070. failf(data, "bind() failed, we ran out of ports!");
  1071. Curl_closesocket(conn, portsock);
  1072. return CURLE_FTP_PORT_FAILED;
  1073. }
  1074. /* get the name again after the bind() so that we can extract the
  1075. port number it uses now */
  1076. sslen = sizeof(ss);
  1077. if(getsockname(portsock, (struct sockaddr *)sa, &sslen)) {
  1078. failf(data, "getsockname() failed: %s",
  1079. Curl_strerror(conn, SOCKERRNO) );
  1080. Curl_closesocket(conn, portsock);
  1081. return CURLE_FTP_PORT_FAILED;
  1082. }
  1083. /* step 4, listen on the socket */
  1084. if(listen(portsock, 1)) {
  1085. failf(data, "socket failure: %s", Curl_strerror(conn, SOCKERRNO));
  1086. Curl_closesocket(conn, portsock);
  1087. return CURLE_FTP_PORT_FAILED;
  1088. }
  1089. /* step 5, send the proper FTP command */
  1090. /* get a plain printable version of the numerical address to work with
  1091. below */
  1092. Curl_printable_address(ai, myhost, sizeof(myhost));
  1093. #ifdef ENABLE_IPV6
  1094. if(!conn->bits.ftp_use_eprt && conn->bits.ipv6)
  1095. /* EPRT is disabled but we are connected to a IPv6 host, so we ignore the
  1096. request and enable EPRT again! */
  1097. conn->bits.ftp_use_eprt = TRUE;
  1098. #endif
  1099. for(; fcmd != DONE; fcmd++) {
  1100. if(!conn->bits.ftp_use_eprt && (EPRT == fcmd))
  1101. /* if disabled, goto next */
  1102. continue;
  1103. if((PORT == fcmd) && sa->sa_family != AF_INET)
  1104. /* PORT is IPv4 only */
  1105. continue;
  1106. switch(sa->sa_family) {
  1107. case AF_INET:
  1108. port = ntohs(sa4->sin_port);
  1109. break;
  1110. #ifdef ENABLE_IPV6
  1111. case AF_INET6:
  1112. port = ntohs(sa6->sin6_port);
  1113. break;
  1114. #endif
  1115. default:
  1116. continue; /* might as well skip this */
  1117. }
  1118. if(EPRT == fcmd) {
  1119. /*
  1120. * Two fine examples from RFC2428;
  1121. *
  1122. * EPRT |1|132.235.1.2|6275|
  1123. *
  1124. * EPRT |2|1080::8:800:200C:417A|5282|
  1125. */
  1126. result = Curl_pp_sendf(&ftpc->pp, "%s |%d|%s|%hu|", mode[fcmd],
  1127. sa->sa_family == AF_INET?1:2,
  1128. myhost, port);
  1129. if(result) {
  1130. failf(data, "Failure sending EPRT command: %s",
  1131. curl_easy_strerror(result));
  1132. Curl_closesocket(conn, portsock);
  1133. /* don't retry using PORT */
  1134. ftpc->count1 = PORT;
  1135. /* bail out */
  1136. state(conn, FTP_STOP);
  1137. return result;
  1138. }
  1139. break;
  1140. }
  1141. else if(PORT == fcmd) {
  1142. char *source = myhost;
  1143. char *dest = tmp;
  1144. /* translate x.x.x.x to x,x,x,x */
  1145. while(source && *source) {
  1146. if(*source == '.')
  1147. *dest=',';
  1148. else
  1149. *dest = *source;
  1150. dest++;
  1151. source++;
  1152. }
  1153. *dest = 0;
  1154. snprintf(dest, 20, ",%d,%d", (int)(port>>8), (int)(port&0xff));
  1155. result = Curl_pp_sendf(&ftpc->pp, "%s %s", mode[fcmd], tmp);
  1156. if(result) {
  1157. failf(data, "Failure sending PORT command: %s",
  1158. curl_easy_strerror(result));
  1159. Curl_closesocket(conn, portsock);
  1160. /* bail out */
  1161. state(conn, FTP_STOP);
  1162. return result;
  1163. }
  1164. break;
  1165. }
  1166. }
  1167. /* store which command was sent */
  1168. ftpc->count1 = fcmd;
  1169. close_secondarysocket(conn);
  1170. /* we set the secondary socket variable to this for now, it is only so that
  1171. the cleanup function will close it in case we fail before the true
  1172. secondary stuff is made */
  1173. conn->sock[SECONDARYSOCKET] = portsock;
  1174. /* this tcpconnect assignment below is a hackish work-around to make the
  1175. multi interface with active FTP work - as it will not wait for a
  1176. (passive) connect in Curl_is_connected().
  1177. The *proper* fix is to make sure that the active connection from the
  1178. server is done in a non-blocking way. Currently, it is still BLOCKING.
  1179. */
  1180. conn->bits.tcpconnect[SECONDARYSOCKET] = TRUE;
  1181. state(conn, FTP_PORT);
  1182. return result;
  1183. }
  1184. static CURLcode ftp_state_use_pasv(struct connectdata *conn)
  1185. {
  1186. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1187. CURLcode result = CURLE_OK;
  1188. /*
  1189. Here's the excecutive summary on what to do:
  1190. PASV is RFC959, expect:
  1191. 227 Entering Passive Mode (a1,a2,a3,a4,p1,p2)
  1192. LPSV is RFC1639, expect:
  1193. 228 Entering Long Passive Mode (4,4,a1,a2,a3,a4,2,p1,p2)
  1194. EPSV is RFC2428, expect:
  1195. 229 Entering Extended Passive Mode (|||port|)
  1196. */
  1197. static const char mode[][5] = { "EPSV", "PASV" };
  1198. int modeoff;
  1199. #ifdef PF_INET6
  1200. if(!conn->bits.ftp_use_epsv && conn->bits.ipv6)
  1201. /* EPSV is disabled but we are connected to a IPv6 host, so we ignore the
  1202. request and enable EPSV again! */
  1203. conn->bits.ftp_use_epsv = TRUE;
  1204. #endif
  1205. modeoff = conn->bits.ftp_use_epsv?0:1;
  1206. PPSENDF(&ftpc->pp, "%s", mode[modeoff]);
  1207. ftpc->count1 = modeoff;
  1208. state(conn, FTP_PASV);
  1209. infof(conn->data, "Connect data stream passively\n");
  1210. return result;
  1211. }
  1212. /*
  1213. * ftp_state_prepare_transfer() starts PORT, PASV or PRET etc.
  1214. *
  1215. * REST is the last command in the chain of commands when a "head"-like
  1216. * request is made. Thus, if an actual transfer is to be made this is where we
  1217. * take off for real.
  1218. */
  1219. static CURLcode ftp_state_prepare_transfer(struct connectdata *conn)
  1220. {
  1221. CURLcode result = CURLE_OK;
  1222. struct FTP *ftp = conn->data->req.protop;
  1223. struct Curl_easy *data = conn->data;
  1224. if(ftp->transfer != FTPTRANSFER_BODY) {
  1225. /* doesn't transfer any data */
  1226. /* still possibly do PRE QUOTE jobs */
  1227. state(conn, FTP_RETR_PREQUOTE);
  1228. result = ftp_state_quote(conn, TRUE, FTP_RETR_PREQUOTE);
  1229. }
  1230. else if(data->set.ftp_use_port) {
  1231. /* We have chosen to use the PORT (or similar) command */
  1232. result = ftp_state_use_port(conn, EPRT);
  1233. }
  1234. else {
  1235. /* We have chosen (this is default) to use the PASV (or similar) command */
  1236. if(data->set.ftp_use_pret) {
  1237. /* The user has requested that we send a PRET command
  1238. to prepare the server for the upcoming PASV */
  1239. if(!conn->proto.ftpc.file) {
  1240. PPSENDF(&conn->proto.ftpc.pp, "PRET %s",
  1241. data->set.str[STRING_CUSTOMREQUEST]?
  1242. data->set.str[STRING_CUSTOMREQUEST]:
  1243. (data->set.ftp_list_only?"NLST":"LIST"));
  1244. }
  1245. else if(data->set.upload) {
  1246. PPSENDF(&conn->proto.ftpc.pp, "PRET STOR %s", conn->proto.ftpc.file);
  1247. }
  1248. else {
  1249. PPSENDF(&conn->proto.ftpc.pp, "PRET RETR %s", conn->proto.ftpc.file);
  1250. }
  1251. state(conn, FTP_PRET);
  1252. }
  1253. else {
  1254. result = ftp_state_use_pasv(conn);
  1255. }
  1256. }
  1257. return result;
  1258. }
  1259. static CURLcode ftp_state_rest(struct connectdata *conn)
  1260. {
  1261. CURLcode result = CURLE_OK;
  1262. struct FTP *ftp = conn->data->req.protop;
  1263. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1264. if((ftp->transfer != FTPTRANSFER_BODY) && ftpc->file) {
  1265. /* if a "head"-like request is being made (on a file) */
  1266. /* Determine if server can respond to REST command and therefore
  1267. whether it supports range */
  1268. PPSENDF(&conn->proto.ftpc.pp, "REST %d", 0);
  1269. state(conn, FTP_REST);
  1270. }
  1271. else
  1272. result = ftp_state_prepare_transfer(conn);
  1273. return result;
  1274. }
  1275. static CURLcode ftp_state_size(struct connectdata *conn)
  1276. {
  1277. CURLcode result = CURLE_OK;
  1278. struct FTP *ftp = conn->data->req.protop;
  1279. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1280. if((ftp->transfer == FTPTRANSFER_INFO) && ftpc->file) {
  1281. /* if a "head"-like request is being made (on a file) */
  1282. /* we know ftpc->file is a valid pointer to a file name */
  1283. PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
  1284. state(conn, FTP_SIZE);
  1285. }
  1286. else
  1287. result = ftp_state_rest(conn);
  1288. return result;
  1289. }
  1290. static CURLcode ftp_state_list(struct connectdata *conn)
  1291. {
  1292. CURLcode result = CURLE_OK;
  1293. struct Curl_easy *data = conn->data;
  1294. /* If this output is to be machine-parsed, the NLST command might be better
  1295. to use, since the LIST command output is not specified or standard in any
  1296. way. It has turned out that the NLST list output is not the same on all
  1297. servers either... */
  1298. /*
  1299. if FTPFILE_NOCWD was specified, we are currently in
  1300. the user's home directory, so we should add the path
  1301. as argument for the LIST / NLST / or custom command.
  1302. Whether the server will support this, is uncertain.
  1303. The other ftp_filemethods will CWD into dir/dir/ first and
  1304. then just do LIST (in that case: nothing to do here)
  1305. */
  1306. char *cmd, *lstArg, *slashPos;
  1307. lstArg = NULL;
  1308. if((data->set.ftp_filemethod == FTPFILE_NOCWD) &&
  1309. data->state.path &&
  1310. data->state.path[0] &&
  1311. strchr(data->state.path, '/')) {
  1312. lstArg = strdup(data->state.path);
  1313. if(!lstArg)
  1314. return CURLE_OUT_OF_MEMORY;
  1315. /* Check if path does not end with /, as then we cut off the file part */
  1316. if(lstArg[strlen(lstArg) - 1] != '/') {
  1317. /* chop off the file part if format is dir/dir/file */
  1318. slashPos = strrchr(lstArg, '/');
  1319. if(slashPos)
  1320. *(slashPos+1) = '\0';
  1321. }
  1322. }
  1323. cmd = aprintf("%s%s%s",
  1324. data->set.str[STRING_CUSTOMREQUEST]?
  1325. data->set.str[STRING_CUSTOMREQUEST]:
  1326. (data->set.ftp_list_only?"NLST":"LIST"),
  1327. lstArg? " ": "",
  1328. lstArg? lstArg: "");
  1329. if(!cmd) {
  1330. free(lstArg);
  1331. return CURLE_OUT_OF_MEMORY;
  1332. }
  1333. result = Curl_pp_sendf(&conn->proto.ftpc.pp, "%s", cmd);
  1334. free(lstArg);
  1335. free(cmd);
  1336. if(result)
  1337. return result;
  1338. state(conn, FTP_LIST);
  1339. return result;
  1340. }
  1341. static CURLcode ftp_state_retr_prequote(struct connectdata *conn)
  1342. {
  1343. CURLcode result = CURLE_OK;
  1344. /* We've sent the TYPE, now we must send the list of prequote strings */
  1345. result = ftp_state_quote(conn, TRUE, FTP_RETR_PREQUOTE);
  1346. return result;
  1347. }
  1348. static CURLcode ftp_state_stor_prequote(struct connectdata *conn)
  1349. {
  1350. CURLcode result = CURLE_OK;
  1351. /* We've sent the TYPE, now we must send the list of prequote strings */
  1352. result = ftp_state_quote(conn, TRUE, FTP_STOR_PREQUOTE);
  1353. return result;
  1354. }
  1355. static CURLcode ftp_state_type(struct connectdata *conn)
  1356. {
  1357. CURLcode result = CURLE_OK;
  1358. struct FTP *ftp = conn->data->req.protop;
  1359. struct Curl_easy *data = conn->data;
  1360. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1361. /* If we have selected NOBODY and HEADER, it means that we only want file
  1362. information. Which in FTP can't be much more than the file size and
  1363. date. */
  1364. if(data->set.opt_no_body && ftpc->file &&
  1365. ftp_need_type(conn, data->set.prefer_ascii)) {
  1366. /* The SIZE command is _not_ RFC 959 specified, and therefor many servers
  1367. may not support it! It is however the only way we have to get a file's
  1368. size! */
  1369. ftp->transfer = FTPTRANSFER_INFO;
  1370. /* this means no actual transfer will be made */
  1371. /* Some servers return different sizes for different modes, and thus we
  1372. must set the proper type before we check the size */
  1373. result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_TYPE);
  1374. if(result)
  1375. return result;
  1376. }
  1377. else
  1378. result = ftp_state_size(conn);
  1379. return result;
  1380. }
  1381. /* This is called after the CWD commands have been done in the beginning of
  1382. the DO phase */
  1383. static CURLcode ftp_state_mdtm(struct connectdata *conn)
  1384. {
  1385. CURLcode result = CURLE_OK;
  1386. struct Curl_easy *data = conn->data;
  1387. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1388. /* Requested time of file or time-depended transfer? */
  1389. if((data->set.get_filetime || data->set.timecondition) && ftpc->file) {
  1390. /* we have requested to get the modified-time of the file, this is a white
  1391. spot as the MDTM is not mentioned in RFC959 */
  1392. PPSENDF(&ftpc->pp, "MDTM %s", ftpc->file);
  1393. state(conn, FTP_MDTM);
  1394. }
  1395. else
  1396. result = ftp_state_type(conn);
  1397. return result;
  1398. }
  1399. /* This is called after the TYPE and possible quote commands have been sent */
  1400. static CURLcode ftp_state_ul_setup(struct connectdata *conn,
  1401. bool sizechecked)
  1402. {
  1403. CURLcode result = CURLE_OK;
  1404. struct FTP *ftp = conn->data->req.protop;
  1405. struct Curl_easy *data = conn->data;
  1406. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1407. int seekerr = CURL_SEEKFUNC_OK;
  1408. if((data->state.resume_from && !sizechecked) ||
  1409. ((data->state.resume_from > 0) && sizechecked)) {
  1410. /* we're about to continue the uploading of a file */
  1411. /* 1. get already existing file's size. We use the SIZE command for this
  1412. which may not exist in the server! The SIZE command is not in
  1413. RFC959. */
  1414. /* 2. This used to set REST. But since we can do append, we
  1415. don't another ftp command. We just skip the source file
  1416. offset and then we APPEND the rest on the file instead */
  1417. /* 3. pass file-size number of bytes in the source file */
  1418. /* 4. lower the infilesize counter */
  1419. /* => transfer as usual */
  1420. if(data->state.resume_from < 0) {
  1421. /* Got no given size to start from, figure it out */
  1422. PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
  1423. state(conn, FTP_STOR_SIZE);
  1424. return result;
  1425. }
  1426. /* enable append */
  1427. data->set.ftp_append = TRUE;
  1428. /* Let's read off the proper amount of bytes from the input. */
  1429. if(conn->seek_func) {
  1430. seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
  1431. SEEK_SET);
  1432. }
  1433. if(seekerr != CURL_SEEKFUNC_OK) {
  1434. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  1435. failf(data, "Could not seek stream");
  1436. return CURLE_FTP_COULDNT_USE_REST;
  1437. }
  1438. /* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  1439. else {
  1440. curl_off_t passed=0;
  1441. do {
  1442. size_t readthisamountnow =
  1443. (data->state.resume_from - passed > CURL_OFF_T_C(BUFSIZE)) ?
  1444. BUFSIZE : curlx_sotouz(data->state.resume_from - passed);
  1445. size_t actuallyread =
  1446. data->state.fread_func(data->state.buffer, 1, readthisamountnow,
  1447. data->state.in);
  1448. passed += actuallyread;
  1449. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  1450. /* this checks for greater-than only to make sure that the
  1451. CURL_READFUNC_ABORT return code still aborts */
  1452. failf(data, "Failed to read data");
  1453. return CURLE_FTP_COULDNT_USE_REST;
  1454. }
  1455. } while(passed < data->state.resume_from);
  1456. }
  1457. }
  1458. /* now, decrease the size of the read */
  1459. if(data->state.infilesize>0) {
  1460. data->state.infilesize -= data->state.resume_from;
  1461. if(data->state.infilesize <= 0) {
  1462. infof(data, "File already completely uploaded\n");
  1463. /* no data to transfer */
  1464. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1465. /* Set ->transfer so that we won't get any error in
  1466. * ftp_done() because we didn't transfer anything! */
  1467. ftp->transfer = FTPTRANSFER_NONE;
  1468. state(conn, FTP_STOP);
  1469. return CURLE_OK;
  1470. }
  1471. }
  1472. /* we've passed, proceed as normal */
  1473. } /* resume_from */
  1474. PPSENDF(&ftpc->pp, data->set.ftp_append?"APPE %s":"STOR %s",
  1475. ftpc->file);
  1476. state(conn, FTP_STOR);
  1477. return result;
  1478. }
  1479. static CURLcode ftp_state_quote(struct connectdata *conn,
  1480. bool init,
  1481. ftpstate instate)
  1482. {
  1483. CURLcode result = CURLE_OK;
  1484. struct Curl_easy *data = conn->data;
  1485. struct FTP *ftp = data->req.protop;
  1486. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1487. bool quote=FALSE;
  1488. struct curl_slist *item;
  1489. switch(instate) {
  1490. case FTP_QUOTE:
  1491. default:
  1492. item = data->set.quote;
  1493. break;
  1494. case FTP_RETR_PREQUOTE:
  1495. case FTP_STOR_PREQUOTE:
  1496. item = data->set.prequote;
  1497. break;
  1498. case FTP_POSTQUOTE:
  1499. item = data->set.postquote;
  1500. break;
  1501. }
  1502. /*
  1503. * This state uses:
  1504. * 'count1' to iterate over the commands to send
  1505. * 'count2' to store wether to allow commands to fail
  1506. */
  1507. if(init)
  1508. ftpc->count1 = 0;
  1509. else
  1510. ftpc->count1++;
  1511. if(item) {
  1512. int i = 0;
  1513. /* Skip count1 items in the linked list */
  1514. while((i< ftpc->count1) && item) {
  1515. item = item->next;
  1516. i++;
  1517. }
  1518. if(item) {
  1519. char *cmd = item->data;
  1520. if(cmd[0] == '*') {
  1521. cmd++;
  1522. ftpc->count2 = 1; /* the sent command is allowed to fail */
  1523. }
  1524. else
  1525. ftpc->count2 = 0; /* failure means cancel operation */
  1526. PPSENDF(&ftpc->pp, "%s", cmd);
  1527. state(conn, instate);
  1528. quote = TRUE;
  1529. }
  1530. }
  1531. if(!quote) {
  1532. /* No more quote to send, continue to ... */
  1533. switch(instate) {
  1534. case FTP_QUOTE:
  1535. default:
  1536. result = ftp_state_cwd(conn);
  1537. break;
  1538. case FTP_RETR_PREQUOTE:
  1539. if(ftp->transfer != FTPTRANSFER_BODY)
  1540. state(conn, FTP_STOP);
  1541. else {
  1542. if(ftpc->known_filesize != -1) {
  1543. Curl_pgrsSetDownloadSize(data, ftpc->known_filesize);
  1544. result = ftp_state_retr(conn, ftpc->known_filesize);
  1545. }
  1546. else {
  1547. if(data->set.ignorecl) {
  1548. /* This code is to support download of growing files. It prevents
  1549. the state machine from requesting the file size from the
  1550. server. With an unknown file size the download continues until
  1551. the server terminates it, otherwise the client stops if the
  1552. received byte count exceeds the reported file size. Set option
  1553. CURLOPT_IGNORE_CONTENT_LENGTH to 1 to enable this behavior.*/
  1554. PPSENDF(&ftpc->pp, "RETR %s", ftpc->file);
  1555. state(conn, FTP_RETR);
  1556. }
  1557. else {
  1558. PPSENDF(&ftpc->pp, "SIZE %s", ftpc->file);
  1559. state(conn, FTP_RETR_SIZE);
  1560. }
  1561. }
  1562. }
  1563. break;
  1564. case FTP_STOR_PREQUOTE:
  1565. result = ftp_state_ul_setup(conn, FALSE);
  1566. break;
  1567. case FTP_POSTQUOTE:
  1568. break;
  1569. }
  1570. }
  1571. return result;
  1572. }
  1573. /* called from ftp_state_pasv_resp to switch to PASV in case of EPSV
  1574. problems */
  1575. static CURLcode ftp_epsv_disable(struct connectdata *conn)
  1576. {
  1577. CURLcode result = CURLE_OK;
  1578. if(conn->bits.ipv6) {
  1579. /* We can't disable EPSV when doing IPv6, so this is instead a fail */
  1580. failf(conn->data, "Failed EPSV attempt, exiting\n");
  1581. return CURLE_WEIRD_SERVER_REPLY;
  1582. }
  1583. infof(conn->data, "Failed EPSV attempt. Disabling EPSV\n");
  1584. /* disable it for next transfer */
  1585. conn->bits.ftp_use_epsv = FALSE;
  1586. conn->data->state.errorbuf = FALSE; /* allow error message to get
  1587. rewritten */
  1588. PPSENDF(&conn->proto.ftpc.pp, "%s", "PASV");
  1589. conn->proto.ftpc.count1++;
  1590. /* remain in/go to the FTP_PASV state */
  1591. state(conn, FTP_PASV);
  1592. return result;
  1593. }
  1594. static char *control_address(struct connectdata *conn)
  1595. {
  1596. /* Returns the control connection IP address.
  1597. If a proxy tunnel is used, returns the original host name instead, because
  1598. the effective control connection address is the proxy address,
  1599. not the ftp host. */
  1600. if(conn->bits.tunnel_proxy || conn->bits.socksproxy)
  1601. return conn->host.name;
  1602. return conn->ip_addr_str;
  1603. }
  1604. static CURLcode ftp_state_pasv_resp(struct connectdata *conn,
  1605. int ftpcode)
  1606. {
  1607. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1608. CURLcode result;
  1609. struct Curl_easy *data=conn->data;
  1610. struct Curl_dns_entry *addr=NULL;
  1611. int rc;
  1612. unsigned short connectport; /* the local port connect() should use! */
  1613. char *str=&data->state.buffer[4]; /* start on the first letter */
  1614. /* if we come here again, make sure the former name is cleared */
  1615. Curl_safefree(ftpc->newhost);
  1616. if((ftpc->count1 == 0) &&
  1617. (ftpcode == 229)) {
  1618. /* positive EPSV response */
  1619. char *ptr = strchr(str, '(');
  1620. if(ptr) {
  1621. unsigned int num;
  1622. char separator[4];
  1623. ptr++;
  1624. if(5 == sscanf(ptr, "%c%c%c%u%c",
  1625. &separator[0],
  1626. &separator[1],
  1627. &separator[2],
  1628. &num,
  1629. &separator[3])) {
  1630. const char sep1 = separator[0];
  1631. int i;
  1632. /* The four separators should be identical, or else this is an oddly
  1633. formatted reply and we bail out immediately. */
  1634. for(i=1; i<4; i++) {
  1635. if(separator[i] != sep1) {
  1636. ptr=NULL; /* set to NULL to signal error */
  1637. break;
  1638. }
  1639. }
  1640. if(num > 0xffff) {
  1641. failf(data, "Illegal port number in EPSV reply");
  1642. return CURLE_FTP_WEIRD_PASV_REPLY;
  1643. }
  1644. if(ptr) {
  1645. ftpc->newport = (unsigned short)(num & 0xffff);
  1646. ftpc->newhost = strdup(control_address(conn));
  1647. if(!ftpc->newhost)
  1648. return CURLE_OUT_OF_MEMORY;
  1649. }
  1650. }
  1651. else
  1652. ptr=NULL;
  1653. }
  1654. if(!ptr) {
  1655. failf(data, "Weirdly formatted EPSV reply");
  1656. return CURLE_FTP_WEIRD_PASV_REPLY;
  1657. }
  1658. }
  1659. else if((ftpc->count1 == 1) &&
  1660. (ftpcode == 227)) {
  1661. /* positive PASV response */
  1662. int ip[4];
  1663. int port[2];
  1664. /*
  1665. * Scan for a sequence of six comma-separated numbers and use them as
  1666. * IP+port indicators.
  1667. *
  1668. * Found reply-strings include:
  1669. * "227 Entering Passive Mode (127,0,0,1,4,51)"
  1670. * "227 Data transfer will passively listen to 127,0,0,1,4,51"
  1671. * "227 Entering passive mode. 127,0,0,1,4,51"
  1672. */
  1673. while(*str) {
  1674. if(6 == sscanf(str, "%d,%d,%d,%d,%d,%d",
  1675. &ip[0], &ip[1], &ip[2], &ip[3],
  1676. &port[0], &port[1]))
  1677. break;
  1678. str++;
  1679. }
  1680. if(!*str) {
  1681. failf(data, "Couldn't interpret the 227-response");
  1682. return CURLE_FTP_WEIRD_227_FORMAT;
  1683. }
  1684. /* we got OK from server */
  1685. if(data->set.ftp_skip_ip) {
  1686. /* told to ignore the remotely given IP but instead use the host we used
  1687. for the control connection */
  1688. infof(data, "Skip %d.%d.%d.%d for data connection, re-use %s instead\n",
  1689. ip[0], ip[1], ip[2], ip[3],
  1690. conn->host.name);
  1691. ftpc->newhost = strdup(control_address(conn));
  1692. }
  1693. else
  1694. ftpc->newhost = aprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  1695. if(!ftpc->newhost)
  1696. return CURLE_OUT_OF_MEMORY;
  1697. ftpc->newport = (unsigned short)(((port[0]<<8) + port[1]) & 0xffff);
  1698. }
  1699. else if(ftpc->count1 == 0) {
  1700. /* EPSV failed, move on to PASV */
  1701. return ftp_epsv_disable(conn);
  1702. }
  1703. else {
  1704. failf(data, "Bad PASV/EPSV response: %03d", ftpcode);
  1705. return CURLE_FTP_WEIRD_PASV_REPLY;
  1706. }
  1707. if(conn->bits.proxy) {
  1708. /*
  1709. * This connection uses a proxy and we need to connect to the proxy again
  1710. * here. We don't want to rely on a former host lookup that might've
  1711. * expired now, instead we remake the lookup here and now!
  1712. */
  1713. const char * const host_name = conn->bits.socksproxy ?
  1714. conn->socks_proxy.host.name : conn->http_proxy.host.name;
  1715. rc = Curl_resolv(conn, host_name, (int)conn->port, &addr);
  1716. if(rc == CURLRESOLV_PENDING)
  1717. /* BLOCKING, ignores the return code but 'addr' will be NULL in
  1718. case of failure */
  1719. (void)Curl_resolver_wait_resolv(conn, &addr);
  1720. connectport =
  1721. (unsigned short)conn->port; /* we connect to the proxy's port */
  1722. if(!addr) {
  1723. failf(data, "Can't resolve proxy host %s:%hu", host_name, connectport);
  1724. return CURLE_COULDNT_RESOLVE_PROXY;
  1725. }
  1726. }
  1727. else {
  1728. /* normal, direct, ftp connection */
  1729. rc = Curl_resolv(conn, ftpc->newhost, ftpc->newport, &addr);
  1730. if(rc == CURLRESOLV_PENDING)
  1731. /* BLOCKING */
  1732. (void)Curl_resolver_wait_resolv(conn, &addr);
  1733. connectport = ftpc->newport; /* we connect to the remote port */
  1734. if(!addr) {
  1735. failf(data, "Can't resolve new host %s:%hu", ftpc->newhost, connectport);
  1736. return CURLE_FTP_CANT_GET_HOST;
  1737. }
  1738. }
  1739. conn->bits.tcpconnect[SECONDARYSOCKET] = FALSE;
  1740. result = Curl_connecthost(conn, addr);
  1741. if(result) {
  1742. Curl_resolv_unlock(data, addr); /* we're done using this address */
  1743. if(ftpc->count1 == 0 && ftpcode == 229)
  1744. return ftp_epsv_disable(conn);
  1745. return result;
  1746. }
  1747. /*
  1748. * When this is used from the multi interface, this might've returned with
  1749. * the 'connected' set to FALSE and thus we are now awaiting a non-blocking
  1750. * connect to connect.
  1751. */
  1752. if(data->set.verbose)
  1753. /* this just dumps information about this second connection */
  1754. ftp_pasv_verbose(conn, addr->addr, ftpc->newhost, connectport);
  1755. Curl_safefree(conn->secondaryhostname);
  1756. conn->secondaryhostname = strdup(ftpc->newhost);
  1757. conn->secondary_port = ftpc->newport;
  1758. Curl_resolv_unlock(data, addr); /* we're done using this address */
  1759. conn->bits.do_more = TRUE;
  1760. state(conn, FTP_STOP); /* this phase is completed */
  1761. return result;
  1762. }
  1763. static CURLcode ftp_state_port_resp(struct connectdata *conn,
  1764. int ftpcode)
  1765. {
  1766. struct Curl_easy *data = conn->data;
  1767. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1768. ftpport fcmd = (ftpport)ftpc->count1;
  1769. CURLcode result = CURLE_OK;
  1770. /* The FTP spec tells a positive response should have code 200.
  1771. Be more permissive here to tolerate deviant servers. */
  1772. if(ftpcode / 100 != 2) {
  1773. /* the command failed */
  1774. if(EPRT == fcmd) {
  1775. infof(data, "disabling EPRT usage\n");
  1776. conn->bits.ftp_use_eprt = FALSE;
  1777. }
  1778. fcmd++;
  1779. if(fcmd == DONE) {
  1780. failf(data, "Failed to do PORT");
  1781. result = CURLE_FTP_PORT_FAILED;
  1782. }
  1783. else
  1784. /* try next */
  1785. result = ftp_state_use_port(conn, fcmd);
  1786. }
  1787. else {
  1788. infof(data, "Connect data stream actively\n");
  1789. state(conn, FTP_STOP); /* end of DO phase */
  1790. result = ftp_dophase_done(conn, FALSE);
  1791. }
  1792. return result;
  1793. }
  1794. static CURLcode ftp_state_mdtm_resp(struct connectdata *conn,
  1795. int ftpcode)
  1796. {
  1797. CURLcode result = CURLE_OK;
  1798. struct Curl_easy *data=conn->data;
  1799. struct FTP *ftp = data->req.protop;
  1800. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1801. switch(ftpcode) {
  1802. case 213:
  1803. {
  1804. /* we got a time. Format should be: "YYYYMMDDHHMMSS[.sss]" where the
  1805. last .sss part is optional and means fractions of a second */
  1806. int year, month, day, hour, minute, second;
  1807. char *buf = data->state.buffer;
  1808. if(6 == sscanf(buf+4, "%04d%02d%02d%02d%02d%02d",
  1809. &year, &month, &day, &hour, &minute, &second)) {
  1810. /* we have a time, reformat it */
  1811. time_t secs=time(NULL);
  1812. /* using the good old yacc/bison yuck */
  1813. snprintf(buf, CURL_BUFSIZE(conn->data->set.buffer_size),
  1814. "%04d%02d%02d %02d:%02d:%02d GMT",
  1815. year, month, day, hour, minute, second);
  1816. /* now, convert this into a time() value: */
  1817. data->info.filetime = (long)curl_getdate(buf, &secs);
  1818. }
  1819. #ifdef CURL_FTP_HTTPSTYLE_HEAD
  1820. /* If we asked for a time of the file and we actually got one as well,
  1821. we "emulate" a HTTP-style header in our output. */
  1822. if(data->set.opt_no_body &&
  1823. ftpc->file &&
  1824. data->set.get_filetime &&
  1825. (data->info.filetime>=0) ) {
  1826. time_t filetime = (time_t)data->info.filetime;
  1827. struct tm buffer;
  1828. const struct tm *tm = &buffer;
  1829. result = Curl_gmtime(filetime, &buffer);
  1830. if(result)
  1831. return result;
  1832. /* format: "Tue, 15 Nov 1994 12:45:26" */
  1833. snprintf(buf, BUFSIZE-1,
  1834. "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
  1835. Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
  1836. tm->tm_mday,
  1837. Curl_month[tm->tm_mon],
  1838. tm->tm_year + 1900,
  1839. tm->tm_hour,
  1840. tm->tm_min,
  1841. tm->tm_sec);
  1842. result = Curl_client_write(conn, CLIENTWRITE_BOTH, buf, 0);
  1843. if(result)
  1844. return result;
  1845. } /* end of a ridiculous amount of conditionals */
  1846. #endif
  1847. }
  1848. break;
  1849. default:
  1850. infof(data, "unsupported MDTM reply format\n");
  1851. break;
  1852. case 550: /* "No such file or directory" */
  1853. failf(data, "Given file does not exist");
  1854. result = CURLE_FTP_COULDNT_RETR_FILE;
  1855. break;
  1856. }
  1857. if(data->set.timecondition) {
  1858. if((data->info.filetime > 0) && (data->set.timevalue > 0)) {
  1859. switch(data->set.timecondition) {
  1860. case CURL_TIMECOND_IFMODSINCE:
  1861. default:
  1862. if(data->info.filetime <= data->set.timevalue) {
  1863. infof(data, "The requested document is not new enough\n");
  1864. ftp->transfer = FTPTRANSFER_NONE; /* mark to not transfer data */
  1865. data->info.timecond = TRUE;
  1866. state(conn, FTP_STOP);
  1867. return CURLE_OK;
  1868. }
  1869. break;
  1870. case CURL_TIMECOND_IFUNMODSINCE:
  1871. if(data->info.filetime > data->set.timevalue) {
  1872. infof(data, "The requested document is not old enough\n");
  1873. ftp->transfer = FTPTRANSFER_NONE; /* mark to not transfer data */
  1874. data->info.timecond = TRUE;
  1875. state(conn, FTP_STOP);
  1876. return CURLE_OK;
  1877. }
  1878. break;
  1879. } /* switch */
  1880. }
  1881. else {
  1882. infof(data, "Skipping time comparison\n");
  1883. }
  1884. }
  1885. if(!result)
  1886. result = ftp_state_type(conn);
  1887. return result;
  1888. }
  1889. static CURLcode ftp_state_type_resp(struct connectdata *conn,
  1890. int ftpcode,
  1891. ftpstate instate)
  1892. {
  1893. CURLcode result = CURLE_OK;
  1894. struct Curl_easy *data=conn->data;
  1895. if(ftpcode/100 != 2) {
  1896. /* "sasserftpd" and "(u)r(x)bot ftpd" both responds with 226 after a
  1897. successful 'TYPE I'. While that is not as RFC959 says, it is still a
  1898. positive response code and we allow that. */
  1899. failf(data, "Couldn't set desired mode");
  1900. return CURLE_FTP_COULDNT_SET_TYPE;
  1901. }
  1902. if(ftpcode != 200)
  1903. infof(data, "Got a %03d response code instead of the assumed 200\n",
  1904. ftpcode);
  1905. if(instate == FTP_TYPE)
  1906. result = ftp_state_size(conn);
  1907. else if(instate == FTP_LIST_TYPE)
  1908. result = ftp_state_list(conn);
  1909. else if(instate == FTP_RETR_TYPE)
  1910. result = ftp_state_retr_prequote(conn);
  1911. else if(instate == FTP_STOR_TYPE)
  1912. result = ftp_state_stor_prequote(conn);
  1913. return result;
  1914. }
  1915. static CURLcode ftp_state_retr(struct connectdata *conn,
  1916. curl_off_t filesize)
  1917. {
  1918. CURLcode result = CURLE_OK;
  1919. struct Curl_easy *data=conn->data;
  1920. struct FTP *ftp = data->req.protop;
  1921. struct ftp_conn *ftpc = &conn->proto.ftpc;
  1922. if(data->set.max_filesize && (filesize > data->set.max_filesize)) {
  1923. failf(data, "Maximum file size exceeded");
  1924. return CURLE_FILESIZE_EXCEEDED;
  1925. }
  1926. ftp->downloadsize = filesize;
  1927. if(data->state.resume_from) {
  1928. /* We always (attempt to) get the size of downloads, so it is done before
  1929. this even when not doing resumes. */
  1930. if(filesize == -1) {
  1931. infof(data, "ftp server doesn't support SIZE\n");
  1932. /* We couldn't get the size and therefore we can't know if there really
  1933. is a part of the file left to get, although the server will just
  1934. close the connection when we start the connection so it won't cause
  1935. us any harm, just not make us exit as nicely. */
  1936. }
  1937. else {
  1938. /* We got a file size report, so we check that there actually is a
  1939. part of the file left to get, or else we go home. */
  1940. if(data->state.resume_from< 0) {
  1941. /* We're supposed to download the last abs(from) bytes */
  1942. if(filesize < -data->state.resume_from) {
  1943. failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
  1944. ") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
  1945. data->state.resume_from, filesize);
  1946. return CURLE_BAD_DOWNLOAD_RESUME;
  1947. }
  1948. /* convert to size to download */
  1949. ftp->downloadsize = -data->state.resume_from;
  1950. /* download from where? */
  1951. data->state.resume_from = filesize - ftp->downloadsize;
  1952. }
  1953. else {
  1954. if(filesize < data->state.resume_from) {
  1955. failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
  1956. ") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
  1957. data->state.resume_from, filesize);
  1958. return CURLE_BAD_DOWNLOAD_RESUME;
  1959. }
  1960. /* Now store the number of bytes we are expected to download */
  1961. ftp->downloadsize = filesize-data->state.resume_from;
  1962. }
  1963. }
  1964. if(ftp->downloadsize == 0) {
  1965. /* no data to transfer */
  1966. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1967. infof(data, "File already completely downloaded\n");
  1968. /* Set ->transfer so that we won't get any error in ftp_done()
  1969. * because we didn't transfer the any file */
  1970. ftp->transfer = FTPTRANSFER_NONE;
  1971. state(conn, FTP_STOP);
  1972. return CURLE_OK;
  1973. }
  1974. /* Set resume file transfer offset */
  1975. infof(data, "Instructs server to resume from offset %"
  1976. CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
  1977. PPSENDF(&ftpc->pp, "REST %" CURL_FORMAT_CURL_OFF_T,
  1978. data->state.resume_from);
  1979. state(conn, FTP_RETR_REST);
  1980. }
  1981. else {
  1982. /* no resume */
  1983. PPSENDF(&ftpc->pp, "RETR %s", ftpc->file);
  1984. state(conn, FTP_RETR);
  1985. }
  1986. return result;
  1987. }
  1988. static CURLcode ftp_state_size_resp(struct connectdata *conn,
  1989. int ftpcode,
  1990. ftpstate instate)
  1991. {
  1992. CURLcode result = CURLE_OK;
  1993. struct Curl_easy *data=conn->data;
  1994. curl_off_t filesize;
  1995. char *buf = data->state.buffer;
  1996. /* get the size from the ascii string: */
  1997. filesize = (ftpcode == 213)?curlx_strtoofft(buf+4, NULL, 0):-1;
  1998. if(instate == FTP_SIZE) {
  1999. #ifdef CURL_FTP_HTTPSTYLE_HEAD
  2000. if(-1 != filesize) {
  2001. snprintf(buf, CURL_BUFSIZE(data->set.buffer_size),
  2002. "Content-Length: %" CURL_FORMAT_CURL_OFF_T "\r\n", filesize);
  2003. result = Curl_client_write(conn, CLIENTWRITE_BOTH, buf, 0);
  2004. if(result)
  2005. return result;
  2006. }
  2007. #endif
  2008. Curl_pgrsSetDownloadSize(data, filesize);
  2009. result = ftp_state_rest(conn);
  2010. }
  2011. else if(instate == FTP_RETR_SIZE) {
  2012. Curl_pgrsSetDownloadSize(data, filesize);
  2013. result = ftp_state_retr(conn, filesize);
  2014. }
  2015. else if(instate == FTP_STOR_SIZE) {
  2016. data->state.resume_from = filesize;
  2017. result = ftp_state_ul_setup(conn, TRUE);
  2018. }
  2019. return result;
  2020. }
  2021. static CURLcode ftp_state_rest_resp(struct connectdata *conn,
  2022. int ftpcode,
  2023. ftpstate instate)
  2024. {
  2025. CURLcode result = CURLE_OK;
  2026. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2027. switch(instate) {
  2028. case FTP_REST:
  2029. default:
  2030. #ifdef CURL_FTP_HTTPSTYLE_HEAD
  2031. if(ftpcode == 350) {
  2032. char buffer[24]= { "Accept-ranges: bytes\r\n" };
  2033. result = Curl_client_write(conn, CLIENTWRITE_BOTH, buffer, 0);
  2034. if(result)
  2035. return result;
  2036. }
  2037. #endif
  2038. result = ftp_state_prepare_transfer(conn);
  2039. break;
  2040. case FTP_RETR_REST:
  2041. if(ftpcode != 350) {
  2042. failf(conn->data, "Couldn't use REST");
  2043. result = CURLE_FTP_COULDNT_USE_REST;
  2044. }
  2045. else {
  2046. PPSENDF(&ftpc->pp, "RETR %s", ftpc->file);
  2047. state(conn, FTP_RETR);
  2048. }
  2049. break;
  2050. }
  2051. return result;
  2052. }
  2053. static CURLcode ftp_state_stor_resp(struct connectdata *conn,
  2054. int ftpcode, ftpstate instate)
  2055. {
  2056. CURLcode result = CURLE_OK;
  2057. struct Curl_easy *data = conn->data;
  2058. if(ftpcode>=400) {
  2059. failf(data, "Failed FTP upload: %0d", ftpcode);
  2060. state(conn, FTP_STOP);
  2061. /* oops, we never close the sockets! */
  2062. return CURLE_UPLOAD_FAILED;
  2063. }
  2064. conn->proto.ftpc.state_saved = instate;
  2065. /* PORT means we are now awaiting the server to connect to us. */
  2066. if(data->set.ftp_use_port) {
  2067. bool connected;
  2068. state(conn, FTP_STOP); /* no longer in STOR state */
  2069. result = AllowServerConnect(conn, &connected);
  2070. if(result)
  2071. return result;
  2072. if(!connected) {
  2073. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2074. infof(data, "Data conn was not available immediately\n");
  2075. ftpc->wait_data_conn = TRUE;
  2076. }
  2077. return CURLE_OK;
  2078. }
  2079. else
  2080. return InitiateTransfer(conn);
  2081. }
  2082. /* for LIST and RETR responses */
  2083. static CURLcode ftp_state_get_resp(struct connectdata *conn,
  2084. int ftpcode,
  2085. ftpstate instate)
  2086. {
  2087. CURLcode result = CURLE_OK;
  2088. struct Curl_easy *data = conn->data;
  2089. struct FTP *ftp = data->req.protop;
  2090. char *buf = data->state.buffer;
  2091. if((ftpcode == 150) || (ftpcode == 125)) {
  2092. /*
  2093. A;
  2094. 150 Opening BINARY mode data connection for /etc/passwd (2241
  2095. bytes). (ok, the file is being transferred)
  2096. B:
  2097. 150 Opening ASCII mode data connection for /bin/ls
  2098. C:
  2099. 150 ASCII data connection for /bin/ls (137.167.104.91,37445) (0 bytes).
  2100. D:
  2101. 150 Opening ASCII mode data connection for [file] (0.0.0.0,0) (545 bytes)
  2102. E:
  2103. 125 Data connection already open; Transfer starting. */
  2104. curl_off_t size=-1; /* default unknown size */
  2105. /*
  2106. * It appears that there are FTP-servers that return size 0 for files when
  2107. * SIZE is used on the file while being in BINARY mode. To work around
  2108. * that (stupid) behavior, we attempt to parse the RETR response even if
  2109. * the SIZE returned size zero.
  2110. *
  2111. * Debugging help from Salvatore Sorrentino on February 26, 2003.
  2112. */
  2113. if((instate != FTP_LIST) &&
  2114. !data->set.prefer_ascii &&
  2115. (ftp->downloadsize < 1)) {
  2116. /*
  2117. * It seems directory listings either don't show the size or very
  2118. * often uses size 0 anyway. ASCII transfers may very well turn out
  2119. * that the transferred amount of data is not the same as this line
  2120. * tells, why using this number in those cases only confuses us.
  2121. *
  2122. * Example D above makes this parsing a little tricky */
  2123. char *bytes;
  2124. bytes=strstr(buf, " bytes");
  2125. if(bytes--) {
  2126. long in=(long)(bytes-buf);
  2127. /* this is a hint there is size information in there! ;-) */
  2128. while(--in) {
  2129. /* scan for the left parenthesis and break there */
  2130. if('(' == *bytes)
  2131. break;
  2132. /* skip only digits */
  2133. if(!ISDIGIT(*bytes)) {
  2134. bytes=NULL;
  2135. break;
  2136. }
  2137. /* one more estep backwards */
  2138. bytes--;
  2139. }
  2140. /* if we have nothing but digits: */
  2141. if(bytes++) {
  2142. /* get the number! */
  2143. size = curlx_strtoofft(bytes, NULL, 0);
  2144. }
  2145. }
  2146. }
  2147. else if(ftp->downloadsize > -1)
  2148. size = ftp->downloadsize;
  2149. if(size > data->req.maxdownload && data->req.maxdownload > 0)
  2150. size = data->req.size = data->req.maxdownload;
  2151. else if((instate != FTP_LIST) && (data->set.prefer_ascii))
  2152. size = -1; /* kludge for servers that understate ASCII mode file size */
  2153. infof(data, "Maxdownload = %" CURL_FORMAT_CURL_OFF_T "\n",
  2154. data->req.maxdownload);
  2155. if(instate != FTP_LIST)
  2156. infof(data, "Getting file with size: %" CURL_FORMAT_CURL_OFF_T "\n",
  2157. size);
  2158. /* FTP download: */
  2159. conn->proto.ftpc.state_saved = instate;
  2160. conn->proto.ftpc.retr_size_saved = size;
  2161. if(data->set.ftp_use_port) {
  2162. bool connected;
  2163. result = AllowServerConnect(conn, &connected);
  2164. if(result)
  2165. return result;
  2166. if(!connected) {
  2167. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2168. infof(data, "Data conn was not available immediately\n");
  2169. state(conn, FTP_STOP);
  2170. ftpc->wait_data_conn = TRUE;
  2171. }
  2172. }
  2173. else
  2174. return InitiateTransfer(conn);
  2175. }
  2176. else {
  2177. if((instate == FTP_LIST) && (ftpcode == 450)) {
  2178. /* simply no matching files in the dir listing */
  2179. ftp->transfer = FTPTRANSFER_NONE; /* don't download anything */
  2180. state(conn, FTP_STOP); /* this phase is over */
  2181. }
  2182. else {
  2183. failf(data, "RETR response: %03d", ftpcode);
  2184. return instate == FTP_RETR && ftpcode == 550?
  2185. CURLE_REMOTE_FILE_NOT_FOUND:
  2186. CURLE_FTP_COULDNT_RETR_FILE;
  2187. }
  2188. }
  2189. return result;
  2190. }
  2191. /* after USER, PASS and ACCT */
  2192. static CURLcode ftp_state_loggedin(struct connectdata *conn)
  2193. {
  2194. CURLcode result = CURLE_OK;
  2195. if(conn->ssl[FIRSTSOCKET].use) {
  2196. /* PBSZ = PROTECTION BUFFER SIZE.
  2197. The 'draft-murray-auth-ftp-ssl' (draft 12, page 7) says:
  2198. Specifically, the PROT command MUST be preceded by a PBSZ
  2199. command and a PBSZ command MUST be preceded by a successful
  2200. security data exchange (the TLS negotiation in this case)
  2201. ... (and on page 8):
  2202. Thus the PBSZ command must still be issued, but must have a
  2203. parameter of '0' to indicate that no buffering is taking place
  2204. and the data connection should not be encapsulated.
  2205. */
  2206. PPSENDF(&conn->proto.ftpc.pp, "PBSZ %d", 0);
  2207. state(conn, FTP_PBSZ);
  2208. }
  2209. else {
  2210. result = ftp_state_pwd(conn);
  2211. }
  2212. return result;
  2213. }
  2214. /* for USER and PASS responses */
  2215. static CURLcode ftp_state_user_resp(struct connectdata *conn,
  2216. int ftpcode,
  2217. ftpstate instate)
  2218. {
  2219. CURLcode result = CURLE_OK;
  2220. struct Curl_easy *data = conn->data;
  2221. struct FTP *ftp = data->req.protop;
  2222. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2223. (void)instate; /* no use for this yet */
  2224. /* some need password anyway, and others just return 2xx ignored */
  2225. if((ftpcode == 331) && (ftpc->state == FTP_USER)) {
  2226. /* 331 Password required for ...
  2227. (the server requires to send the user's password too) */
  2228. PPSENDF(&ftpc->pp, "PASS %s", ftp->passwd?ftp->passwd:"");
  2229. state(conn, FTP_PASS);
  2230. }
  2231. else if(ftpcode/100 == 2) {
  2232. /* 230 User ... logged in.
  2233. (the user logged in with or without password) */
  2234. result = ftp_state_loggedin(conn);
  2235. }
  2236. else if(ftpcode == 332) {
  2237. if(data->set.str[STRING_FTP_ACCOUNT]) {
  2238. PPSENDF(&ftpc->pp, "ACCT %s", data->set.str[STRING_FTP_ACCOUNT]);
  2239. state(conn, FTP_ACCT);
  2240. }
  2241. else {
  2242. failf(data, "ACCT requested but none available");
  2243. result = CURLE_LOGIN_DENIED;
  2244. }
  2245. }
  2246. else {
  2247. /* All other response codes, like:
  2248. 530 User ... access denied
  2249. (the server denies to log the specified user) */
  2250. if(conn->data->set.str[STRING_FTP_ALTERNATIVE_TO_USER] &&
  2251. !conn->data->state.ftp_trying_alternative) {
  2252. /* Ok, USER failed. Let's try the supplied command. */
  2253. PPSENDF(&conn->proto.ftpc.pp, "%s",
  2254. conn->data->set.str[STRING_FTP_ALTERNATIVE_TO_USER]);
  2255. conn->data->state.ftp_trying_alternative = TRUE;
  2256. state(conn, FTP_USER);
  2257. result = CURLE_OK;
  2258. }
  2259. else {
  2260. failf(data, "Access denied: %03d", ftpcode);
  2261. result = CURLE_LOGIN_DENIED;
  2262. }
  2263. }
  2264. return result;
  2265. }
  2266. /* for ACCT response */
  2267. static CURLcode ftp_state_acct_resp(struct connectdata *conn,
  2268. int ftpcode)
  2269. {
  2270. CURLcode result = CURLE_OK;
  2271. struct Curl_easy *data = conn->data;
  2272. if(ftpcode != 230) {
  2273. failf(data, "ACCT rejected by server: %03d", ftpcode);
  2274. result = CURLE_FTP_WEIRD_PASS_REPLY; /* FIX */
  2275. }
  2276. else
  2277. result = ftp_state_loggedin(conn);
  2278. return result;
  2279. }
  2280. static CURLcode ftp_statemach_act(struct connectdata *conn)
  2281. {
  2282. CURLcode result;
  2283. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  2284. struct Curl_easy *data=conn->data;
  2285. int ftpcode;
  2286. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2287. struct pingpong *pp = &ftpc->pp;
  2288. static const char ftpauth[][4] = { "SSL", "TLS" };
  2289. size_t nread = 0;
  2290. if(pp->sendleft)
  2291. return Curl_pp_flushsend(pp);
  2292. result = ftp_readresp(sock, pp, &ftpcode, &nread);
  2293. if(result)
  2294. return result;
  2295. if(ftpcode) {
  2296. /* we have now received a full FTP server response */
  2297. switch(ftpc->state) {
  2298. case FTP_WAIT220:
  2299. if(ftpcode == 230)
  2300. /* 230 User logged in - already! */
  2301. return ftp_state_user_resp(conn, ftpcode, ftpc->state);
  2302. else if(ftpcode != 220) {
  2303. failf(data, "Got a %03d ftp-server response when 220 was expected",
  2304. ftpcode);
  2305. return CURLE_WEIRD_SERVER_REPLY;
  2306. }
  2307. /* We have received a 220 response fine, now we proceed. */
  2308. #ifdef HAVE_GSSAPI
  2309. if(data->set.krb) {
  2310. /* If not anonymous login, try a secure login. Note that this
  2311. procedure is still BLOCKING. */
  2312. Curl_sec_request_prot(conn, "private");
  2313. /* We set private first as default, in case the line below fails to
  2314. set a valid level */
  2315. Curl_sec_request_prot(conn, data->set.str[STRING_KRB_LEVEL]);
  2316. if(Curl_sec_login(conn))
  2317. infof(data, "Logging in with password in cleartext!\n");
  2318. else
  2319. infof(data, "Authentication successful\n");
  2320. }
  2321. #endif
  2322. if(data->set.use_ssl &&
  2323. (!conn->ssl[FIRSTSOCKET].use ||
  2324. (conn->bits.proxy_ssl_connected[FIRSTSOCKET] &&
  2325. !conn->proxy_ssl[FIRSTSOCKET].use))) {
  2326. /* We don't have a SSL/TLS connection yet, but FTPS is
  2327. requested. Try a FTPS connection now */
  2328. ftpc->count3=0;
  2329. switch(data->set.ftpsslauth) {
  2330. case CURLFTPAUTH_DEFAULT:
  2331. case CURLFTPAUTH_SSL:
  2332. ftpc->count2 = 1; /* add one to get next */
  2333. ftpc->count1 = 0;
  2334. break;
  2335. case CURLFTPAUTH_TLS:
  2336. ftpc->count2 = -1; /* subtract one to get next */
  2337. ftpc->count1 = 1;
  2338. break;
  2339. default:
  2340. failf(data, "unsupported parameter to CURLOPT_FTPSSLAUTH: %d",
  2341. (int)data->set.ftpsslauth);
  2342. return CURLE_UNKNOWN_OPTION; /* we don't know what to do */
  2343. }
  2344. PPSENDF(&ftpc->pp, "AUTH %s", ftpauth[ftpc->count1]);
  2345. state(conn, FTP_AUTH);
  2346. }
  2347. else {
  2348. result = ftp_state_user(conn);
  2349. if(result)
  2350. return result;
  2351. }
  2352. break;
  2353. case FTP_AUTH:
  2354. /* we have gotten the response to a previous AUTH command */
  2355. /* RFC2228 (page 5) says:
  2356. *
  2357. * If the server is willing to accept the named security mechanism,
  2358. * and does not require any security data, it must respond with
  2359. * reply code 234/334.
  2360. */
  2361. if((ftpcode == 234) || (ftpcode == 334)) {
  2362. /* Curl_ssl_connect is BLOCKING */
  2363. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  2364. if(!result) {
  2365. conn->bits.ftp_use_data_ssl = FALSE; /* clear-text data */
  2366. result = ftp_state_user(conn);
  2367. }
  2368. }
  2369. else if(ftpc->count3 < 1) {
  2370. ftpc->count3++;
  2371. ftpc->count1 += ftpc->count2; /* get next attempt */
  2372. result = Curl_pp_sendf(&ftpc->pp, "AUTH %s", ftpauth[ftpc->count1]);
  2373. /* remain in this same state */
  2374. }
  2375. else {
  2376. if(data->set.use_ssl > CURLUSESSL_TRY)
  2377. /* we failed and CURLUSESSL_CONTROL or CURLUSESSL_ALL is set */
  2378. result = CURLE_USE_SSL_FAILED;
  2379. else
  2380. /* ignore the failure and continue */
  2381. result = ftp_state_user(conn);
  2382. }
  2383. if(result)
  2384. return result;
  2385. break;
  2386. case FTP_USER:
  2387. case FTP_PASS:
  2388. result = ftp_state_user_resp(conn, ftpcode, ftpc->state);
  2389. break;
  2390. case FTP_ACCT:
  2391. result = ftp_state_acct_resp(conn, ftpcode);
  2392. break;
  2393. case FTP_PBSZ:
  2394. PPSENDF(&ftpc->pp, "PROT %c",
  2395. data->set.use_ssl == CURLUSESSL_CONTROL ? 'C' : 'P');
  2396. state(conn, FTP_PROT);
  2397. break;
  2398. case FTP_PROT:
  2399. if(ftpcode/100 == 2)
  2400. /* We have enabled SSL for the data connection! */
  2401. conn->bits.ftp_use_data_ssl =
  2402. (data->set.use_ssl != CURLUSESSL_CONTROL) ? TRUE : FALSE;
  2403. /* FTP servers typically responds with 500 if they decide to reject
  2404. our 'P' request */
  2405. else if(data->set.use_ssl > CURLUSESSL_CONTROL)
  2406. /* we failed and bails out */
  2407. return CURLE_USE_SSL_FAILED;
  2408. if(data->set.ftp_ccc) {
  2409. /* CCC - Clear Command Channel
  2410. */
  2411. PPSENDF(&ftpc->pp, "%s", "CCC");
  2412. state(conn, FTP_CCC);
  2413. }
  2414. else {
  2415. result = ftp_state_pwd(conn);
  2416. if(result)
  2417. return result;
  2418. }
  2419. break;
  2420. case FTP_CCC:
  2421. if(ftpcode < 500) {
  2422. /* First shut down the SSL layer (note: this call will block) */
  2423. result = Curl_ssl_shutdown(conn, FIRSTSOCKET);
  2424. if(result) {
  2425. failf(conn->data, "Failed to clear the command channel (CCC)");
  2426. return result;
  2427. }
  2428. }
  2429. /* Then continue as normal */
  2430. result = ftp_state_pwd(conn);
  2431. if(result)
  2432. return result;
  2433. break;
  2434. case FTP_PWD:
  2435. if(ftpcode == 257) {
  2436. char *ptr=&data->state.buffer[4]; /* start on the first letter */
  2437. const size_t buf_size = CURL_BUFSIZE(data->set.buffer_size);
  2438. char *dir;
  2439. char *store;
  2440. dir = malloc(nread + 1);
  2441. if(!dir)
  2442. return CURLE_OUT_OF_MEMORY;
  2443. /* Reply format is like
  2444. 257<space>[rubbish]"<directory-name>"<space><commentary> and the
  2445. RFC959 says
  2446. The directory name can contain any character; embedded
  2447. double-quotes should be escaped by double-quotes (the
  2448. "quote-doubling" convention).
  2449. */
  2450. /* scan for the first double-quote for non-standard responses */
  2451. while(ptr < &data->state.buffer[buf_size]
  2452. && *ptr != '\n' && *ptr != '\0' && *ptr != '"')
  2453. ptr++;
  2454. if('\"' == *ptr) {
  2455. /* it started good */
  2456. ptr++;
  2457. for(store = dir; *ptr;) {
  2458. if('\"' == *ptr) {
  2459. if('\"' == ptr[1]) {
  2460. /* "quote-doubling" */
  2461. *store = ptr[1];
  2462. ptr++;
  2463. }
  2464. else {
  2465. /* end of path */
  2466. *store = '\0'; /* zero terminate */
  2467. break; /* get out of this loop */
  2468. }
  2469. }
  2470. else
  2471. *store = *ptr;
  2472. store++;
  2473. ptr++;
  2474. }
  2475. /* If the path name does not look like an absolute path (i.e.: it
  2476. does not start with a '/'), we probably need some server-dependent
  2477. adjustments. For example, this is the case when connecting to
  2478. an OS400 FTP server: this server supports two name syntaxes,
  2479. the default one being incompatible with standard pathes. In
  2480. addition, this server switches automatically to the regular path
  2481. syntax when one is encountered in a command: this results in
  2482. having an entrypath in the wrong syntax when later used in CWD.
  2483. The method used here is to check the server OS: we do it only
  2484. if the path name looks strange to minimize overhead on other
  2485. systems. */
  2486. if(!ftpc->server_os && dir[0] != '/') {
  2487. result = Curl_pp_sendf(&ftpc->pp, "%s", "SYST");
  2488. if(result) {
  2489. free(dir);
  2490. return result;
  2491. }
  2492. Curl_safefree(ftpc->entrypath);
  2493. ftpc->entrypath = dir; /* remember this */
  2494. infof(data, "Entry path is '%s'\n", ftpc->entrypath);
  2495. /* also save it where getinfo can access it: */
  2496. data->state.most_recent_ftp_entrypath = ftpc->entrypath;
  2497. state(conn, FTP_SYST);
  2498. break;
  2499. }
  2500. Curl_safefree(ftpc->entrypath);
  2501. ftpc->entrypath = dir; /* remember this */
  2502. infof(data, "Entry path is '%s'\n", ftpc->entrypath);
  2503. /* also save it where getinfo can access it: */
  2504. data->state.most_recent_ftp_entrypath = ftpc->entrypath;
  2505. }
  2506. else {
  2507. /* couldn't get the path */
  2508. free(dir);
  2509. infof(data, "Failed to figure out path\n");
  2510. }
  2511. }
  2512. state(conn, FTP_STOP); /* we are done with the CONNECT phase! */
  2513. DEBUGF(infof(data, "protocol connect phase DONE\n"));
  2514. break;
  2515. case FTP_SYST:
  2516. if(ftpcode == 215) {
  2517. char *ptr=&data->state.buffer[4]; /* start on the first letter */
  2518. char *os;
  2519. char *store;
  2520. os = malloc(nread + 1);
  2521. if(!os)
  2522. return CURLE_OUT_OF_MEMORY;
  2523. /* Reply format is like
  2524. 215<space><OS-name><space><commentary>
  2525. */
  2526. while(*ptr == ' ')
  2527. ptr++;
  2528. for(store = os; *ptr && *ptr != ' ';)
  2529. *store++ = *ptr++;
  2530. *store = '\0'; /* zero terminate */
  2531. /* Check for special servers here. */
  2532. if(strcasecompare(os, "OS/400")) {
  2533. /* Force OS400 name format 1. */
  2534. result = Curl_pp_sendf(&ftpc->pp, "%s", "SITE NAMEFMT 1");
  2535. if(result) {
  2536. free(os);
  2537. return result;
  2538. }
  2539. /* remember target server OS */
  2540. Curl_safefree(ftpc->server_os);
  2541. ftpc->server_os = os;
  2542. state(conn, FTP_NAMEFMT);
  2543. break;
  2544. }
  2545. else {
  2546. /* Nothing special for the target server. */
  2547. /* remember target server OS */
  2548. Curl_safefree(ftpc->server_os);
  2549. ftpc->server_os = os;
  2550. }
  2551. }
  2552. else {
  2553. /* Cannot identify server OS. Continue anyway and cross fingers. */
  2554. }
  2555. state(conn, FTP_STOP); /* we are done with the CONNECT phase! */
  2556. DEBUGF(infof(data, "protocol connect phase DONE\n"));
  2557. break;
  2558. case FTP_NAMEFMT:
  2559. if(ftpcode == 250) {
  2560. /* Name format change successful: reload initial path. */
  2561. ftp_state_pwd(conn);
  2562. break;
  2563. }
  2564. state(conn, FTP_STOP); /* we are done with the CONNECT phase! */
  2565. DEBUGF(infof(data, "protocol connect phase DONE\n"));
  2566. break;
  2567. case FTP_QUOTE:
  2568. case FTP_POSTQUOTE:
  2569. case FTP_RETR_PREQUOTE:
  2570. case FTP_STOR_PREQUOTE:
  2571. if((ftpcode >= 400) && !ftpc->count2) {
  2572. /* failure response code, and not allowed to fail */
  2573. failf(conn->data, "QUOT command failed with %03d", ftpcode);
  2574. return CURLE_QUOTE_ERROR;
  2575. }
  2576. result = ftp_state_quote(conn, FALSE, ftpc->state);
  2577. if(result)
  2578. return result;
  2579. break;
  2580. case FTP_CWD:
  2581. if(ftpcode/100 != 2) {
  2582. /* failure to CWD there */
  2583. if(conn->data->set.ftp_create_missing_dirs &&
  2584. ftpc->count1 && !ftpc->count2) {
  2585. /* try making it */
  2586. ftpc->count2++; /* counter to prevent CWD-MKD loops */
  2587. PPSENDF(&ftpc->pp, "MKD %s", ftpc->dirs[ftpc->count1 - 1]);
  2588. state(conn, FTP_MKD);
  2589. }
  2590. else {
  2591. /* return failure */
  2592. failf(data, "Server denied you to change to the given directory");
  2593. ftpc->cwdfail = TRUE; /* don't remember this path as we failed
  2594. to enter it */
  2595. return CURLE_REMOTE_ACCESS_DENIED;
  2596. }
  2597. }
  2598. else {
  2599. /* success */
  2600. ftpc->count2=0;
  2601. if(++ftpc->count1 <= ftpc->dirdepth) {
  2602. /* send next CWD */
  2603. PPSENDF(&ftpc->pp, "CWD %s", ftpc->dirs[ftpc->count1 - 1]);
  2604. }
  2605. else {
  2606. result = ftp_state_mdtm(conn);
  2607. if(result)
  2608. return result;
  2609. }
  2610. }
  2611. break;
  2612. case FTP_MKD:
  2613. if((ftpcode/100 != 2) && !ftpc->count3--) {
  2614. /* failure to MKD the dir */
  2615. failf(data, "Failed to MKD dir: %03d", ftpcode);
  2616. return CURLE_REMOTE_ACCESS_DENIED;
  2617. }
  2618. state(conn, FTP_CWD);
  2619. /* send CWD */
  2620. PPSENDF(&ftpc->pp, "CWD %s", ftpc->dirs[ftpc->count1 - 1]);
  2621. break;
  2622. case FTP_MDTM:
  2623. result = ftp_state_mdtm_resp(conn, ftpcode);
  2624. break;
  2625. case FTP_TYPE:
  2626. case FTP_LIST_TYPE:
  2627. case FTP_RETR_TYPE:
  2628. case FTP_STOR_TYPE:
  2629. result = ftp_state_type_resp(conn, ftpcode, ftpc->state);
  2630. break;
  2631. case FTP_SIZE:
  2632. case FTP_RETR_SIZE:
  2633. case FTP_STOR_SIZE:
  2634. result = ftp_state_size_resp(conn, ftpcode, ftpc->state);
  2635. break;
  2636. case FTP_REST:
  2637. case FTP_RETR_REST:
  2638. result = ftp_state_rest_resp(conn, ftpcode, ftpc->state);
  2639. break;
  2640. case FTP_PRET:
  2641. if(ftpcode != 200) {
  2642. /* there only is this one standard OK return code. */
  2643. failf(data, "PRET command not accepted: %03d", ftpcode);
  2644. return CURLE_FTP_PRET_FAILED;
  2645. }
  2646. result = ftp_state_use_pasv(conn);
  2647. break;
  2648. case FTP_PASV:
  2649. result = ftp_state_pasv_resp(conn, ftpcode);
  2650. break;
  2651. case FTP_PORT:
  2652. result = ftp_state_port_resp(conn, ftpcode);
  2653. break;
  2654. case FTP_LIST:
  2655. case FTP_RETR:
  2656. result = ftp_state_get_resp(conn, ftpcode, ftpc->state);
  2657. break;
  2658. case FTP_STOR:
  2659. result = ftp_state_stor_resp(conn, ftpcode, ftpc->state);
  2660. break;
  2661. case FTP_QUIT:
  2662. /* fallthrough, just stop! */
  2663. default:
  2664. /* internal error */
  2665. state(conn, FTP_STOP);
  2666. break;
  2667. }
  2668. } /* if(ftpcode) */
  2669. return result;
  2670. }
  2671. /* called repeatedly until done from multi.c */
  2672. static CURLcode ftp_multi_statemach(struct connectdata *conn,
  2673. bool *done)
  2674. {
  2675. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2676. CURLcode result = Curl_pp_statemach(&ftpc->pp, FALSE);
  2677. /* Check for the state outside of the Curl_socket_check() return code checks
  2678. since at times we are in fact already in this state when this function
  2679. gets called. */
  2680. *done = (ftpc->state == FTP_STOP) ? TRUE : FALSE;
  2681. return result;
  2682. }
  2683. static CURLcode ftp_block_statemach(struct connectdata *conn)
  2684. {
  2685. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2686. struct pingpong *pp = &ftpc->pp;
  2687. CURLcode result = CURLE_OK;
  2688. while(ftpc->state != FTP_STOP) {
  2689. result = Curl_pp_statemach(pp, TRUE);
  2690. if(result)
  2691. break;
  2692. }
  2693. return result;
  2694. }
  2695. /*
  2696. * ftp_connect() should do everything that is to be considered a part of
  2697. * the connection phase.
  2698. *
  2699. * The variable 'done' points to will be TRUE if the protocol-layer connect
  2700. * phase is done when this function returns, or FALSE if not.
  2701. *
  2702. */
  2703. static CURLcode ftp_connect(struct connectdata *conn,
  2704. bool *done) /* see description above */
  2705. {
  2706. CURLcode result;
  2707. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2708. struct pingpong *pp = &ftpc->pp;
  2709. *done = FALSE; /* default to not done yet */
  2710. /* We always support persistent connections on ftp */
  2711. connkeep(conn, "FTP default");
  2712. pp->response_time = RESP_TIMEOUT; /* set default response time-out */
  2713. pp->statemach_act = ftp_statemach_act;
  2714. pp->endofresp = ftp_endofresp;
  2715. pp->conn = conn;
  2716. if(conn->handler->flags & PROTOPT_SSL) {
  2717. /* BLOCKING */
  2718. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  2719. if(result)
  2720. return result;
  2721. }
  2722. Curl_pp_init(pp); /* init the generic pingpong data */
  2723. /* When we connect, we start in the state where we await the 220
  2724. response */
  2725. state(conn, FTP_WAIT220);
  2726. result = ftp_multi_statemach(conn, done);
  2727. return result;
  2728. }
  2729. /***********************************************************************
  2730. *
  2731. * ftp_done()
  2732. *
  2733. * The DONE function. This does what needs to be done after a single DO has
  2734. * performed.
  2735. *
  2736. * Input argument is already checked for validity.
  2737. */
  2738. static CURLcode ftp_done(struct connectdata *conn, CURLcode status,
  2739. bool premature)
  2740. {
  2741. struct Curl_easy *data = conn->data;
  2742. struct FTP *ftp = data->req.protop;
  2743. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2744. struct pingpong *pp = &ftpc->pp;
  2745. ssize_t nread;
  2746. int ftpcode;
  2747. CURLcode result = CURLE_OK;
  2748. char *path = NULL;
  2749. const char *path_to_use = data->state.path;
  2750. if(!ftp)
  2751. return CURLE_OK;
  2752. switch(status) {
  2753. case CURLE_BAD_DOWNLOAD_RESUME:
  2754. case CURLE_FTP_WEIRD_PASV_REPLY:
  2755. case CURLE_FTP_PORT_FAILED:
  2756. case CURLE_FTP_ACCEPT_FAILED:
  2757. case CURLE_FTP_ACCEPT_TIMEOUT:
  2758. case CURLE_FTP_COULDNT_SET_TYPE:
  2759. case CURLE_FTP_COULDNT_RETR_FILE:
  2760. case CURLE_PARTIAL_FILE:
  2761. case CURLE_UPLOAD_FAILED:
  2762. case CURLE_REMOTE_ACCESS_DENIED:
  2763. case CURLE_FILESIZE_EXCEEDED:
  2764. case CURLE_REMOTE_FILE_NOT_FOUND:
  2765. case CURLE_WRITE_ERROR:
  2766. /* the connection stays alive fine even though this happened */
  2767. /* fall-through */
  2768. case CURLE_OK: /* doesn't affect the control connection's status */
  2769. if(!premature)
  2770. break;
  2771. /* until we cope better with prematurely ended requests, let them
  2772. * fallback as if in complete failure */
  2773. default: /* by default, an error means the control connection is
  2774. wedged and should not be used anymore */
  2775. ftpc->ctl_valid = FALSE;
  2776. ftpc->cwdfail = TRUE; /* set this TRUE to prevent us to remember the
  2777. current path, as this connection is going */
  2778. connclose(conn, "FTP ended with bad error code");
  2779. result = status; /* use the already set error code */
  2780. break;
  2781. }
  2782. /* now store a copy of the directory we are in */
  2783. free(ftpc->prevpath);
  2784. if(data->set.wildcardmatch) {
  2785. if(data->set.chunk_end && ftpc->file) {
  2786. data->set.chunk_end(data->wildcard.customptr);
  2787. }
  2788. ftpc->known_filesize = -1;
  2789. }
  2790. if(!result)
  2791. /* get the "raw" path */
  2792. result = Curl_urldecode(data, path_to_use, 0, &path, NULL, FALSE);
  2793. if(result) {
  2794. /* We can limp along anyway (and should try to since we may already be in
  2795. * the error path) */
  2796. ftpc->ctl_valid = FALSE; /* mark control connection as bad */
  2797. connclose(conn, "FTP: out of memory!"); /* mark for connection closure */
  2798. ftpc->prevpath = NULL; /* no path remembering */
  2799. }
  2800. else {
  2801. size_t flen = ftpc->file?strlen(ftpc->file):0; /* file is "raw" already */
  2802. size_t dlen = strlen(path)-flen;
  2803. if(!ftpc->cwdfail) {
  2804. if(dlen && (data->set.ftp_filemethod != FTPFILE_NOCWD)) {
  2805. ftpc->prevpath = path;
  2806. if(flen)
  2807. /* if 'path' is not the whole string */
  2808. ftpc->prevpath[dlen]=0; /* terminate */
  2809. }
  2810. else {
  2811. /* we never changed dir */
  2812. ftpc->prevpath=strdup("");
  2813. free(path);
  2814. }
  2815. if(ftpc->prevpath)
  2816. infof(data, "Remembering we are in dir \"%s\"\n", ftpc->prevpath);
  2817. }
  2818. else {
  2819. ftpc->prevpath = NULL; /* no path */
  2820. free(path);
  2821. }
  2822. }
  2823. /* free the dir tree and file parts */
  2824. freedirs(ftpc);
  2825. /* shut down the socket to inform the server we're done */
  2826. #ifdef _WIN32_WCE
  2827. shutdown(conn->sock[SECONDARYSOCKET], 2); /* SD_BOTH */
  2828. #endif
  2829. if(conn->sock[SECONDARYSOCKET] != CURL_SOCKET_BAD) {
  2830. if(!result && ftpc->dont_check && data->req.maxdownload > 0) {
  2831. /* partial download completed */
  2832. result = Curl_pp_sendf(pp, "%s", "ABOR");
  2833. if(result) {
  2834. failf(data, "Failure sending ABOR command: %s",
  2835. curl_easy_strerror(result));
  2836. ftpc->ctl_valid = FALSE; /* mark control connection as bad */
  2837. connclose(conn, "ABOR command failed"); /* connection closure */
  2838. }
  2839. }
  2840. if(conn->ssl[SECONDARYSOCKET].use) {
  2841. /* The secondary socket is using SSL so we must close down that part
  2842. first before we close the socket for real */
  2843. Curl_ssl_close(conn, SECONDARYSOCKET);
  2844. /* Note that we keep "use" set to TRUE since that (next) connection is
  2845. still requested to use SSL */
  2846. }
  2847. close_secondarysocket(conn);
  2848. }
  2849. if(!result && (ftp->transfer == FTPTRANSFER_BODY) && ftpc->ctl_valid &&
  2850. pp->pending_resp && !premature) {
  2851. /*
  2852. * Let's see what the server says about the transfer we just performed,
  2853. * but lower the timeout as sometimes this connection has died while the
  2854. * data has been transferred. This happens when doing through NATs etc that
  2855. * abandon old silent connections.
  2856. */
  2857. long old_time = pp->response_time;
  2858. pp->response_time = 60*1000; /* give it only a minute for now */
  2859. pp->response = Curl_tvnow(); /* timeout relative now */
  2860. result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
  2861. pp->response_time = old_time; /* set this back to previous value */
  2862. if(!nread && (CURLE_OPERATION_TIMEDOUT == result)) {
  2863. failf(data, "control connection looks dead");
  2864. ftpc->ctl_valid = FALSE; /* mark control connection as bad */
  2865. connclose(conn, "Timeout or similar in FTP DONE operation"); /* close */
  2866. }
  2867. if(result)
  2868. return result;
  2869. if(ftpc->dont_check && data->req.maxdownload > 0) {
  2870. /* we have just sent ABOR and there is no reliable way to check if it was
  2871. * successful or not; we have to close the connection now */
  2872. infof(data, "partial download completed, closing connection\n");
  2873. connclose(conn, "Partial download with no ability to check");
  2874. return result;
  2875. }
  2876. if(!ftpc->dont_check) {
  2877. /* 226 Transfer complete, 250 Requested file action okay, completed. */
  2878. if((ftpcode != 226) && (ftpcode != 250)) {
  2879. failf(data, "server did not report OK, got %d", ftpcode);
  2880. result = CURLE_PARTIAL_FILE;
  2881. }
  2882. }
  2883. }
  2884. if(result || premature)
  2885. /* the response code from the transfer showed an error already so no
  2886. use checking further */
  2887. ;
  2888. else if(data->set.upload) {
  2889. if((-1 != data->state.infilesize) &&
  2890. (data->state.infilesize != *ftp->bytecountp) &&
  2891. !data->set.crlf &&
  2892. (ftp->transfer == FTPTRANSFER_BODY)) {
  2893. failf(data, "Uploaded unaligned file size (%" CURL_FORMAT_CURL_OFF_T
  2894. " out of %" CURL_FORMAT_CURL_OFF_T " bytes)",
  2895. *ftp->bytecountp, data->state.infilesize);
  2896. result = CURLE_PARTIAL_FILE;
  2897. }
  2898. }
  2899. else {
  2900. if((-1 != data->req.size) &&
  2901. (data->req.size != *ftp->bytecountp) &&
  2902. #ifdef CURL_DO_LINEEND_CONV
  2903. /* Most FTP servers don't adjust their file SIZE response for CRLFs, so
  2904. * we'll check to see if the discrepancy can be explained by the number
  2905. * of CRLFs we've changed to LFs.
  2906. */
  2907. ((data->req.size + data->state.crlf_conversions) !=
  2908. *ftp->bytecountp) &&
  2909. #endif /* CURL_DO_LINEEND_CONV */
  2910. (data->req.maxdownload != *ftp->bytecountp)) {
  2911. failf(data, "Received only partial file: %" CURL_FORMAT_CURL_OFF_T
  2912. " bytes", *ftp->bytecountp);
  2913. result = CURLE_PARTIAL_FILE;
  2914. }
  2915. else if(!ftpc->dont_check &&
  2916. !*ftp->bytecountp &&
  2917. (data->req.size>0)) {
  2918. failf(data, "No data was received!");
  2919. result = CURLE_FTP_COULDNT_RETR_FILE;
  2920. }
  2921. }
  2922. /* clear these for next connection */
  2923. ftp->transfer = FTPTRANSFER_BODY;
  2924. ftpc->dont_check = FALSE;
  2925. /* Send any post-transfer QUOTE strings? */
  2926. if(!status && !result && !premature && data->set.postquote)
  2927. result = ftp_sendquote(conn, data->set.postquote);
  2928. return result;
  2929. }
  2930. /***********************************************************************
  2931. *
  2932. * ftp_sendquote()
  2933. *
  2934. * Where a 'quote' means a list of custom commands to send to the server.
  2935. * The quote list is passed as an argument.
  2936. *
  2937. * BLOCKING
  2938. */
  2939. static
  2940. CURLcode ftp_sendquote(struct connectdata *conn, struct curl_slist *quote)
  2941. {
  2942. struct curl_slist *item;
  2943. ssize_t nread;
  2944. int ftpcode;
  2945. CURLcode result;
  2946. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2947. struct pingpong *pp = &ftpc->pp;
  2948. item = quote;
  2949. while(item) {
  2950. if(item->data) {
  2951. char *cmd = item->data;
  2952. bool acceptfail = FALSE;
  2953. /* if a command starts with an asterisk, which a legal FTP command never
  2954. can, the command will be allowed to fail without it causing any
  2955. aborts or cancels etc. It will cause libcurl to act as if the command
  2956. is successful, whatever the server reponds. */
  2957. if(cmd[0] == '*') {
  2958. cmd++;
  2959. acceptfail = TRUE;
  2960. }
  2961. PPSENDF(&conn->proto.ftpc.pp, "%s", cmd);
  2962. pp->response = Curl_tvnow(); /* timeout relative now */
  2963. result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
  2964. if(result)
  2965. return result;
  2966. if(!acceptfail && (ftpcode >= 400)) {
  2967. failf(conn->data, "QUOT string not accepted: %s", cmd);
  2968. return CURLE_QUOTE_ERROR;
  2969. }
  2970. }
  2971. item = item->next;
  2972. }
  2973. return CURLE_OK;
  2974. }
  2975. /***********************************************************************
  2976. *
  2977. * ftp_need_type()
  2978. *
  2979. * Returns TRUE if we in the current situation should send TYPE
  2980. */
  2981. static int ftp_need_type(struct connectdata *conn,
  2982. bool ascii_wanted)
  2983. {
  2984. return conn->proto.ftpc.transfertype != (ascii_wanted?'A':'I');
  2985. }
  2986. /***********************************************************************
  2987. *
  2988. * ftp_nb_type()
  2989. *
  2990. * Set TYPE. We only deal with ASCII or BINARY so this function
  2991. * sets one of them.
  2992. * If the transfer type is not sent, simulate on OK response in newstate
  2993. */
  2994. static CURLcode ftp_nb_type(struct connectdata *conn,
  2995. bool ascii, ftpstate newstate)
  2996. {
  2997. struct ftp_conn *ftpc = &conn->proto.ftpc;
  2998. CURLcode result;
  2999. char want = (char)(ascii?'A':'I');
  3000. if(ftpc->transfertype == want) {
  3001. state(conn, newstate);
  3002. return ftp_state_type_resp(conn, 200, newstate);
  3003. }
  3004. PPSENDF(&ftpc->pp, "TYPE %c", want);
  3005. state(conn, newstate);
  3006. /* keep track of our current transfer type */
  3007. ftpc->transfertype = want;
  3008. return CURLE_OK;
  3009. }
  3010. /***************************************************************************
  3011. *
  3012. * ftp_pasv_verbose()
  3013. *
  3014. * This function only outputs some informationals about this second connection
  3015. * when we've issued a PASV command before and thus we have connected to a
  3016. * possibly new IP address.
  3017. *
  3018. */
  3019. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  3020. static void
  3021. ftp_pasv_verbose(struct connectdata *conn,
  3022. Curl_addrinfo *ai,
  3023. char *newhost, /* ascii version */
  3024. int port)
  3025. {
  3026. char buf[256];
  3027. Curl_printable_address(ai, buf, sizeof(buf));
  3028. infof(conn->data, "Connecting to %s (%s) port %d\n", newhost, buf, port);
  3029. }
  3030. #endif
  3031. /*
  3032. Check if this is a range download, and if so, set the internal variables
  3033. properly.
  3034. */
  3035. static CURLcode ftp_range(struct connectdata *conn)
  3036. {
  3037. curl_off_t from, to;
  3038. char *ptr;
  3039. char *ptr2;
  3040. struct Curl_easy *data = conn->data;
  3041. struct ftp_conn *ftpc = &conn->proto.ftpc;
  3042. if(data->state.use_range && data->state.range) {
  3043. from=curlx_strtoofft(data->state.range, &ptr, 0);
  3044. while(*ptr && (ISSPACE(*ptr) || (*ptr=='-')))
  3045. ptr++;
  3046. to=curlx_strtoofft(ptr, &ptr2, 0);
  3047. if(ptr == ptr2) {
  3048. /* we didn't get any digit */
  3049. to=-1;
  3050. }
  3051. if((-1 == to) && (from>=0)) {
  3052. /* X - */
  3053. data->state.resume_from = from;
  3054. DEBUGF(infof(conn->data, "FTP RANGE %" CURL_FORMAT_CURL_OFF_T
  3055. " to end of file\n", from));
  3056. }
  3057. else if(from < 0) {
  3058. /* -Y */
  3059. data->req.maxdownload = -from;
  3060. data->state.resume_from = from;
  3061. DEBUGF(infof(conn->data, "FTP RANGE the last %" CURL_FORMAT_CURL_OFF_T
  3062. " bytes\n", -from));
  3063. }
  3064. else {
  3065. /* X-Y */
  3066. data->req.maxdownload = (to-from)+1; /* include last byte */
  3067. data->state.resume_from = from;
  3068. DEBUGF(infof(conn->data, "FTP RANGE from %" CURL_FORMAT_CURL_OFF_T
  3069. " getting %" CURL_FORMAT_CURL_OFF_T " bytes\n",
  3070. from, data->req.maxdownload));
  3071. }
  3072. DEBUGF(infof(conn->data, "range-download from %" CURL_FORMAT_CURL_OFF_T
  3073. " to %" CURL_FORMAT_CURL_OFF_T ", totally %"
  3074. CURL_FORMAT_CURL_OFF_T " bytes\n",
  3075. from, to, data->req.maxdownload));
  3076. ftpc->dont_check = TRUE; /* dont check for successful transfer */
  3077. }
  3078. else
  3079. data->req.maxdownload = -1;
  3080. return CURLE_OK;
  3081. }
  3082. /*
  3083. * ftp_do_more()
  3084. *
  3085. * This function shall be called when the second FTP (data) connection is
  3086. * connected.
  3087. *
  3088. * 'complete' can return 0 for incomplete, 1 for done and -1 for go back
  3089. * (which basically is only for when PASV is being sent to retry a failed
  3090. * EPSV).
  3091. */
  3092. static CURLcode ftp_do_more(struct connectdata *conn, int *completep)
  3093. {
  3094. struct Curl_easy *data=conn->data;
  3095. struct ftp_conn *ftpc = &conn->proto.ftpc;
  3096. CURLcode result = CURLE_OK;
  3097. bool connected = FALSE;
  3098. bool complete = FALSE;
  3099. /* the ftp struct is inited in ftp_connect() */
  3100. struct FTP *ftp = data->req.protop;
  3101. /* if the second connection isn't done yet, wait for it */
  3102. if(!conn->bits.tcpconnect[SECONDARYSOCKET]) {
  3103. if(conn->tunnel_state[SECONDARYSOCKET] == TUNNEL_CONNECT) {
  3104. /* As we're in TUNNEL_CONNECT state now, we know the proxy name and port
  3105. aren't used so we blank their arguments. TODO: make this nicer */
  3106. result = Curl_proxyCONNECT(conn, SECONDARYSOCKET, NULL, 0, FALSE);
  3107. return result;
  3108. }
  3109. result = Curl_is_connected(conn, SECONDARYSOCKET, &connected);
  3110. /* Ready to do more? */
  3111. if(connected) {
  3112. DEBUGF(infof(data, "DO-MORE connected phase starts\n"));
  3113. }
  3114. else {
  3115. if(result && (ftpc->count1 == 0)) {
  3116. *completep = -1; /* go back to DOING please */
  3117. /* this is a EPSV connect failing, try PASV instead */
  3118. return ftp_epsv_disable(conn);
  3119. }
  3120. return result;
  3121. }
  3122. }
  3123. result = Curl_proxy_connect(conn, SECONDARYSOCKET);
  3124. if(result)
  3125. return result;
  3126. if(CONNECT_SECONDARYSOCKET_PROXY_SSL())
  3127. return result;
  3128. if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
  3129. conn->tunnel_state[SECONDARYSOCKET] != TUNNEL_COMPLETE)
  3130. return result;
  3131. if(ftpc->state) {
  3132. /* already in a state so skip the intial commands.
  3133. They are only done to kickstart the do_more state */
  3134. result = ftp_multi_statemach(conn, &complete);
  3135. *completep = (int)complete;
  3136. /* if we got an error or if we don't wait for a data connection return
  3137. immediately */
  3138. if(result || (ftpc->wait_data_conn != TRUE))
  3139. return result;
  3140. if(ftpc->wait_data_conn)
  3141. /* if we reach the end of the FTP state machine here, *complete will be
  3142. TRUE but so is ftpc->wait_data_conn, which says we need to wait for
  3143. the data connection and therefore we're not actually complete */
  3144. *completep = 0;
  3145. }
  3146. if(ftp->transfer <= FTPTRANSFER_INFO) {
  3147. /* a transfer is about to take place, or if not a file name was given
  3148. so we'll do a SIZE on it later and then we need the right TYPE first */
  3149. if(ftpc->wait_data_conn == TRUE) {
  3150. bool serv_conned;
  3151. result = ReceivedServerConnect(conn, &serv_conned);
  3152. if(result)
  3153. return result; /* Failed to accept data connection */
  3154. if(serv_conned) {
  3155. /* It looks data connection is established */
  3156. result = AcceptServerConnect(conn);
  3157. ftpc->wait_data_conn = FALSE;
  3158. if(!result)
  3159. result = InitiateTransfer(conn);
  3160. if(result)
  3161. return result;
  3162. *completep = 1; /* this state is now complete when the server has
  3163. connected back to us */
  3164. }
  3165. }
  3166. else if(data->set.upload) {
  3167. result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_STOR_TYPE);
  3168. if(result)
  3169. return result;
  3170. result = ftp_multi_statemach(conn, &complete);
  3171. if(ftpc->wait_data_conn)
  3172. /* if we reach the end of the FTP state machine here, *complete will be
  3173. TRUE but so is ftpc->wait_data_conn, which says we need to wait for
  3174. the data connection and therefore we're not actually complete */
  3175. *completep = 0;
  3176. else
  3177. *completep = (int)complete;
  3178. }
  3179. else {
  3180. /* download */
  3181. ftp->downloadsize = -1; /* unknown as of yet */
  3182. result = ftp_range(conn);
  3183. if(result)
  3184. ;
  3185. else if(data->set.ftp_list_only || !ftpc->file) {
  3186. /* The specified path ends with a slash, and therefore we think this
  3187. is a directory that is requested, use LIST. But before that we
  3188. need to set ASCII transfer mode. */
  3189. /* But only if a body transfer was requested. */
  3190. if(ftp->transfer == FTPTRANSFER_BODY) {
  3191. result = ftp_nb_type(conn, TRUE, FTP_LIST_TYPE);
  3192. if(result)
  3193. return result;
  3194. }
  3195. /* otherwise just fall through */
  3196. }
  3197. else {
  3198. result = ftp_nb_type(conn, data->set.prefer_ascii, FTP_RETR_TYPE);
  3199. if(result)
  3200. return result;
  3201. }
  3202. result = ftp_multi_statemach(conn, &complete);
  3203. *completep = (int)complete;
  3204. }
  3205. return result;
  3206. }
  3207. if(!result && (ftp->transfer != FTPTRANSFER_BODY))
  3208. /* no data to transfer. FIX: it feels like a kludge to have this here
  3209. too! */
  3210. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  3211. if(!ftpc->wait_data_conn) {
  3212. /* no waiting for the data connection so this is now complete */
  3213. *completep = 1;
  3214. DEBUGF(infof(data, "DO-MORE phase ends with %d\n", (int)result));
  3215. }
  3216. return result;
  3217. }
  3218. /***********************************************************************
  3219. *
  3220. * ftp_perform()
  3221. *
  3222. * This is the actual DO function for FTP. Get a file/directory according to
  3223. * the options previously setup.
  3224. */
  3225. static
  3226. CURLcode ftp_perform(struct connectdata *conn,
  3227. bool *connected, /* connect status after PASV / PORT */
  3228. bool *dophase_done)
  3229. {
  3230. /* this is FTP and no proxy */
  3231. CURLcode result=CURLE_OK;
  3232. DEBUGF(infof(conn->data, "DO phase starts\n"));
  3233. if(conn->data->set.opt_no_body) {
  3234. /* requested no body means no transfer... */
  3235. struct FTP *ftp = conn->data->req.protop;
  3236. ftp->transfer = FTPTRANSFER_INFO;
  3237. }
  3238. *dophase_done = FALSE; /* not done yet */
  3239. /* start the first command in the DO phase */
  3240. result = ftp_state_quote(conn, TRUE, FTP_QUOTE);
  3241. if(result)
  3242. return result;
  3243. /* run the state-machine */
  3244. result = ftp_multi_statemach(conn, dophase_done);
  3245. *connected = conn->bits.tcpconnect[SECONDARYSOCKET];
  3246. infof(conn->data, "ftp_perform ends with SECONDARY: %d\n", *connected);
  3247. if(*dophase_done)
  3248. DEBUGF(infof(conn->data, "DO phase is complete1\n"));
  3249. return result;
  3250. }
  3251. static void wc_data_dtor(void *ptr)
  3252. {
  3253. struct ftp_wc_tmpdata *tmp = ptr;
  3254. if(tmp)
  3255. Curl_ftp_parselist_data_free(&tmp->parser);
  3256. free(tmp);
  3257. }
  3258. static CURLcode init_wc_data(struct connectdata *conn)
  3259. {
  3260. char *last_slash;
  3261. char *path = conn->data->state.path;
  3262. struct WildcardData *wildcard = &(conn->data->wildcard);
  3263. CURLcode result = CURLE_OK;
  3264. struct ftp_wc_tmpdata *ftp_tmp;
  3265. last_slash = strrchr(conn->data->state.path, '/');
  3266. if(last_slash) {
  3267. last_slash++;
  3268. if(last_slash[0] == '\0') {
  3269. wildcard->state = CURLWC_CLEAN;
  3270. result = ftp_parse_url_path(conn);
  3271. return result;
  3272. }
  3273. else {
  3274. wildcard->pattern = strdup(last_slash);
  3275. if(!wildcard->pattern)
  3276. return CURLE_OUT_OF_MEMORY;
  3277. last_slash[0] = '\0'; /* cut file from path */
  3278. }
  3279. }
  3280. else { /* there is only 'wildcard pattern' or nothing */
  3281. if(path[0]) {
  3282. wildcard->pattern = strdup(path);
  3283. if(!wildcard->pattern)
  3284. return CURLE_OUT_OF_MEMORY;
  3285. path[0] = '\0';
  3286. }
  3287. else { /* only list */
  3288. wildcard->state = CURLWC_CLEAN;
  3289. result = ftp_parse_url_path(conn);
  3290. return result;
  3291. }
  3292. }
  3293. /* program continues only if URL is not ending with slash, allocate needed
  3294. resources for wildcard transfer */
  3295. /* allocate ftp protocol specific temporary wildcard data */
  3296. ftp_tmp = calloc(1, sizeof(struct ftp_wc_tmpdata));
  3297. if(!ftp_tmp) {
  3298. Curl_safefree(wildcard->pattern);
  3299. return CURLE_OUT_OF_MEMORY;
  3300. }
  3301. /* INITIALIZE parselist structure */
  3302. ftp_tmp->parser = Curl_ftp_parselist_data_alloc();
  3303. if(!ftp_tmp->parser) {
  3304. Curl_safefree(wildcard->pattern);
  3305. free(ftp_tmp);
  3306. return CURLE_OUT_OF_MEMORY;
  3307. }
  3308. wildcard->tmp = ftp_tmp; /* put it to the WildcardData tmp pointer */
  3309. wildcard->tmp_dtor = wc_data_dtor;
  3310. /* wildcard does not support NOCWD option (assert it?) */
  3311. if(conn->data->set.ftp_filemethod == FTPFILE_NOCWD)
  3312. conn->data->set.ftp_filemethod = FTPFILE_MULTICWD;
  3313. /* try to parse ftp url */
  3314. result = ftp_parse_url_path(conn);
  3315. if(result) {
  3316. Curl_safefree(wildcard->pattern);
  3317. wildcard->tmp_dtor(wildcard->tmp);
  3318. wildcard->tmp_dtor = ZERO_NULL;
  3319. wildcard->tmp = NULL;
  3320. return result;
  3321. }
  3322. wildcard->path = strdup(conn->data->state.path);
  3323. if(!wildcard->path) {
  3324. Curl_safefree(wildcard->pattern);
  3325. wildcard->tmp_dtor(wildcard->tmp);
  3326. wildcard->tmp_dtor = ZERO_NULL;
  3327. wildcard->tmp = NULL;
  3328. return CURLE_OUT_OF_MEMORY;
  3329. }
  3330. /* backup old write_function */
  3331. ftp_tmp->backup.write_function = conn->data->set.fwrite_func;
  3332. /* parsing write function */
  3333. conn->data->set.fwrite_func = Curl_ftp_parselist;
  3334. /* backup old file descriptor */
  3335. ftp_tmp->backup.file_descriptor = conn->data->set.out;
  3336. /* let the writefunc callback know what curl pointer is working with */
  3337. conn->data->set.out = conn;
  3338. infof(conn->data, "Wildcard - Parsing started\n");
  3339. return CURLE_OK;
  3340. }
  3341. /* This is called recursively */
  3342. static CURLcode wc_statemach(struct connectdata *conn)
  3343. {
  3344. struct WildcardData * const wildcard = &(conn->data->wildcard);
  3345. CURLcode result = CURLE_OK;
  3346. switch(wildcard->state) {
  3347. case CURLWC_INIT:
  3348. result = init_wc_data(conn);
  3349. if(wildcard->state == CURLWC_CLEAN)
  3350. /* only listing! */
  3351. break;
  3352. else
  3353. wildcard->state = result ? CURLWC_ERROR : CURLWC_MATCHING;
  3354. break;
  3355. case CURLWC_MATCHING: {
  3356. /* In this state is LIST response successfully parsed, so lets restore
  3357. previous WRITEFUNCTION callback and WRITEDATA pointer */
  3358. struct ftp_wc_tmpdata *ftp_tmp = wildcard->tmp;
  3359. conn->data->set.fwrite_func = ftp_tmp->backup.write_function;
  3360. conn->data->set.out = ftp_tmp->backup.file_descriptor;
  3361. ftp_tmp->backup.write_function = ZERO_NULL;
  3362. ftp_tmp->backup.file_descriptor = NULL;
  3363. wildcard->state = CURLWC_DOWNLOADING;
  3364. if(Curl_ftp_parselist_geterror(ftp_tmp->parser)) {
  3365. /* error found in LIST parsing */
  3366. wildcard->state = CURLWC_CLEAN;
  3367. return wc_statemach(conn);
  3368. }
  3369. else if(wildcard->filelist->size == 0) {
  3370. /* no corresponding file */
  3371. wildcard->state = CURLWC_CLEAN;
  3372. return CURLE_REMOTE_FILE_NOT_FOUND;
  3373. }
  3374. return wc_statemach(conn);
  3375. }
  3376. case CURLWC_DOWNLOADING: {
  3377. /* filelist has at least one file, lets get first one */
  3378. struct ftp_conn *ftpc = &conn->proto.ftpc;
  3379. struct curl_fileinfo *finfo = wildcard->filelist->head->ptr;
  3380. char *tmp_path = aprintf("%s%s", wildcard->path, finfo->filename);
  3381. if(!tmp_path)
  3382. return CURLE_OUT_OF_MEMORY;
  3383. /* switch default "state.pathbuffer" and tmp_path, good to see
  3384. ftp_parse_url_path function to understand this trick */
  3385. Curl_safefree(conn->data->state.pathbuffer);
  3386. conn->data->state.pathbuffer = tmp_path;
  3387. conn->data->state.path = tmp_path;
  3388. infof(conn->data, "Wildcard - START of \"%s\"\n", finfo->filename);
  3389. if(conn->data->set.chunk_bgn) {
  3390. long userresponse = conn->data->set.chunk_bgn(
  3391. finfo, wildcard->customptr, (int)wildcard->filelist->size);
  3392. switch(userresponse) {
  3393. case CURL_CHUNK_BGN_FUNC_SKIP:
  3394. infof(conn->data, "Wildcard - \"%s\" skipped by user\n",
  3395. finfo->filename);
  3396. wildcard->state = CURLWC_SKIP;
  3397. return wc_statemach(conn);
  3398. case CURL_CHUNK_BGN_FUNC_FAIL:
  3399. return CURLE_CHUNK_FAILED;
  3400. }
  3401. }
  3402. if(finfo->filetype != CURLFILETYPE_FILE) {
  3403. wildcard->state = CURLWC_SKIP;
  3404. return wc_statemach(conn);
  3405. }
  3406. if(finfo->flags & CURLFINFOFLAG_KNOWN_SIZE)
  3407. ftpc->known_filesize = finfo->size;
  3408. result = ftp_parse_url_path(conn);
  3409. if(result)
  3410. return result;
  3411. /* we don't need the Curl_fileinfo of first file anymore */
  3412. Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
  3413. if(wildcard->filelist->size == 0) { /* remains only one file to down. */
  3414. wildcard->state = CURLWC_CLEAN;
  3415. /* after that will be ftp_do called once again and no transfer
  3416. will be done because of CURLWC_CLEAN state */
  3417. return CURLE_OK;
  3418. }
  3419. } break;
  3420. case CURLWC_SKIP: {
  3421. if(conn->data->set.chunk_end)
  3422. conn->data->set.chunk_end(conn->data->wildcard.customptr);
  3423. Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
  3424. wildcard->state = (wildcard->filelist->size == 0) ?
  3425. CURLWC_CLEAN : CURLWC_DOWNLOADING;
  3426. return wc_statemach(conn);
  3427. }
  3428. case CURLWC_CLEAN: {
  3429. struct ftp_wc_tmpdata *ftp_tmp = wildcard->tmp;
  3430. result = CURLE_OK;
  3431. if(ftp_tmp)
  3432. result = Curl_ftp_parselist_geterror(ftp_tmp->parser);
  3433. wildcard->state = result ? CURLWC_ERROR : CURLWC_DONE;
  3434. } break;
  3435. case CURLWC_DONE:
  3436. case CURLWC_ERROR:
  3437. break;
  3438. }
  3439. return result;
  3440. }
  3441. /***********************************************************************
  3442. *
  3443. * ftp_do()
  3444. *
  3445. * This function is registered as 'curl_do' function. It decodes the path
  3446. * parts etc as a wrapper to the actual DO function (ftp_perform).
  3447. *
  3448. * The input argument is already checked for validity.
  3449. */
  3450. static CURLcode ftp_do(struct connectdata *conn, bool *done)
  3451. {
  3452. CURLcode result = CURLE_OK;
  3453. struct ftp_conn *ftpc = &conn->proto.ftpc;
  3454. *done = FALSE; /* default to false */
  3455. ftpc->wait_data_conn = FALSE; /* default to no such wait */
  3456. if(conn->data->set.wildcardmatch) {
  3457. result = wc_statemach(conn);
  3458. if(conn->data->wildcard.state == CURLWC_SKIP ||
  3459. conn->data->wildcard.state == CURLWC_DONE) {
  3460. /* do not call ftp_regular_transfer */
  3461. return CURLE_OK;
  3462. }
  3463. if(result) /* error, loop or skipping the file */
  3464. return result;
  3465. }
  3466. else { /* no wildcard FSM needed */
  3467. result = ftp_parse_url_path(conn);
  3468. if(result)
  3469. return result;
  3470. }
  3471. result = ftp_regular_transfer(conn, done);
  3472. return result;
  3473. }
  3474. CURLcode Curl_ftpsend(struct connectdata *conn, const char *cmd)
  3475. {
  3476. ssize_t bytes_written;
  3477. #define SBUF_SIZE 1024
  3478. char s[SBUF_SIZE];
  3479. size_t write_len;
  3480. char *sptr=s;
  3481. CURLcode result = CURLE_OK;
  3482. #ifdef HAVE_GSSAPI
  3483. enum protection_level data_sec = conn->data_prot;
  3484. #endif
  3485. write_len = strlen(cmd);
  3486. if(write_len > (sizeof(s) -3))
  3487. return CURLE_BAD_FUNCTION_ARGUMENT;
  3488. strcpy(&s[write_len], "\r\n"); /* append a trailing CRLF */
  3489. write_len +=2;
  3490. bytes_written=0;
  3491. result = Curl_convert_to_network(conn->data, s, write_len);
  3492. /* Curl_convert_to_network calls failf if unsuccessful */
  3493. if(result)
  3494. return result;
  3495. for(;;) {
  3496. #ifdef HAVE_GSSAPI
  3497. conn->data_prot = PROT_CMD;
  3498. #endif
  3499. result = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
  3500. &bytes_written);
  3501. #ifdef HAVE_GSSAPI
  3502. DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
  3503. conn->data_prot = data_sec;
  3504. #endif
  3505. if(result)
  3506. break;
  3507. if(conn->data->set.verbose)
  3508. Curl_debug(conn->data, CURLINFO_HEADER_OUT,
  3509. sptr, (size_t)bytes_written, conn);
  3510. if(bytes_written != (ssize_t)write_len) {
  3511. write_len -= bytes_written;
  3512. sptr += bytes_written;
  3513. }
  3514. else
  3515. break;
  3516. }
  3517. return result;
  3518. }
  3519. /***********************************************************************
  3520. *
  3521. * ftp_quit()
  3522. *
  3523. * This should be called before calling sclose() on an ftp control connection
  3524. * (not data connections). We should then wait for the response from the
  3525. * server before returning. The calling code should then try to close the
  3526. * connection.
  3527. *
  3528. */
  3529. static CURLcode ftp_quit(struct connectdata *conn)
  3530. {
  3531. CURLcode result = CURLE_OK;
  3532. if(conn->proto.ftpc.ctl_valid) {
  3533. result = Curl_pp_sendf(&conn->proto.ftpc.pp, "%s", "QUIT");
  3534. if(result) {
  3535. failf(conn->data, "Failure sending QUIT command: %s",
  3536. curl_easy_strerror(result));
  3537. conn->proto.ftpc.ctl_valid = FALSE; /* mark control connection as bad */
  3538. connclose(conn, "QUIT command failed"); /* mark for connection closure */
  3539. state(conn, FTP_STOP);
  3540. return result;
  3541. }
  3542. state(conn, FTP_QUIT);
  3543. result = ftp_block_statemach(conn);
  3544. }
  3545. return result;
  3546. }
  3547. /***********************************************************************
  3548. *
  3549. * ftp_disconnect()
  3550. *
  3551. * Disconnect from an FTP server. Cleanup protocol-specific per-connection
  3552. * resources. BLOCKING.
  3553. */
  3554. static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection)
  3555. {
  3556. struct ftp_conn *ftpc= &conn->proto.ftpc;
  3557. struct pingpong *pp = &ftpc->pp;
  3558. /* We cannot send quit unconditionally. If this connection is stale or
  3559. bad in any way, sending quit and waiting around here will make the
  3560. disconnect wait in vain and cause more problems than we need to.
  3561. ftp_quit() will check the state of ftp->ctl_valid. If it's ok it
  3562. will try to send the QUIT command, otherwise it will just return.
  3563. */
  3564. if(dead_connection)
  3565. ftpc->ctl_valid = FALSE;
  3566. /* The FTP session may or may not have been allocated/setup at this point! */
  3567. (void)ftp_quit(conn); /* ignore errors on the QUIT */
  3568. if(ftpc->entrypath) {
  3569. struct Curl_easy *data = conn->data;
  3570. if(data->state.most_recent_ftp_entrypath == ftpc->entrypath) {
  3571. data->state.most_recent_ftp_entrypath = NULL;
  3572. }
  3573. free(ftpc->entrypath);
  3574. ftpc->entrypath = NULL;
  3575. }
  3576. freedirs(ftpc);
  3577. free(ftpc->prevpath);
  3578. ftpc->prevpath = NULL;
  3579. free(ftpc->server_os);
  3580. ftpc->server_os = NULL;
  3581. Curl_pp_disconnect(pp);
  3582. #ifdef HAVE_GSSAPI
  3583. Curl_sec_end(conn);
  3584. #endif
  3585. return CURLE_OK;
  3586. }
  3587. /***********************************************************************
  3588. *
  3589. * ftp_parse_url_path()
  3590. *
  3591. * Parse the URL path into separate path components.
  3592. *
  3593. */
  3594. static
  3595. CURLcode ftp_parse_url_path(struct connectdata *conn)
  3596. {
  3597. struct Curl_easy *data = conn->data;
  3598. /* the ftp struct is already inited in ftp_connect() */
  3599. struct FTP *ftp = data->req.protop;
  3600. struct ftp_conn *ftpc = &conn->proto.ftpc;
  3601. const char *slash_pos; /* position of the first '/' char in curpos */
  3602. const char *path_to_use = data->state.path;
  3603. const char *cur_pos;
  3604. const char *filename = NULL;
  3605. cur_pos = path_to_use; /* current position in path. point at the begin of
  3606. next path component */
  3607. ftpc->ctl_valid = FALSE;
  3608. ftpc->cwdfail = FALSE;
  3609. switch(data->set.ftp_filemethod) {
  3610. case FTPFILE_NOCWD:
  3611. /* fastest, but less standard-compliant */
  3612. /*
  3613. The best time to check whether the path is a file or directory is right
  3614. here. so:
  3615. the first condition in the if() right here, is there just in case
  3616. someone decides to set path to NULL one day
  3617. */
  3618. if(path_to_use[0] &&
  3619. (path_to_use[strlen(path_to_use) - 1] != '/') )
  3620. filename = path_to_use; /* this is a full file path */
  3621. /*
  3622. else {
  3623. ftpc->file is not used anywhere other than for operations on a file.
  3624. In other words, never for directory operations.
  3625. So we can safely leave filename as NULL here and use it as a
  3626. argument in dir/file decisions.
  3627. }
  3628. */
  3629. break;
  3630. case FTPFILE_SINGLECWD:
  3631. /* get the last slash */
  3632. if(!path_to_use[0]) {
  3633. /* no dir, no file */
  3634. ftpc->dirdepth = 0;
  3635. break;
  3636. }
  3637. slash_pos=strrchr(cur_pos, '/');
  3638. if(slash_pos || !*cur_pos) {
  3639. size_t dirlen = slash_pos-cur_pos;
  3640. CURLcode result;
  3641. ftpc->dirs = calloc(1, sizeof(ftpc->dirs[0]));
  3642. if(!ftpc->dirs)
  3643. return CURLE_OUT_OF_MEMORY;
  3644. if(!dirlen)
  3645. dirlen++;
  3646. result = Curl_urldecode(conn->data, slash_pos ? cur_pos : "/",
  3647. slash_pos ? dirlen : 1,
  3648. &ftpc->dirs[0], NULL,
  3649. FALSE);
  3650. if(result) {
  3651. freedirs(ftpc);
  3652. return result;
  3653. }
  3654. ftpc->dirdepth = 1; /* we consider it to be a single dir */
  3655. filename = slash_pos ? slash_pos+1 : cur_pos; /* rest is file name */
  3656. }
  3657. else
  3658. filename = cur_pos; /* this is a file name only */
  3659. break;
  3660. default: /* allow pretty much anything */
  3661. case FTPFILE_MULTICWD:
  3662. ftpc->dirdepth = 0;
  3663. ftpc->diralloc = 5; /* default dir depth to allocate */
  3664. ftpc->dirs = calloc(ftpc->diralloc, sizeof(ftpc->dirs[0]));
  3665. if(!ftpc->dirs)
  3666. return CURLE_OUT_OF_MEMORY;
  3667. /* we have a special case for listing the root dir only */
  3668. if(!strcmp(path_to_use, "/")) {
  3669. cur_pos++; /* make it point to the zero byte */
  3670. ftpc->dirs[0] = strdup("/");
  3671. ftpc->dirdepth++;
  3672. }
  3673. else {
  3674. /* parse the URL path into separate path components */
  3675. while((slash_pos = strchr(cur_pos, '/')) != NULL) {
  3676. /* 1 or 0 pointer offset to indicate absolute directory */
  3677. ssize_t absolute_dir = ((cur_pos - data->state.path > 0) &&
  3678. (ftpc->dirdepth == 0))?1:0;
  3679. /* seek out the next path component */
  3680. if(slash_pos-cur_pos) {
  3681. /* we skip empty path components, like "x//y" since the FTP command
  3682. CWD requires a parameter and a non-existent parameter a) doesn't
  3683. work on many servers and b) has no effect on the others. */
  3684. size_t len = slash_pos - cur_pos + absolute_dir;
  3685. CURLcode result =
  3686. Curl_urldecode(conn->data, cur_pos - absolute_dir, len,
  3687. &ftpc->dirs[ftpc->dirdepth], NULL,
  3688. TRUE);
  3689. if(result) {
  3690. free(ftpc->dirs[ftpc->dirdepth]);
  3691. freedirs(ftpc);
  3692. return result;
  3693. }
  3694. }
  3695. else {
  3696. cur_pos = slash_pos + 1; /* jump to the rest of the string */
  3697. if(!ftpc->dirdepth) {
  3698. /* path starts with a slash, add that as a directory */
  3699. ftpc->dirs[ftpc->dirdepth] = strdup("/");
  3700. if(!ftpc->dirs[ftpc->dirdepth++]) { /* run out of memory ... */
  3701. failf(data, "no memory");
  3702. freedirs(ftpc);
  3703. return CURLE_OUT_OF_MEMORY;
  3704. }
  3705. }
  3706. continue;
  3707. }
  3708. cur_pos = slash_pos + 1; /* jump to the rest of the string */
  3709. if(++ftpc->dirdepth >= ftpc->diralloc) {
  3710. /* enlarge array */
  3711. char **bigger;
  3712. ftpc->diralloc *= 2; /* double the size each time */
  3713. bigger = realloc(ftpc->dirs, ftpc->diralloc * sizeof(ftpc->dirs[0]));
  3714. if(!bigger) {
  3715. freedirs(ftpc);
  3716. return CURLE_OUT_OF_MEMORY;
  3717. }
  3718. ftpc->dirs = bigger;
  3719. }
  3720. }
  3721. }
  3722. filename = cur_pos; /* the rest is the file name */
  3723. break;
  3724. } /* switch */
  3725. if(filename && *filename) {
  3726. CURLcode result =
  3727. Curl_urldecode(conn->data, filename, 0, &ftpc->file, NULL, TRUE);
  3728. if(result) {
  3729. freedirs(ftpc);
  3730. return result;
  3731. }
  3732. }
  3733. else
  3734. ftpc->file=NULL; /* instead of point to a zero byte, we make it a NULL
  3735. pointer */
  3736. if(data->set.upload && !ftpc->file && (ftp->transfer == FTPTRANSFER_BODY)) {
  3737. /* We need a file name when uploading. Return error! */
  3738. failf(data, "Uploading to a URL without a file name!");
  3739. return CURLE_URL_MALFORMAT;
  3740. }
  3741. ftpc->cwddone = FALSE; /* default to not done */
  3742. if(ftpc->prevpath) {
  3743. /* prevpath is "raw" so we convert the input path before we compare the
  3744. strings */
  3745. size_t dlen;
  3746. char *path;
  3747. CURLcode result =
  3748. Curl_urldecode(conn->data, data->state.path, 0, &path, &dlen, FALSE);
  3749. if(result) {
  3750. freedirs(ftpc);
  3751. return result;
  3752. }
  3753. dlen -= ftpc->file?strlen(ftpc->file):0;
  3754. if((dlen == strlen(ftpc->prevpath)) &&
  3755. !strncmp(path, ftpc->prevpath, dlen)) {
  3756. infof(data, "Request has same path as previous transfer\n");
  3757. ftpc->cwddone = TRUE;
  3758. }
  3759. free(path);
  3760. }
  3761. return CURLE_OK;
  3762. }
  3763. /* call this when the DO phase has completed */
  3764. static CURLcode ftp_dophase_done(struct connectdata *conn,
  3765. bool connected)
  3766. {
  3767. struct FTP *ftp = conn->data->req.protop;
  3768. struct ftp_conn *ftpc = &conn->proto.ftpc;
  3769. if(connected) {
  3770. int completed;
  3771. CURLcode result = ftp_do_more(conn, &completed);
  3772. if(result) {
  3773. close_secondarysocket(conn);
  3774. return result;
  3775. }
  3776. }
  3777. if(ftp->transfer != FTPTRANSFER_BODY)
  3778. /* no data to transfer */
  3779. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  3780. else if(!connected)
  3781. /* since we didn't connect now, we want do_more to get called */
  3782. conn->bits.do_more = TRUE;
  3783. ftpc->ctl_valid = TRUE; /* seems good */
  3784. return CURLE_OK;
  3785. }
  3786. /* called from multi.c while DOing */
  3787. static CURLcode ftp_doing(struct connectdata *conn,
  3788. bool *dophase_done)
  3789. {
  3790. CURLcode result = ftp_multi_statemach(conn, dophase_done);
  3791. if(result)
  3792. DEBUGF(infof(conn->data, "DO phase failed\n"));
  3793. else if(*dophase_done) {
  3794. result = ftp_dophase_done(conn, FALSE /* not connected */);
  3795. DEBUGF(infof(conn->data, "DO phase is complete2\n"));
  3796. }
  3797. return result;
  3798. }
  3799. /***********************************************************************
  3800. *
  3801. * ftp_regular_transfer()
  3802. *
  3803. * The input argument is already checked for validity.
  3804. *
  3805. * Performs all commands done before a regular transfer between a local and a
  3806. * remote host.
  3807. *
  3808. * ftp->ctl_valid starts out as FALSE, and gets set to TRUE if we reach the
  3809. * ftp_done() function without finding any major problem.
  3810. */
  3811. static
  3812. CURLcode ftp_regular_transfer(struct connectdata *conn,
  3813. bool *dophase_done)
  3814. {
  3815. CURLcode result=CURLE_OK;
  3816. bool connected=FALSE;
  3817. struct Curl_easy *data = conn->data;
  3818. struct ftp_conn *ftpc = &conn->proto.ftpc;
  3819. data->req.size = -1; /* make sure this is unknown at this point */
  3820. Curl_pgrsSetUploadCounter(data, 0);
  3821. Curl_pgrsSetDownloadCounter(data, 0);
  3822. Curl_pgrsSetUploadSize(data, -1);
  3823. Curl_pgrsSetDownloadSize(data, -1);
  3824. ftpc->ctl_valid = TRUE; /* starts good */
  3825. result = ftp_perform(conn,
  3826. &connected, /* have we connected after PASV/PORT */
  3827. dophase_done); /* all commands in the DO-phase done? */
  3828. if(!result) {
  3829. if(!*dophase_done)
  3830. /* the DO phase has not completed yet */
  3831. return CURLE_OK;
  3832. result = ftp_dophase_done(conn, connected);
  3833. if(result)
  3834. return result;
  3835. }
  3836. else
  3837. freedirs(ftpc);
  3838. return result;
  3839. }
  3840. static CURLcode ftp_setup_connection(struct connectdata *conn)
  3841. {
  3842. struct Curl_easy *data = conn->data;
  3843. char *type;
  3844. char command;
  3845. struct FTP *ftp;
  3846. if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
  3847. /* Unless we have asked to tunnel ftp operations through the proxy, we
  3848. switch and use HTTP operations only */
  3849. #ifndef CURL_DISABLE_HTTP
  3850. if(conn->handler == &Curl_handler_ftp)
  3851. conn->handler = &Curl_handler_ftp_proxy;
  3852. else {
  3853. #ifdef USE_SSL
  3854. conn->handler = &Curl_handler_ftps_proxy;
  3855. #else
  3856. failf(data, "FTPS not supported!");
  3857. return CURLE_UNSUPPORTED_PROTOCOL;
  3858. #endif
  3859. }
  3860. /* set it up as a HTTP connection instead */
  3861. return conn->handler->setup_connection(conn);
  3862. #else
  3863. failf(data, "FTP over http proxy requires HTTP support built-in!");
  3864. return CURLE_UNSUPPORTED_PROTOCOL;
  3865. #endif
  3866. }
  3867. conn->data->req.protop = ftp = malloc(sizeof(struct FTP));
  3868. if(NULL == ftp)
  3869. return CURLE_OUT_OF_MEMORY;
  3870. data->state.path++; /* don't include the initial slash */
  3871. data->state.slash_removed = TRUE; /* we've skipped the slash */
  3872. /* FTP URLs support an extension like ";type=<typecode>" that
  3873. * we'll try to get now! */
  3874. type = strstr(data->state.path, ";type=");
  3875. if(!type)
  3876. type = strstr(conn->host.rawalloc, ";type=");
  3877. if(type) {
  3878. *type = 0; /* it was in the middle of the hostname */
  3879. command = Curl_raw_toupper(type[6]);
  3880. conn->bits.type_set = TRUE;
  3881. switch(command) {
  3882. case 'A': /* ASCII mode */
  3883. data->set.prefer_ascii = TRUE;
  3884. break;
  3885. case 'D': /* directory mode */
  3886. data->set.ftp_list_only = TRUE;
  3887. break;
  3888. case 'I': /* binary mode */
  3889. default:
  3890. /* switch off ASCII */
  3891. data->set.prefer_ascii = FALSE;
  3892. break;
  3893. }
  3894. }
  3895. /* get some initial data into the ftp struct */
  3896. ftp->bytecountp = &conn->data->req.bytecount;
  3897. ftp->transfer = FTPTRANSFER_BODY;
  3898. ftp->downloadsize = 0;
  3899. /* No need to duplicate user+password, the connectdata struct won't change
  3900. during a session, but we re-init them here since on subsequent inits
  3901. since the conn struct may have changed or been replaced.
  3902. */
  3903. ftp->user = conn->user;
  3904. ftp->passwd = conn->passwd;
  3905. if(isBadFtpString(ftp->user))
  3906. return CURLE_URL_MALFORMAT;
  3907. if(isBadFtpString(ftp->passwd))
  3908. return CURLE_URL_MALFORMAT;
  3909. conn->proto.ftpc.known_filesize = -1; /* unknown size for now */
  3910. return CURLE_OK;
  3911. }
  3912. #endif /* CURL_DISABLE_FTP */