FtpFileSystem.cpp 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. //---------------------------------------------------------------------------
  5. #include <list>
  6. #define MPEXT
  7. #include "FtpFileSystem.h"
  8. #include "FileZillaIntf.h"
  9. #include "Common.h"
  10. #include "Exceptions.h"
  11. #include "Terminal.h"
  12. #include "TextsCore.h"
  13. #include "TextsFileZilla.h"
  14. #include "HelpCore.h"
  15. #include "Security.h"
  16. #include "NeonIntf.h"
  17. #include <StrUtils.hpp>
  18. #include <DateUtils.hpp>
  19. #include <openssl/x509_vfy.h>
  20. #include <limits>
  21. //---------------------------------------------------------------------------
  22. #pragma package(smart_init)
  23. //---------------------------------------------------------------------------
  24. #define FILE_OPERATION_LOOP_TERMINAL FTerminal
  25. //---------------------------------------------------------------------------
  26. const int DummyCodeClass = 8;
  27. const int DummyTimeoutCode = 801;
  28. const int DummyCancelCode = 802;
  29. const int DummyDisconnectCode = 803;
  30. //---------------------------------------------------------------------------
  31. class TFileZillaImpl : public TFileZillaIntf
  32. {
  33. public:
  34. __fastcall TFileZillaImpl(TFTPFileSystem * FileSystem);
  35. virtual const wchar_t * __fastcall Option(int OptionID) const;
  36. virtual int __fastcall OptionVal(int OptionID) const;
  37. protected:
  38. virtual bool __fastcall DoPostMessage(TMessageType Type, WPARAM wParam, LPARAM lParam);
  39. virtual bool __fastcall HandleStatus(const wchar_t * Status, int Type);
  40. virtual bool __fastcall HandleAsynchRequestOverwrite(
  41. wchar_t * FileName1, size_t FileName1Len, const wchar_t * FileName2,
  42. const wchar_t * Path1, const wchar_t * Path2,
  43. __int64 Size1, __int64 Size2, time_t LocalTime,
  44. bool HasLocalTime, const TRemoteFileTime & RemoteTime, void * UserData, int & RequestResult);
  45. virtual bool __fastcall HandleAsynchRequestVerifyCertificate(
  46. const TFtpsCertificateData & Data, int & RequestResult);
  47. virtual bool __fastcall HandleAsynchRequestNeedPass(
  48. struct TNeedPassRequestData & Data, int & RequestResult);
  49. virtual bool __fastcall HandleListData(const wchar_t * Path, const TListDataEntry * Entries,
  50. unsigned int Count);
  51. virtual bool __fastcall HandleTransferStatus(bool Valid, __int64 TransferSize,
  52. __int64 Bytes, bool FileTransfer);
  53. virtual bool __fastcall HandleReply(int Command, unsigned int Reply);
  54. virtual bool __fastcall HandleCapabilities(TFTPServerCapabilities * ServerCapabilities);
  55. virtual bool __fastcall CheckError(int ReturnCode, const wchar_t * Context);
  56. virtual void PreserveDownloadFileTime(HANDLE Handle, void * UserData);
  57. virtual bool GetFileModificationTimeInUtc(const wchar_t * FileName, struct tm & Time);
  58. virtual wchar_t * LastSysErrorMessage();
  59. virtual std::wstring GetClientString();
  60. virtual void SetupSsl(ssl_st * Ssl);
  61. private:
  62. TFTPFileSystem * FFileSystem;
  63. };
  64. //---------------------------------------------------------------------------
  65. __fastcall TFileZillaImpl::TFileZillaImpl(TFTPFileSystem * FileSystem) :
  66. TFileZillaIntf(),
  67. FFileSystem(FileSystem)
  68. {
  69. }
  70. //---------------------------------------------------------------------------
  71. const wchar_t * __fastcall TFileZillaImpl::Option(int OptionID) const
  72. {
  73. return FFileSystem->GetOption(OptionID);
  74. }
  75. //---------------------------------------------------------------------------
  76. int __fastcall TFileZillaImpl::OptionVal(int OptionID) const
  77. {
  78. return FFileSystem->GetOptionVal(OptionID);
  79. }
  80. //---------------------------------------------------------------------------
  81. bool __fastcall TFileZillaImpl::DoPostMessage(TMessageType Type, WPARAM wParam, LPARAM lParam)
  82. {
  83. return FFileSystem->PostMessage(Type, wParam, lParam);
  84. }
  85. //---------------------------------------------------------------------------
  86. bool __fastcall TFileZillaImpl::HandleStatus(const wchar_t * Status, int Type)
  87. {
  88. return FFileSystem->HandleStatus(Status, Type);
  89. }
  90. //---------------------------------------------------------------------------
  91. bool __fastcall TFileZillaImpl::HandleAsynchRequestOverwrite(
  92. wchar_t * FileName1, size_t FileName1Len, const wchar_t * FileName2,
  93. const wchar_t * Path1, const wchar_t * Path2,
  94. __int64 Size1, __int64 Size2, time_t LocalTime,
  95. bool HasLocalTime, const TRemoteFileTime & RemoteTime, void * UserData, int & RequestResult)
  96. {
  97. return FFileSystem->HandleAsynchRequestOverwrite(
  98. FileName1, FileName1Len, FileName2, Path1, Path2, Size1, Size2, LocalTime,
  99. HasLocalTime, RemoteTime, UserData, RequestResult);
  100. }
  101. //---------------------------------------------------------------------------
  102. bool __fastcall TFileZillaImpl::HandleAsynchRequestVerifyCertificate(
  103. const TFtpsCertificateData & Data, int & RequestResult)
  104. {
  105. return FFileSystem->HandleAsynchRequestVerifyCertificate(Data, RequestResult);
  106. }
  107. //---------------------------------------------------------------------------
  108. bool __fastcall TFileZillaImpl::HandleAsynchRequestNeedPass(
  109. struct TNeedPassRequestData & Data, int & RequestResult)
  110. {
  111. return FFileSystem->HandleAsynchRequestNeedPass(Data, RequestResult);
  112. }
  113. //---------------------------------------------------------------------------
  114. bool __fastcall TFileZillaImpl::HandleListData(const wchar_t * Path,
  115. const TListDataEntry * Entries, unsigned int Count)
  116. {
  117. return FFileSystem->HandleListData(Path, Entries, Count);
  118. }
  119. //---------------------------------------------------------------------------
  120. bool __fastcall TFileZillaImpl::HandleTransferStatus(bool Valid, __int64 TransferSize,
  121. __int64 Bytes, bool FileTransfer)
  122. {
  123. return FFileSystem->HandleTransferStatus(Valid, TransferSize, Bytes, FileTransfer);
  124. }
  125. //---------------------------------------------------------------------------
  126. bool __fastcall TFileZillaImpl::HandleReply(int Command, unsigned int Reply)
  127. {
  128. return FFileSystem->HandleReply(Command, Reply);
  129. }
  130. //---------------------------------------------------------------------------
  131. bool __fastcall TFileZillaImpl::HandleCapabilities(TFTPServerCapabilities * ServerCapabilities)
  132. {
  133. return FFileSystem->HandleCapabilities(ServerCapabilities);
  134. }
  135. //---------------------------------------------------------------------------
  136. bool __fastcall TFileZillaImpl::CheckError(int ReturnCode, const wchar_t * Context)
  137. {
  138. return FFileSystem->CheckError(ReturnCode, Context);
  139. }
  140. //---------------------------------------------------------------------------
  141. void TFileZillaImpl::PreserveDownloadFileTime(HANDLE Handle, void * UserData)
  142. {
  143. return FFileSystem->PreserveDownloadFileTime(Handle, UserData);
  144. }
  145. //---------------------------------------------------------------------------
  146. bool TFileZillaImpl::GetFileModificationTimeInUtc(const wchar_t * FileName, struct tm & Time)
  147. {
  148. return FFileSystem->GetFileModificationTimeInUtc(FileName, Time);
  149. }
  150. //---------------------------------------------------------------------------
  151. wchar_t * TFileZillaImpl::LastSysErrorMessage()
  152. {
  153. return _wcsdup(::LastSysErrorMessage().c_str());
  154. }
  155. //---------------------------------------------------------------------------
  156. std::wstring TFileZillaImpl::GetClientString()
  157. {
  158. return std::wstring(SshVersionString().c_str());
  159. }
  160. //---------------------------------------------------------------------------
  161. void TFileZillaImpl::SetupSsl(ssl_st * Ssl)
  162. {
  163. ::SetupSsl(Ssl, FFileSystem->FTerminal->SessionData->MinTlsVersion, FFileSystem->FTerminal->SessionData->MaxTlsVersion);
  164. }
  165. //---------------------------------------------------------------------------
  166. //---------------------------------------------------------------------------
  167. class TMessageQueue : public std::list<std::pair<WPARAM, LPARAM> >
  168. {
  169. };
  170. //---------------------------------------------------------------------------
  171. //---------------------------------------------------------------------------
  172. struct TFileTransferData
  173. {
  174. TFileTransferData()
  175. {
  176. Params = 0;
  177. AutoResume = false;
  178. OverwriteResult = -1;
  179. CopyParam = NULL;
  180. }
  181. UnicodeString FileName;
  182. int Params;
  183. bool AutoResume;
  184. int OverwriteResult;
  185. const TCopyParamType * CopyParam;
  186. TDateTime Modification;
  187. };
  188. //---------------------------------------------------------------------------
  189. const UnicodeString SiteCommand(L"SITE");
  190. const UnicodeString SymlinkSiteCommand(L"SYMLINK");
  191. const UnicodeString CopySiteCommand(L"COPY");
  192. const UnicodeString HashCommand(L"HASH"); // Cerberos + FileZilla servers
  193. const UnicodeString AvblCommand(L"AVBL");
  194. const UnicodeString XQuotaCommand(L"XQUOTA");
  195. const UnicodeString MdtmCommand(L"MDTM");
  196. const UnicodeString SizeCommand(L"SIZE");
  197. const UnicodeString CsidCommand(L"CSID");
  198. const UnicodeString DirectoryHasBytesPrefix(L"226-Directory has");
  199. //---------------------------------------------------------------------------
  200. class TFileListHelper
  201. {
  202. public:
  203. TFileListHelper(TFTPFileSystem * FileSystem, TRemoteFileList * FileList,
  204. bool IgnoreFileList) :
  205. FFileSystem(FileSystem),
  206. FFileList(FFileSystem->FFileList),
  207. FIgnoreFileList(FFileSystem->FIgnoreFileList)
  208. {
  209. FFileSystem->FFileList = FileList;
  210. FFileSystem->FIgnoreFileList = IgnoreFileList;
  211. }
  212. ~TFileListHelper()
  213. {
  214. FFileSystem->FFileList = FFileList;
  215. FFileSystem->FIgnoreFileList = FIgnoreFileList;
  216. }
  217. private:
  218. TFTPFileSystem * FFileSystem;
  219. TRemoteFileList * FFileList;
  220. bool FIgnoreFileList;
  221. };
  222. //---------------------------------------------------------------------------
  223. __fastcall TFTPFileSystem::TFTPFileSystem(TTerminal * ATerminal):
  224. TCustomFileSystem(ATerminal),
  225. FFileZillaIntf(NULL),
  226. FQueueCriticalSection(new TCriticalSection),
  227. FTransferStatusCriticalSection(new TCriticalSection),
  228. FQueueEvent(CreateEvent(NULL, true, false, NULL)),
  229. FQueue(new TMessageQueue),
  230. FReply(0),
  231. FCommandReply(0),
  232. FLastCommand(CMD_UNKNOWN),
  233. FLastResponse(new TStringList()),
  234. FLastErrorResponse(new TStringList()),
  235. FLastError(new TStringList()),
  236. FFeatures(new TStringList()),
  237. FFileList(NULL),
  238. FFileListCache(NULL),
  239. FActive(false),
  240. FWaitingForReply(false),
  241. FIgnoreFileList(false),
  242. FOnCaptureOutput(NULL),
  243. FFileSystemInfoValid(false),
  244. FDoListAll(false),
  245. FServerCapabilities(NULL),
  246. FReadCurrentDirectory(false)
  247. {
  248. ResetReply();
  249. FListAll = FTerminal->SessionData->FtpListAll;
  250. FWorkFromCwd = FTerminal->SessionData->FtpWorkFromCwd;
  251. FFileSystemInfo.ProtocolBaseName = L"FTP";
  252. FFileSystemInfo.ProtocolName = FFileSystemInfo.ProtocolBaseName;
  253. FTimeoutStatus = LoadStr(IDS_ERRORMSG_TIMEOUT);
  254. FDisconnectStatus = LoadStr(IDS_STATUSMSG_DISCONNECTED);
  255. FServerCapabilities = new TFTPServerCapabilities();
  256. FHashAlgs.reset(new TStringList());
  257. FSupportedCommands.reset(CreateSortedStringList());
  258. FSupportedSiteCommands.reset(CreateSortedStringList());
  259. FCertificate = NULL;
  260. FPrivateKey = NULL;
  261. FBytesAvailable = -1;
  262. FBytesAvailableSupported = false;
  263. FLoggedIn = false;
  264. FAnyTransferSucceeded = false; // Do not reset on reconnect
  265. FForceReadSymlink = false;
  266. FChecksumAlgs.reset(new TStringList());
  267. FChecksumCommands.reset(new TStringList());
  268. RegisterChecksumAlgCommand(Sha1ChecksumAlg, L"XSHA1"); // e.g. Cerberos FTP
  269. RegisterChecksumAlgCommand(Sha256ChecksumAlg, L"XSHA256"); // e.g. Cerberos FTP
  270. RegisterChecksumAlgCommand(Sha512ChecksumAlg, L"XSHA512"); // e.g. Cerberos FTP
  271. RegisterChecksumAlgCommand(Md5ChecksumAlg, L"XMD5"); // e.g. Cerberos FTP
  272. RegisterChecksumAlgCommand(Md5ChecksumAlg, L"MD5"); // e.g. Apache FTP
  273. RegisterChecksumAlgCommand(Crc32ChecksumAlg, L"XCRC"); // e.g. Cerberos FTP
  274. }
  275. //---------------------------------------------------------------------------
  276. __fastcall TFTPFileSystem::~TFTPFileSystem()
  277. {
  278. DebugAssert(FFileList == NULL);
  279. FFileZillaIntf->Destroying();
  280. // to release memory associated with the messages
  281. DiscardMessages();
  282. delete FFileZillaIntf;
  283. FFileZillaIntf = NULL;
  284. delete FQueue;
  285. FQueue = NULL;
  286. CloseHandle(FQueueEvent);
  287. delete FQueueCriticalSection;
  288. FQueueCriticalSection = NULL;
  289. delete FTransferStatusCriticalSection;
  290. FTransferStatusCriticalSection = NULL;
  291. delete FLastResponse;
  292. FLastResponse = NULL;
  293. delete FLastErrorResponse;
  294. FLastErrorResponse = NULL;
  295. delete FLastError;
  296. FLastError = NULL;
  297. delete FFeatures;
  298. FFeatures = NULL;
  299. delete FServerCapabilities;
  300. FServerCapabilities = NULL;
  301. ResetCaches();
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TFTPFileSystem::Open()
  305. {
  306. // on reconnect, typically there may be pending status messages from previous session
  307. DiscardMessages();
  308. ResetCaches();
  309. FReadCurrentDirectory = true;
  310. FHomeDirectory = L"";
  311. FLoggedIn = false;
  312. FLastDataSent = Now();
  313. FMultiLineResponse = false;
  314. // initialize FZAPI on the first connect only
  315. if (FFileZillaIntf == NULL)
  316. {
  317. FFileZillaIntf = new TFileZillaImpl(this);
  318. try
  319. {
  320. TFileZillaIntf::TLogLevel LogLevel;
  321. switch (FTerminal->Configuration->ActualLogProtocol)
  322. {
  323. default:
  324. case -1:
  325. LogLevel = TFileZillaIntf::LOG_WARNING;
  326. break;
  327. case 0:
  328. case 1:
  329. LogLevel = TFileZillaIntf::LOG_PROGRESS;
  330. break;
  331. case 2:
  332. LogLevel = TFileZillaIntf::LOG_INFO;
  333. break;
  334. }
  335. FFileZillaIntf->SetDebugLevel(LogLevel);
  336. FFileZillaIntf->Init();
  337. }
  338. catch(...)
  339. {
  340. delete FFileZillaIntf;
  341. FFileZillaIntf = NULL;
  342. throw;
  343. }
  344. }
  345. TSessionData * Data = FTerminal->SessionData;
  346. FWindowsServer = false;
  347. FMVS = false;
  348. FVMS = false;
  349. FFileZilla = false;
  350. FIIS = false;
  351. FTransferActiveImmediately = (Data->FtpTransferActiveImmediately == asOn);
  352. FVMSAllRevisions = Data->VMSAllRevisions;
  353. FSessionInfo.LoginTime = Now();
  354. FSessionInfo.CertificateVerifiedManually = false;
  355. UnicodeString HostName = Data->HostNameExpanded;
  356. UnicodeString UserName = Data->UserNameExpanded;
  357. UnicodeString Password = Data->Password;
  358. UnicodeString Account = Data->FtpAccount;
  359. UnicodeString Path = Data->RemoteDirectory;
  360. int ServerType;
  361. switch (Data->Ftps)
  362. {
  363. case ftpsNone:
  364. ServerType = TFileZillaIntf::SERVER_FTP;
  365. break;
  366. case ftpsImplicit:
  367. ServerType = TFileZillaIntf::SERVER_FTP_SSL_IMPLICIT;
  368. FSessionInfo.SecurityProtocolName = LoadStr(FTPS_IMPLICIT);
  369. break;
  370. case ftpsExplicitSsl:
  371. ServerType = TFileZillaIntf::SERVER_FTP_SSL_EXPLICIT;
  372. FSessionInfo.SecurityProtocolName = LoadStr(FTPS_EXPLICIT);
  373. break;
  374. case ftpsExplicitTls:
  375. ServerType = TFileZillaIntf::SERVER_FTP_TLS_EXPLICIT;
  376. FSessionInfo.SecurityProtocolName = LoadStr(FTPS_EXPLICIT);
  377. break;
  378. default:
  379. DebugFail();
  380. break;
  381. }
  382. int Pasv = (Data->FtpPasvMode ? 1 : 2);
  383. int TimeZoneOffset = Data->TimeDifferenceAuto ? 0 : TimeToMinutes(Data->TimeDifference);
  384. int UTF8 = 0;
  385. switch (Data->NotUtf)
  386. {
  387. case asOn:
  388. UTF8 = 2;
  389. break;
  390. case asOff:
  391. UTF8 = 1;
  392. break;
  393. case asAuto:
  394. UTF8 = 0;
  395. break;
  396. }
  397. FPasswordFailed = false;
  398. FStoredPasswordTried = false;
  399. bool PromptedForCredentials = false;
  400. do
  401. {
  402. FDetectTimeDifference = Data->TimeDifferenceAuto;
  403. FTimeDifference = 0;
  404. ResetFeatures();
  405. FSystem = EmptyStr;
  406. FServerID = EmptyStr;
  407. FWelcomeMessage = EmptyStr;
  408. FFileSystemInfoValid = false;
  409. // TODO: the same for account? it ever used?
  410. // ask for username if it was not specified in advance, even on retry,
  411. // but keep previous one as default,
  412. if (Data->UserNameExpanded.IsEmpty() && !FTerminal->SessionData->FingerprintScan)
  413. {
  414. FTerminal->LogEvent(L"Username prompt (no username provided)");
  415. if (!PromptedForCredentials)
  416. {
  417. FTerminal->Information(LoadStr(FTP_CREDENTIAL_PROMPT), false);
  418. PromptedForCredentials = true;
  419. }
  420. if (!FTerminal->PromptUser(Data, pkUserName, LoadStr(USERNAME_TITLE), L"",
  421. LoadStr(USERNAME_PROMPT2), true, 0, UserName))
  422. {
  423. FTerminal->FatalError(NULL, LoadStr(CREDENTIALS_NOT_SPECIFIED));
  424. }
  425. else
  426. {
  427. FUserName = UserName;
  428. }
  429. }
  430. // On retry ask for password.
  431. // This is particularly important, when stored password is no longer valid,
  432. // so we do not blindly try keep trying it in a loop (possibly causing account lockout)
  433. if (FPasswordFailed)
  434. {
  435. FTerminal->LogEvent(L"Password prompt (last login attempt failed)");
  436. Password = L"";
  437. if (!FTerminal->PromptUser(Data, pkPassword, LoadStr(PASSWORD_TITLE), L"",
  438. LoadStr(PASSWORD_PROMPT), false, 0, Password))
  439. {
  440. FTerminal->FatalError(NULL, LoadStr(CREDENTIALS_NOT_SPECIFIED));
  441. }
  442. }
  443. if ((Data->Ftps != ftpsNone) && (FCertificate == NULL))
  444. {
  445. FTerminal->LoadTlsCertificate(FCertificate, FPrivateKey);
  446. }
  447. FPasswordFailed = false;
  448. FActive = FFileZillaIntf->Connect(
  449. HostName.c_str(), Data->PortNumber, UserName.c_str(),
  450. Password.c_str(), Account.c_str(), Path.c_str(),
  451. ServerType, Pasv, TimeZoneOffset, UTF8, Data->FtpForcePasvIp,
  452. Data->FtpUseMlsd, FCertificate, FPrivateKey);
  453. DebugAssert(FActive);
  454. try
  455. {
  456. // do not wait for FTP response code as Connect is complex operation
  457. GotReply(WaitForCommandReply(false), REPLY_CONNECT, LoadStr(CONNECTION_FAILED));
  458. Shred(Password);
  459. // we have passed, even if we got 530 on the way (if it is possible at all),
  460. // ignore it
  461. DebugAssert(!FPasswordFailed);
  462. FPasswordFailed = false;
  463. }
  464. catch(...)
  465. {
  466. if (FPasswordFailed)
  467. {
  468. FTerminal->Information(LoadStr(FTP_ACCESS_DENIED), false);
  469. }
  470. else
  471. {
  472. // see handling of REPLY_CONNECT in GotReply
  473. FTerminal->Closed();
  474. throw;
  475. }
  476. }
  477. }
  478. while (FPasswordFailed);
  479. ProcessFeatures();
  480. // see also TWebDAVFileSystem::CollectTLSSessionInfo()
  481. FSessionInfo.CSCipher = FFileZillaIntf->GetCipherName().c_str();
  482. FSessionInfo.SCCipher = FSessionInfo.CSCipher;
  483. UnicodeString TlsVersionStr = FFileZillaIntf->GetTlsVersionStr().c_str();
  484. AddToList(FSessionInfo.SecurityProtocolName, TlsVersionStr, L", ");
  485. FLoggedIn = true;
  486. }
  487. //---------------------------------------------------------------------------
  488. void __fastcall TFTPFileSystem::Close()
  489. {
  490. DebugAssert(FActive);
  491. bool Result;
  492. bool Opening = (FTerminal->Status == ssOpening);
  493. if (FFileZillaIntf->Close(Opening))
  494. {
  495. DebugCheck(FLAGSET(WaitForCommandReply(false), TFileZillaIntf::REPLY_DISCONNECTED));
  496. Result = true;
  497. }
  498. else
  499. {
  500. // See TFileZillaIntf::Close
  501. Result = Opening;
  502. }
  503. if (DebugAlwaysTrue(Result))
  504. {
  505. DebugAssert(FActive);
  506. Disconnect();
  507. }
  508. }
  509. //---------------------------------------------------------------------------
  510. bool __fastcall TFTPFileSystem::GetActive()
  511. {
  512. return FActive;
  513. }
  514. //---------------------------------------------------------------------------
  515. void __fastcall TFTPFileSystem::CollectUsage()
  516. {
  517. switch (FTerminal->SessionData->Ftps)
  518. {
  519. case ftpsNone:
  520. // noop
  521. break;
  522. case ftpsImplicit:
  523. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPSImplicit");
  524. break;
  525. case ftpsExplicitSsl:
  526. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPSExplicitSSL");
  527. break;
  528. case ftpsExplicitTls:
  529. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPSExplicitTLS");
  530. break;
  531. default:
  532. DebugFail();
  533. break;
  534. }
  535. if (!FTerminal->SessionData->TlsCertificateFile.IsEmpty())
  536. {
  537. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPSCertificate");
  538. }
  539. if (FFileZillaIntf->UsingMlsd())
  540. {
  541. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPMLSD");
  542. }
  543. else
  544. {
  545. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPLIST");
  546. }
  547. if (FFileZillaIntf->UsingUtf8())
  548. {
  549. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPUTF8");
  550. }
  551. else
  552. {
  553. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPNonUTF8");
  554. }
  555. if (!CurrentDirectory.IsEmpty() && (CurrentDirectory[1] != L'/'))
  556. {
  557. if (IsUnixStyleWindowsPath(CurrentDirectory))
  558. {
  559. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPWindowsPath");
  560. }
  561. else if ((CurrentDirectory.Length() >= 3) && IsLetter(CurrentDirectory[1]) && (CurrentDirectory[2] == L':') && (CurrentDirectory[3] == L'/'))
  562. {
  563. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPRealWindowsPath");
  564. }
  565. else
  566. {
  567. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPOtherPath");
  568. }
  569. }
  570. UnicodeString TlsVersionStr = FFileZillaIntf->GetTlsVersionStr().c_str();
  571. if (!TlsVersionStr.IsEmpty())
  572. {
  573. FTerminal->CollectTlsUsage(TlsVersionStr);
  574. }
  575. if (FFileZilla)
  576. {
  577. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPFileZilla");
  578. }
  579. // 220 ProFTPD 1.3.4a Server (Debian) [::ffff:192.168.179.137]
  580. // SYST
  581. // 215 UNIX Type: L8
  582. else if (ContainsText(FWelcomeMessage, L"ProFTPD"))
  583. {
  584. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPProFTPD");
  585. }
  586. // 220 Microsoft FTP Service
  587. // SYST
  588. // 215 Windows_NT
  589. else if (FIIS)
  590. {
  591. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPIIS");
  592. }
  593. // 220 (vsFTPd 3.0.2)
  594. // SYST
  595. // 215 UNIX Type: L8
  596. // (Welcome message is configurable)
  597. else if (ContainsText(FWelcomeMessage, L"vsFTPd"))
  598. {
  599. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPvsFTPd");
  600. }
  601. // 220 Welcome to Pure-FTPd.
  602. // ...
  603. // SYST
  604. // 215 UNIX Type: L8
  605. else if (ContainsText(FWelcomeMessage, L"Pure-FTPd"))
  606. {
  607. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPPureFTPd");
  608. }
  609. // 220 Titan FTP Server 10.47.1892 Ready.
  610. // ...
  611. // SYST
  612. // 215 UNIX Type: L8
  613. else if (ContainsText(FWelcomeMessage, L"Titan FTP Server"))
  614. {
  615. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPTitan");
  616. }
  617. // 220-Cerberus FTP Server - Home Edition
  618. // 220-This is the UNLICENSED Home Edition and may be used for home, personal use only
  619. // 220-Welcome to Cerberus FTP Server
  620. // 220 Created by Cerberus, LLC
  621. else if (ContainsText(FWelcomeMessage, L"Cerberus FTP Server"))
  622. {
  623. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPCerberus");
  624. }
  625. // 220 Serv-U FTP Server v15.1 ready...
  626. else if (ContainsText(FWelcomeMessage, L"Serv-U FTP Server"))
  627. {
  628. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPServU");
  629. }
  630. else if (ContainsText(FWelcomeMessage, L"WS_FTP"))
  631. {
  632. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPWSFTP");
  633. }
  634. // 220 Welcome to the most popular FTP hosting service! Save on hardware, software, hosting and admin. Share files/folders with read-write permission. Visit http://www.drivehq.com/ftp/
  635. // ...
  636. // SYST
  637. // 215 UNIX emulated by DriveHQ FTP Server.
  638. else if (ContainsText(FSystem, L"DriveHQ"))
  639. {
  640. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPDriveHQ");
  641. }
  642. // 220 GlobalSCAPE EFT Server (v. 6.0) * UNREGISTERED COPY *
  643. // ...
  644. // SYST
  645. // 215 UNIX Type: L8
  646. else if (ContainsText(FWelcomeMessage, L"GlobalSCAPE"))
  647. {
  648. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPGlobalScape");
  649. }
  650. // 220-<custom message>
  651. // 220 CompleteFTP v 8.1.3
  652. // ...
  653. // SYST
  654. // UNIX Type: L8
  655. else if (ContainsText(FWelcomeMessage, L"CompleteFTP"))
  656. {
  657. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPComplete");
  658. }
  659. // 220 Core FTP Server Version 1.2, build 567, 64-bit, installed 8 days ago Unregistered
  660. // ...
  661. // SYST
  662. // 215 UNIX Type: L8
  663. else if (ContainsText(FWelcomeMessage, L"Core FTP Server"))
  664. {
  665. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPCore");
  666. }
  667. // 220 Service ready for new user.
  668. // ..
  669. // SYST
  670. // 215 UNIX Type: Apache FtpServer
  671. // (e.g. brickftp.com)
  672. else if (ContainsText(FSystem, L"Apache FtpServer"))
  673. {
  674. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPApache");
  675. }
  676. // 220 pos1 FTP server (GNU inetutils 1.3b) ready.
  677. // ...
  678. // SYST
  679. // 215 UNIX Type: L8 Version: Linux 2.6.15.7-ELinOS-314pm3
  680. // Displaying "(GNU inetutils 1.3b)" in a welcome message can be turned off (-q switch):
  681. // 220 pos1 FTP server ready.
  682. // (the same for "Version: Linux 2.6.15.7-ELinOS-314pm3" in SYST response)
  683. else if (ContainsText(FWelcomeMessage, L"GNU inetutils"))
  684. {
  685. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPInetutils");
  686. }
  687. // 220 Syncplify.me Server! FTP(S) Service Ready
  688. // Message is configurable
  689. else if (ContainsText(FWelcomeMessage, L"Syncplify"))
  690. {
  691. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPSyncplify");
  692. }
  693. // 220-Idea FTP Server v0.80 (xxx.home.pl) [xxx.xxx.xxx.xxx]
  694. // 220 Ready
  695. // ...
  696. // SYST
  697. // UNIX Type: L8
  698. else if (ContainsText(FWelcomeMessage, L"Idea FTP Server"))
  699. {
  700. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPIdea");
  701. }
  702. // 220-FTPD1 IBM FTP CS V2R1 at name.test.com, 13:49:38 on 2016-01-28.
  703. // ...
  704. // SYST
  705. // 215 MVS is the operating system of this server. FTP Server is running on z/OS.
  706. else if (FMVS)
  707. {
  708. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPMVS");
  709. }
  710. // 220 xxx.xxx.xxx (xxx.xxx.xxx) FTP-OpenVMS FTPD V5.3-3 (c) 1998 Process Software Corporation
  711. // ...
  712. // SYST
  713. // 215 VMS system type. VMS V5.5-2.
  714. else if (FVMS)
  715. {
  716. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPVMS");
  717. }
  718. else
  719. {
  720. FTerminal->Configuration->Usage->Inc(L"OpenedSessionsFTPOther");
  721. }
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TFTPFileSystem::DummyReadDirectory(const UnicodeString & Directory)
  725. {
  726. std::unique_ptr<TRemoteDirectory> Files(new TRemoteDirectory(FTerminal));
  727. try
  728. {
  729. Files->Directory = Directory;
  730. DoReadDirectory(Files.get());
  731. }
  732. catch(...)
  733. {
  734. // ignore non-fatal errors
  735. // (i.e. current directory may not exist anymore)
  736. if (!FTerminal->Active)
  737. {
  738. throw;
  739. }
  740. }
  741. }
  742. //---------------------------------------------------------------------------
  743. void __fastcall TFTPFileSystem::Idle()
  744. {
  745. if (FActive && !FWaitingForReply)
  746. {
  747. PoolForFatalNonCommandReply();
  748. // Keep session alive
  749. if ((FTerminal->SessionData->FtpPingType == fptDirectoryListing) &&
  750. (double(Now() - FLastDataSent) > double(FTerminal->SessionData->FtpPingIntervalDT) * 4))
  751. {
  752. FTerminal->LogEvent(L"Dummy directory read to keep session alive.");
  753. FLastDataSent = Now(); // probably redundant to the same statement in DoReadDirectory
  754. DummyReadDirectory(CurrentDirectory);
  755. }
  756. }
  757. }
  758. //---------------------------------------------------------------------------
  759. void __fastcall TFTPFileSystem::Discard()
  760. {
  761. // remove all pending messages, to get complete log
  762. // note that we need to retry discard on reconnect, as there still may be another
  763. // "disconnect/timeout/..." status messages coming
  764. DiscardMessages();
  765. DebugAssert(FActive);
  766. FActive = false;
  767. // See neon's ne_ssl_clicert_free
  768. if (FPrivateKey != NULL)
  769. {
  770. EVP_PKEY_free(FPrivateKey);
  771. FPrivateKey = NULL;
  772. }
  773. if (FCertificate != NULL)
  774. {
  775. X509_free(FCertificate);
  776. FCertificate = NULL;
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. UnicodeString __fastcall TFTPFileSystem::AbsolutePath(UnicodeString Path, bool /*Local*/)
  781. {
  782. // TODO: improve (handle .. etc.)
  783. if (UnixIsAbsolutePath(Path))
  784. {
  785. return Path;
  786. }
  787. else
  788. {
  789. return ::AbsolutePath(FCurrentDirectory, Path);
  790. }
  791. }
  792. //---------------------------------------------------------------------------
  793. UnicodeString __fastcall TFTPFileSystem::ActualCurrentDirectory()
  794. {
  795. wchar_t CurrentPath[1024];
  796. FFileZillaIntf->GetCurrentPath(CurrentPath, LENOF(CurrentPath));
  797. return UnixExcludeTrailingBackslash(CurrentPath);
  798. }
  799. //---------------------------------------------------------------------------
  800. void __fastcall TFTPFileSystem::EnsureLocation(const UnicodeString & Directory, bool Log)
  801. {
  802. UnicodeString ADirectory = UnixExcludeTrailingBackslash(Directory);
  803. if (!UnixSamePath(ActualCurrentDirectory(), ADirectory))
  804. {
  805. if (Log)
  806. {
  807. FTerminal->LogEvent(FORMAT(L"Synchronizing current directory \"%s\".",
  808. (ADirectory)));
  809. }
  810. DoChangeDirectory(ADirectory);
  811. // make sure FZAPI is aware that we changed current working directory
  812. FFileZillaIntf->SetCurrentPath(ADirectory.c_str());
  813. }
  814. }
  815. //---------------------------------------------------------------------------
  816. void __fastcall TFTPFileSystem::EnsureLocation()
  817. {
  818. // if we do not know what's the current directory, do nothing
  819. if (!FCurrentDirectory.IsEmpty())
  820. {
  821. // Make sure that the FZAPI current working directory,
  822. // is actually our working directory.
  823. // It may not be because:
  824. // 1) We did cached directory change
  825. // 2) Listing was requested for non-current directory, which
  826. // makes FZAPI change its current directory (and not restoring it back afterwards)
  827. EnsureLocation(FCurrentDirectory, true);
  828. }
  829. }
  830. //---------------------------------------------------------------------------
  831. bool TFTPFileSystem::EnsureLocationWhenWorkFromCwd(const UnicodeString & Directory)
  832. {
  833. bool Result = (FWorkFromCwd == asOn);
  834. if (Result)
  835. {
  836. EnsureLocation(Directory, false);
  837. }
  838. return Result;
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TFTPFileSystem::AnyCommand(const UnicodeString Command,
  842. TCaptureOutputEvent OutputEvent)
  843. {
  844. // end-user has right to expect that client current directory is really
  845. // current directory for the server
  846. EnsureLocation();
  847. DebugAssert(FOnCaptureOutput == NULL);
  848. FOnCaptureOutput = OutputEvent;
  849. try
  850. {
  851. SendCommand(Command);
  852. GotReply(WaitForCommandReply(), REPLY_2XX_CODE | REPLY_3XX_CODE);
  853. }
  854. __finally
  855. {
  856. FOnCaptureOutput = NULL;
  857. }
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TFTPFileSystem::ResetCaches()
  861. {
  862. delete FFileListCache;
  863. FFileListCache = NULL;
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TFTPFileSystem::AnnounceFileListOperation()
  867. {
  868. ResetCaches();
  869. }
  870. //---------------------------------------------------------------------------
  871. void TFTPFileSystem::SendCwd(const UnicodeString & Directory)
  872. {
  873. UnicodeString Command = FORMAT(L"CWD %s", (Directory));
  874. SendCommand(Command);
  875. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  876. }
  877. //---------------------------------------------------------------------------
  878. void __fastcall TFTPFileSystem::DoChangeDirectory(const UnicodeString & Directory)
  879. {
  880. if (FWorkFromCwd == asOn)
  881. {
  882. UnicodeString ADirectory = UnixIncludeTrailingBackslash(AbsolutePath(Directory, false));
  883. UnicodeString Actual = UnixIncludeTrailingBackslash(ActualCurrentDirectory());
  884. while (!UnixSamePath(Actual, ADirectory))
  885. {
  886. if (UnixIsChildPath(Actual, ADirectory))
  887. {
  888. UnicodeString SubDirectory = UnixExcludeTrailingBackslash(ADirectory);
  889. SubDirectory.Delete(1, Actual.Length());
  890. int P = SubDirectory.Pos(L'/');
  891. if (P > 0)
  892. {
  893. SubDirectory.SetLength(P - 1);
  894. }
  895. SendCwd(SubDirectory);
  896. Actual = UnixIncludeTrailingBackslash(Actual + SubDirectory);
  897. }
  898. else
  899. {
  900. SendCommand(L"CDUP");
  901. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  902. Actual = UnixExtractFilePath(UnixExcludeTrailingBackslash(Actual));
  903. }
  904. }
  905. }
  906. else
  907. {
  908. SendCwd(Directory);
  909. }
  910. }
  911. //---------------------------------------------------------------------------
  912. void __fastcall TFTPFileSystem::ChangeDirectory(const UnicodeString ADirectory)
  913. {
  914. UnicodeString Directory = ADirectory;
  915. try
  916. {
  917. // For changing directory, we do not make paths absolute, instead we
  918. // delegate this to the server, hence we synchronize current working
  919. // directory with the server and only then we ask for the change with
  920. // relative path.
  921. // But if synchronization fails, typically because current working directory
  922. // no longer exists, we fall back to out own resolution, to give
  923. // user chance to leave the non-existing directory.
  924. EnsureLocation();
  925. }
  926. catch(...)
  927. {
  928. if (FTerminal->Active)
  929. {
  930. Directory = AbsolutePath(Directory, false);
  931. }
  932. else
  933. {
  934. throw;
  935. }
  936. }
  937. DoChangeDirectory(Directory);
  938. // make next ReadCurrentDirectory retrieve actual server-side current directory
  939. FReadCurrentDirectory = true;
  940. }
  941. //---------------------------------------------------------------------------
  942. void __fastcall TFTPFileSystem::CachedChangeDirectory(const UnicodeString Directory)
  943. {
  944. FCurrentDirectory = UnixExcludeTrailingBackslash(Directory);
  945. FReadCurrentDirectory = false;
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TFTPFileSystem::ChangeFileProperties(const UnicodeString AFileName,
  949. const TRemoteFile * File, const TRemoteProperties * Properties,
  950. TChmodSessionAction & Action)
  951. {
  952. DebugAssert(Properties);
  953. DebugAssert(!Properties->Valid.Contains(vpGroup));
  954. DebugAssert(!Properties->Valid.Contains(vpOwner));
  955. DebugAssert(!Properties->Valid.Contains(vpLastAccess));
  956. DebugAssert(!Properties->Valid.Contains(vpModification));
  957. if (Properties->Valid.Contains(vpRights))
  958. {
  959. TRemoteFile * OwnedFile = NULL;
  960. try
  961. {
  962. UnicodeString FileName = AbsolutePath(AFileName, false);
  963. if (File == NULL)
  964. {
  965. ReadFile(FileName, OwnedFile);
  966. File = OwnedFile;
  967. }
  968. if ((File != NULL) && File->IsDirectory && FTerminal->CanRecurseToDirectory(File) && Properties->Recursive)
  969. {
  970. try
  971. {
  972. FTerminal->ProcessDirectory(AFileName, FTerminal->ChangeFileProperties,
  973. (void*)Properties);
  974. }
  975. catch(...)
  976. {
  977. Action.Cancel();
  978. throw;
  979. }
  980. }
  981. TRights Rights;
  982. if (File != NULL)
  983. {
  984. Rights = *File->Rights;
  985. }
  986. Rights |= Properties->Rights.NumberSet;
  987. Rights &= (unsigned short)~Properties->Rights.NumberUnset;
  988. if ((File != NULL) && File->IsDirectory && Properties->AddXToDirectories)
  989. {
  990. Rights.AddExecute();
  991. }
  992. Action.Rights(Rights);
  993. UnicodeString FileNameOnly = UnixExtractFileName(FileName);
  994. UnicodeString FilePath = RemoteExtractFilePath(FileName);
  995. // FZAPI wants octal number represented as decadic
  996. FFileZillaIntf->Chmod(Rights.NumberDecadic, FileNameOnly.c_str(), FilePath.c_str());
  997. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  998. }
  999. __finally
  1000. {
  1001. delete OwnedFile;
  1002. }
  1003. }
  1004. else
  1005. {
  1006. Action.Cancel();
  1007. }
  1008. }
  1009. //---------------------------------------------------------------------------
  1010. bool __fastcall TFTPFileSystem::LoadFilesProperties(TStrings * /*FileList*/)
  1011. {
  1012. DebugFail();
  1013. return false;
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. UnicodeString __fastcall TFTPFileSystem::DoCalculateFileChecksum(const UnicodeString & Alg, TRemoteFile * File)
  1017. {
  1018. // Overview of server supporting various hash commands is at:
  1019. // https://datatracker.ietf.org/doc/html/draft-bryan-ftpext-hash-02#appendix-B
  1020. UnicodeString CommandName;
  1021. bool UsingHashCommand = UsingHashCommandChecksum(Alg);
  1022. if (UsingHashCommand)
  1023. {
  1024. CommandName = HashCommand;
  1025. }
  1026. else
  1027. {
  1028. int Index = FChecksumAlgs->IndexOf(Alg);
  1029. if (Index < 0)
  1030. {
  1031. DebugFail();
  1032. EXCEPTION;
  1033. }
  1034. else
  1035. {
  1036. CommandName = FChecksumCommands->Strings[Index];
  1037. }
  1038. }
  1039. UnicodeString FileName = File->FullFileName;
  1040. // FTP way is not to quote.
  1041. // But as Serv-U, GlobalSCAPE and possibly others allow
  1042. // additional parameters (SP ER range), they need to quote file name.
  1043. // Cerberus and FileZilla Server on the other hand can do without quotes
  1044. // (but they can handle them, not sure about other servers)
  1045. // Quoting:
  1046. // FileZilla Server simply checks if argument starts and ends with double-quote
  1047. // and strips them, no double-quote escaping is possible.
  1048. // That's for all commands, not just HASH
  1049. // ProFTPD: TODO: Check how "SITE SYMLINK target link" is parsed
  1050. // We can possibly autodetect this from announced command format:
  1051. // XCRC filename;start;end
  1052. // XMD5 filename;start;end
  1053. // XSHA1 filename;start;end
  1054. // XSHA256 filename;start;end
  1055. // XSHA512 filename;start;end
  1056. if (FileName.Pos(L" ") > 0)
  1057. {
  1058. FileName = FORMAT(L"\"%s\"", (FileName));
  1059. }
  1060. UnicodeString Command = FORMAT(L"%s %s", (CommandName, FileName));
  1061. SendCommand(Command);
  1062. TStrings * Response;
  1063. GotReply(WaitForCommandReply(), REPLY_2XX_CODE, EmptyStr, NULL, &Response);
  1064. UnicodeString ResponseText;
  1065. if (DebugAlwaysTrue(Response->Count > 0))
  1066. {
  1067. // ProFTPD response has this format:
  1068. // 213-Computing MD5 digest
  1069. // 213 MD5 0-687104 b359479cfda703b7fd473c7e09f39049 filename
  1070. ResponseText = Response->Strings[Response->Count - 1];
  1071. }
  1072. delete Response;
  1073. UnicodeString Hash;
  1074. if (UsingHashCommand)
  1075. {
  1076. // Code should be 213, but let's be tolerant and accept any 2xx
  1077. // ("213" SP) hashname SP start-point "-" end-point SP filehash SP <pathname> (CRLF)
  1078. UnicodeString Buf = ResponseText;
  1079. // skip alg
  1080. CutToChar(Buf, L' ', true);
  1081. // skip range
  1082. UnicodeString Range = CutToChar(Buf, L' ', true);
  1083. // This should be range (SP-EP), but if it does not conform to the format,
  1084. // it's likely because the server uses version of the HASH spec
  1085. // before draft-ietf-ftpext2-hash-01
  1086. // (including draft-bryan-ftp-hash-06 implemented by FileZilla server; or Cerberus),
  1087. // that did not have the "range" part.
  1088. // The FileZilla Server even omits the file name.
  1089. // The latest draft as of implementing this is draft-bryan-ftpext-hash-02.
  1090. if (Range.Pos(L"-") > 0)
  1091. {
  1092. Hash = CutToChar(Buf, L' ', true);
  1093. }
  1094. else
  1095. {
  1096. Hash = Range;
  1097. }
  1098. }
  1099. else // All hash-specific commands
  1100. {
  1101. // Accepting any 2xx response. Most servers use 213,
  1102. // but for example WS_FTP uses non-sense code 220 (Service ready for new user)
  1103. // MD5 response according to a draft-twine-ftpmd5-00 includes a file name
  1104. // (implemented by Apache FtpServer).
  1105. // Other commands (X<hash>) return the hash only.
  1106. ResponseText = ResponseText.Trim();
  1107. int P = ResponseText.LastDelimiter(L" ");
  1108. if (P > 0)
  1109. {
  1110. ResponseText.Delete(1, P);
  1111. }
  1112. Hash = ResponseText;
  1113. }
  1114. if (Hash.IsEmpty())
  1115. {
  1116. throw Exception(FMTLOAD(FTP_RESPONSE_ERROR, (CommandName, ResponseText)));
  1117. }
  1118. return LowerCase(Hash);
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TFTPFileSystem::CalculateFilesChecksum(
  1122. const UnicodeString & Alg, TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  1123. TFileOperationProgressType * OperationProgress, bool FirstLevel)
  1124. {
  1125. FTerminal->CalculateSubFoldersChecksum(Alg, FileList, OnCalculatedChecksum, OperationProgress, FirstLevel);
  1126. int Index = 0;
  1127. TOnceDoneOperation OnceDoneOperation; // not used
  1128. while ((Index < FileList->Count) && !OperationProgress->Cancel)
  1129. {
  1130. TRemoteFile * File = (TRemoteFile *)FileList->Objects[Index];
  1131. DebugAssert(File != NULL);
  1132. if (!File->IsDirectory)
  1133. {
  1134. TChecksumSessionAction Action(FTerminal->ActionLog);
  1135. try
  1136. {
  1137. OperationProgress->SetFile(File->FileName);
  1138. Action.FileName(File->FullFileName);
  1139. bool Success = false;
  1140. try
  1141. {
  1142. UnicodeString Checksum = DoCalculateFileChecksum(Alg, File);
  1143. if (OnCalculatedChecksum != NULL)
  1144. {
  1145. OnCalculatedChecksum(File->FileName, Alg, Checksum);
  1146. }
  1147. Action.Checksum(Alg, Checksum);
  1148. Success = true;
  1149. }
  1150. __finally
  1151. {
  1152. if (FirstLevel)
  1153. {
  1154. OperationProgress->Finish(File->FileName, Success, OnceDoneOperation);
  1155. }
  1156. }
  1157. }
  1158. catch (Exception & E)
  1159. {
  1160. FTerminal->RollbackAction(Action, OperationProgress, &E);
  1161. // Error formatting expanded from inline to avoid strange exceptions
  1162. UnicodeString Error = FMTLOAD(CHECKSUM_ERROR, (File->FullFileName));
  1163. FTerminal->CommandError(&E, Error);
  1164. // Abort loop.
  1165. // TODO: retries? resume?
  1166. Index = FileList->Count;
  1167. }
  1168. }
  1169. Index++;
  1170. }
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. UnicodeString TFTPFileSystem::CalculateFilesChecksumInitialize(const UnicodeString & Alg)
  1174. {
  1175. UnicodeString NormalizedAlg = FindIdent(FindIdent(Alg, FHashAlgs.get()), FChecksumAlgs.get());
  1176. if (UsingHashCommandChecksum(NormalizedAlg))
  1177. {
  1178. // The server should understand lowercase alg name by spec,
  1179. // but we should use uppercase anyway
  1180. SendCommand(FORMAT(L"OPTS %s %s", (HashCommand, UpperCase(NormalizedAlg))));
  1181. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  1182. }
  1183. else if (FChecksumAlgs->IndexOf(NormalizedAlg) >= 0)
  1184. {
  1185. // will use algorithm-specific command
  1186. }
  1187. else
  1188. {
  1189. throw Exception(FMTLOAD(UNKNOWN_CHECKSUM, (Alg)));
  1190. }
  1191. return NormalizedAlg;
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. bool TFTPFileSystem::UsingHashCommandChecksum(const UnicodeString & Alg)
  1195. {
  1196. return (FHashAlgs->IndexOf(Alg) >= 0);
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. bool __fastcall TFTPFileSystem::ConfirmOverwrite(
  1200. const UnicodeString & SourceFullFileName, UnicodeString & TargetFileName,
  1201. TOverwriteMode & OverwriteMode, TFileOperationProgressType * OperationProgress,
  1202. const TOverwriteFileParams * FileParams, const TCopyParamType * CopyParam,
  1203. int Params, bool AutoResume)
  1204. {
  1205. bool Result;
  1206. bool CanAutoResume = FLAGSET(Params, cpNoConfirmation) && AutoResume;
  1207. bool DestIsSmaller = (FileParams != NULL) && (FileParams->DestSize < FileParams->SourceSize);
  1208. bool DestIsSame = (FileParams != NULL) && (FileParams->DestSize == FileParams->SourceSize);
  1209. bool CanResume =
  1210. !OperationProgress->AsciiTransfer &&
  1211. // when resuming transfer after interrupted connection,
  1212. // do nothing (dummy resume) when the files has the same size.
  1213. // this is workaround for servers that strangely fails just after successful
  1214. // upload.
  1215. (DestIsSmaller || (DestIsSame && CanAutoResume));
  1216. unsigned int Answer;
  1217. if (CanAutoResume && CanResume)
  1218. {
  1219. if (DestIsSame)
  1220. {
  1221. DebugAssert(CanAutoResume);
  1222. Answer = qaSkip;
  1223. }
  1224. else
  1225. {
  1226. Answer = qaRetry;
  1227. }
  1228. }
  1229. else
  1230. {
  1231. // retry = "resume"
  1232. // all = "yes to newer"
  1233. // ignore = "rename"
  1234. int Answers = qaYes | qaNo | qaCancel | qaYesToAll | qaNoToAll | qaAll | qaIgnore;
  1235. if (CanResume)
  1236. {
  1237. Answers |= qaRetry;
  1238. }
  1239. TQueryButtonAlias Aliases[5];
  1240. Aliases[0].Button = qaRetry;
  1241. Aliases[0].Alias = LoadStr(RESUME_BUTTON);
  1242. Aliases[0].GroupWith = qaNo;
  1243. Aliases[0].GrouppedShiftState = TShiftState() << ssAlt;
  1244. Aliases[1] = TQueryButtonAlias::CreateAllAsYesToNewerGrouppedWithYes();
  1245. Aliases[2] = TQueryButtonAlias::CreateIgnoreAsRenameGrouppedWithNo();
  1246. Aliases[3] = TQueryButtonAlias::CreateYesToAllGrouppedWithYes();
  1247. Aliases[4] = TQueryButtonAlias::CreateNoToAllGrouppedWithNo();
  1248. TQueryParams QueryParams(qpNeverAskAgainCheck);
  1249. QueryParams.Aliases = Aliases;
  1250. QueryParams.AliasesCount = LENOF(Aliases);
  1251. {
  1252. TSuspendFileOperationProgress Suspend(OperationProgress);
  1253. Answer = FTerminal->ConfirmFileOverwrite(
  1254. SourceFullFileName, TargetFileName, FileParams, Answers, &QueryParams,
  1255. ReverseOperationSide(OperationProgress->Side),
  1256. CopyParam, Params, OperationProgress);
  1257. }
  1258. }
  1259. Result = true;
  1260. switch (Answer)
  1261. {
  1262. // resume
  1263. case qaRetry:
  1264. OverwriteMode = omResume;
  1265. DebugAssert(FileParams != NULL);
  1266. DebugAssert(CanResume);
  1267. FFileTransferResumed = FileParams->DestSize;
  1268. break;
  1269. // rename
  1270. case qaIgnore:
  1271. if (FTerminal->PromptUser(FTerminal->SessionData, pkFileName,
  1272. LoadStr(RENAME_TITLE), L"", LoadStr(RENAME_PROMPT2), true, 0, TargetFileName))
  1273. {
  1274. OverwriteMode = omOverwrite;
  1275. }
  1276. else
  1277. {
  1278. OperationProgress->SetCancelAtLeast(csCancel);
  1279. FFileTransferAbort = ftaCancel;
  1280. Result = false;
  1281. }
  1282. break;
  1283. case qaYes:
  1284. OverwriteMode = omOverwrite;
  1285. break;
  1286. case qaCancel:
  1287. OperationProgress->SetCancelAtLeast(csCancel);
  1288. FFileTransferAbort = ftaCancel;
  1289. Result = false;
  1290. break;
  1291. case qaNo:
  1292. FFileTransferAbort = ftaSkip;
  1293. Result = false;
  1294. break;
  1295. case qaSkip:
  1296. OverwriteMode = omComplete;
  1297. break;
  1298. default:
  1299. DebugFail();
  1300. Result = false;
  1301. break;
  1302. }
  1303. return Result;
  1304. }
  1305. //---------------------------------------------------------------------------
  1306. void __fastcall TFTPFileSystem::ResetFileTransfer()
  1307. {
  1308. FFileTransferAbort = ftaNone;
  1309. FFileTransferCancelled = false;
  1310. FFileTransferResumed = 0;
  1311. }
  1312. //---------------------------------------------------------------------------
  1313. void __fastcall TFTPFileSystem::ReadDirectoryProgress(__int64 Bytes)
  1314. {
  1315. // with FTP we do not know exactly how many entries we have received,
  1316. // instead we know number of bytes received only.
  1317. // so we report approximation based on average size of entry.
  1318. int Progress = int(Bytes / 80);
  1319. DWORD Ticks = GetTickCount();
  1320. if ((Ticks - FLastReadDirectoryProgress >= 100) &&
  1321. // Cannot call OnReadDirectoryProgress with 0 as it would unmatch the "starting" and "ending" signals for disabling the window
  1322. (Progress > 0))
  1323. {
  1324. FLastReadDirectoryProgress = Ticks;
  1325. bool Cancel = false;
  1326. FTerminal->DoReadDirectoryProgress(Progress, 0, Cancel);
  1327. if (Cancel)
  1328. {
  1329. FTerminal->DoReadDirectoryProgress(-2, 0, Cancel);
  1330. FFileZillaIntf->Cancel();
  1331. }
  1332. }
  1333. }
  1334. //---------------------------------------------------------------------------
  1335. void __fastcall TFTPFileSystem::DoFileTransferProgress(__int64 TransferSize,
  1336. __int64 Bytes)
  1337. {
  1338. TFileOperationProgressType * OperationProgress = FTerminal->OperationProgress;
  1339. OperationProgress->SetTransferSize(TransferSize);
  1340. if (FFileTransferResumed > 0)
  1341. {
  1342. // Bytes will be 0, if resume was not possible
  1343. if (Bytes >= FFileTransferResumed)
  1344. {
  1345. OperationProgress->AddResumed(FFileTransferResumed);
  1346. }
  1347. FFileTransferResumed = 0;
  1348. }
  1349. __int64 Diff = Bytes - OperationProgress->TransferredSize;
  1350. if (DebugAlwaysTrue(Diff >= 0))
  1351. {
  1352. OperationProgress->AddTransferred(Diff);
  1353. FFileTransferAny = true;
  1354. }
  1355. if (OperationProgress->Cancel != csContinue)
  1356. {
  1357. if (OperationProgress->ClearCancelFile())
  1358. {
  1359. FFileTransferAbort = ftaSkip;
  1360. }
  1361. else
  1362. {
  1363. FFileTransferAbort = ftaCancel;
  1364. }
  1365. FFileTransferCancelled = true;
  1366. FFileZillaIntf->Cancel();
  1367. }
  1368. if (FFileTransferCPSLimit != OperationProgress->CPSLimit)
  1369. {
  1370. SetCPSLimit(OperationProgress);
  1371. }
  1372. }
  1373. //---------------------------------------------------------------------------
  1374. void __fastcall TFTPFileSystem::SetCPSLimit(TFileOperationProgressType * OperationProgress)
  1375. {
  1376. // Any reason we use separate field intead of directly using OperationProgress->CPSLimit?
  1377. // Maybe thread-safety?
  1378. FFileTransferCPSLimit = OperationProgress->CPSLimit;
  1379. OperationProgress->SetSpeedCounters();
  1380. }
  1381. //---------------------------------------------------------------------------
  1382. void __fastcall TFTPFileSystem::FileTransferProgress(__int64 TransferSize,
  1383. __int64 Bytes)
  1384. {
  1385. TGuard Guard(FTransferStatusCriticalSection);
  1386. DoFileTransferProgress(TransferSize, Bytes);
  1387. }
  1388. //---------------------------------------------------------------------------
  1389. void __fastcall TFTPFileSystem::FileTransfer(const UnicodeString & FileName,
  1390. const UnicodeString & LocalFile, const UnicodeString & RemoteFile,
  1391. const UnicodeString & RemotePath, bool Get, __int64 Size, int Type,
  1392. TFileTransferData & UserData, TFileOperationProgressType * OperationProgress)
  1393. {
  1394. FILE_OPERATION_LOOP_BEGIN
  1395. {
  1396. FFileZillaIntf->FileTransfer(
  1397. ApiPath(LocalFile).c_str(), RemoteFile.c_str(), RemotePath.c_str(),
  1398. Get, Size, Type, &UserData, UserData.CopyParam->OnTransferOut, UserData.CopyParam->OnTransferIn);
  1399. // we may actually catch response code of the listing
  1400. // command (when checking for existence of the remote file)
  1401. unsigned int Reply = WaitForCommandReply();
  1402. GotReply(Reply, FLAGMASK(FFileTransferCancelled, REPLY_ALLOW_CANCEL));
  1403. }
  1404. FILE_OPERATION_LOOP_END(FMTLOAD(TRANSFER_ERROR, (FileName)));
  1405. switch (FFileTransferAbort)
  1406. {
  1407. case ftaSkip:
  1408. throw ESkipFile();
  1409. case ftaCancel:
  1410. Abort();
  1411. break;
  1412. }
  1413. if (!FFileTransferCancelled)
  1414. {
  1415. // show completion of transfer
  1416. // call non-guarded variant to avoid deadlock with keepalives
  1417. // (we are not waiting for reply anymore so keepalives are free to proceed)
  1418. DoFileTransferProgress(OperationProgress->TransferSize, OperationProgress->TransferSize);
  1419. FAnyTransferSucceeded = true;
  1420. }
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. void __fastcall TFTPFileSystem::CopyToLocal(TStrings * FilesToCopy,
  1424. const UnicodeString TargetDir, const TCopyParamType * CopyParam,
  1425. int Params, TFileOperationProgressType * OperationProgress,
  1426. TOnceDoneOperation & OnceDoneOperation)
  1427. {
  1428. Params &= ~cpAppend;
  1429. FTerminal->DoCopyToLocal(
  1430. FilesToCopy, TargetDir, CopyParam, Params, OperationProgress, tfUseFileTransferAny, OnceDoneOperation);
  1431. }
  1432. //---------------------------------------------------------------------------
  1433. UnicodeString TFTPFileSystem::RemoteExtractFilePath(const UnicodeString & Path)
  1434. {
  1435. UnicodeString Result;
  1436. // If the path ends with a slash, FZAPI CServerPath constructor does not identify the path as VMS.
  1437. // It is probably ok to use UnixExtractFileDir for all paths passed to FZAPI,
  1438. // but for now, we limit the impact of the change to VMS.
  1439. if (FVMS)
  1440. {
  1441. Result = UnixExtractFileDir(Path);
  1442. }
  1443. else
  1444. {
  1445. Result = UnixExtractFilePath(Path);
  1446. }
  1447. return Result;
  1448. }
  1449. //---------------------------------------------------------------------------
  1450. void __fastcall TFTPFileSystem::Sink(
  1451. const UnicodeString & FileName, const TRemoteFile * File,
  1452. const UnicodeString & TargetDir, UnicodeString & DestFileName, int Attrs,
  1453. const TCopyParamType * CopyParam, int Params, TFileOperationProgressType * OperationProgress,
  1454. unsigned int Flags, TDownloadSessionAction & Action)
  1455. {
  1456. AutoDetectTimeDifference(UnixExtractFileDir(FileName), CopyParam, Params);
  1457. ResetFileTransfer();
  1458. TFileTransferData UserData;
  1459. UnicodeString DestFullName = TargetDir + DestFileName;
  1460. UnicodeString FilePath = RemoteExtractFilePath(FileName);
  1461. unsigned int TransferType = (OperationProgress->AsciiTransfer ? 1 : 2);
  1462. UnicodeString AFileName;
  1463. UnicodeString OnlyFileName = UnixExtractFileName(FileName);
  1464. if (EnsureLocationWhenWorkFromCwd(FilePath))
  1465. {
  1466. AFileName = OnlyFileName;
  1467. FilePath = EmptyStr;
  1468. }
  1469. else
  1470. {
  1471. AFileName = FileName;
  1472. }
  1473. {
  1474. // ignore file list
  1475. TFileListHelper Helper(this, NULL, true);
  1476. SetCPSLimit(OperationProgress);
  1477. FFileTransferPreserveTime = CopyParam->PreserveTime;
  1478. // not used for downloads anyway
  1479. FFileTransferRemoveBOM = CopyParam->RemoveBOM;
  1480. FFileTransferNoList = CanTransferSkipList(Params, Flags, CopyParam);
  1481. UserData.FileName = DestFileName;
  1482. UserData.Params = Params;
  1483. UserData.AutoResume = FLAGSET(Flags, tfAutoResume);
  1484. UserData.CopyParam = CopyParam;
  1485. UserData.Modification = File->Modification;
  1486. FileTransfer(AFileName, DestFullName, OnlyFileName,
  1487. FilePath, true, File->Size, TransferType, UserData, OperationProgress);
  1488. }
  1489. // in case dest filename is changed from overwrite dialog
  1490. if (DestFileName != UserData.FileName)
  1491. {
  1492. DestFullName = TargetDir + UserData.FileName;
  1493. Attrs = FileGetAttrFix(ApiPath(DestFullName));
  1494. }
  1495. UnicodeString ExpandedDestFullName = ExpandUNCFileName(DestFullName);
  1496. Action.Destination(ExpandedDestFullName);
  1497. if (CopyParam->OnTransferOut == NULL)
  1498. {
  1499. FTerminal->UpdateTargetAttrs(DestFullName, File, CopyParam, Attrs);
  1500. }
  1501. FLastDataSent = Now();
  1502. }
  1503. //---------------------------------------------------------------------------
  1504. void __fastcall TFTPFileSystem::TransferOnDirectory(
  1505. const UnicodeString & Directory, const TCopyParamType * CopyParam, int Params)
  1506. {
  1507. AutoDetectTimeDifference(Directory, CopyParam, Params);
  1508. }
  1509. //---------------------------------------------------------------------------
  1510. void __fastcall TFTPFileSystem::CopyToRemote(TStrings * FilesToCopy,
  1511. const UnicodeString TargetDir, const TCopyParamType * CopyParam,
  1512. int Params, TFileOperationProgressType * OperationProgress,
  1513. TOnceDoneOperation & OnceDoneOperation)
  1514. {
  1515. Params &= ~cpAppend;
  1516. FTerminal->DoCopyToRemote(FilesToCopy, TargetDir, CopyParam, Params, OperationProgress, tfUseFileTransferAny, OnceDoneOperation);
  1517. }
  1518. //---------------------------------------------------------------------------
  1519. bool __fastcall TFTPFileSystem::CanTransferSkipList(int Params, unsigned int Flags, const TCopyParamType * CopyParam)
  1520. {
  1521. bool Result =
  1522. (CopyParam->OnTransferIn != NULL) ||
  1523. (FLAGSET(Params, cpNoConfirmation) &&
  1524. // cpAppend is not supported with FTP
  1525. DebugAlwaysTrue(FLAGCLEAR(Params, cpAppend)) &&
  1526. FLAGCLEAR(Params, cpResume) &&
  1527. FLAGCLEAR(Flags, tfAutoResume) &&
  1528. !CopyParam->NewerOnly);
  1529. return Result;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TFTPFileSystem::Source(
  1533. TLocalFileHandle & Handle, const UnicodeString & TargetDir, UnicodeString & DestFileName,
  1534. const TCopyParamType * CopyParam, int Params,
  1535. TFileOperationProgressType * OperationProgress, unsigned int Flags,
  1536. TUploadSessionAction & Action, bool & /*ChildError*/)
  1537. {
  1538. if (CopyParam->OnTransferIn == NULL)
  1539. {
  1540. Handle.Close();
  1541. }
  1542. ResetFileTransfer();
  1543. TFileTransferData UserData;
  1544. unsigned int TransferType = (OperationProgress->AsciiTransfer ? 1 : 2);
  1545. EnsureLocationWhenWorkFromCwd(TargetDir);
  1546. {
  1547. // ignore file list
  1548. TFileListHelper Helper(this, NULL, true);
  1549. SetCPSLimit(OperationProgress);
  1550. // not used for uploads anyway
  1551. FFileTransferPreserveTime = CopyParam->PreserveTime && (CopyParam->OnTransferIn == NULL);
  1552. FFileTransferRemoveBOM = CopyParam->RemoveBOM;
  1553. FFileTransferNoList = CanTransferSkipList(Params, Flags, CopyParam);
  1554. // not used for uploads, but we get new name (if any) back in this field
  1555. UserData.FileName = DestFileName;
  1556. UserData.Params = Params;
  1557. UserData.AutoResume = FLAGSET(Flags, tfAutoResume);
  1558. UserData.CopyParam = CopyParam;
  1559. UserData.Modification = Handle.Modification;
  1560. FileTransfer(Handle.FileName, Handle.FileName, DestFileName,
  1561. TargetDir, false, Handle.Size, TransferType, UserData, OperationProgress);
  1562. }
  1563. UnicodeString DestFullName = TargetDir + UserData.FileName;
  1564. // only now, we know the final destination
  1565. Action.Destination(DestFullName);
  1566. // We are not able to tell if setting timestamp succeeded,
  1567. // so we log it always (if supported).
  1568. // Support for MDTM does not necessarily mean that the server supports
  1569. // non-standard hack of setting timestamp using
  1570. // MFMT-like (two argument) call to MDTM.
  1571. // IIS definitely does.
  1572. if (FFileTransferPreserveTime &&
  1573. ((FServerCapabilities->GetCapability(mfmt_command) == yes) ||
  1574. ((FServerCapabilities->GetCapability(mdtm_command) == yes))))
  1575. {
  1576. TTouchSessionAction TouchAction(FTerminal->ActionLog, DestFullName, Handle.Modification);
  1577. if (!FFileZillaIntf->UsingMlsd())
  1578. {
  1579. FUploadedTimes[DestFullName] = Handle.Modification;
  1580. if ((FTerminal->Configuration->ActualLogProtocol >= 2))
  1581. {
  1582. FTerminal->LogEvent(
  1583. FORMAT(L"Remembering modification time of \"%s\" as [%s]",
  1584. (DestFullName, StandardTimestamp(FUploadedTimes[DestFullName]))));
  1585. }
  1586. }
  1587. }
  1588. FLastDataSent = Now();
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. void __fastcall TFTPFileSystem::CreateDirectory(const UnicodeString & ADirName, bool /*Encrypt*/)
  1592. {
  1593. UnicodeString DirName = AbsolutePath(ADirName, false);
  1594. {
  1595. // ignore file list
  1596. TFileListHelper Helper(this, NULL, true);
  1597. FFileZillaIntf->MakeDir(DirName.c_str());
  1598. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  1599. }
  1600. }
  1601. //---------------------------------------------------------------------------
  1602. void __fastcall TFTPFileSystem::CreateLink(const UnicodeString FileName,
  1603. const UnicodeString PointTo, bool Symbolic)
  1604. {
  1605. DebugAssert(SupportsSiteCommand(SymlinkSiteCommand));
  1606. if (DebugAlwaysTrue(Symbolic))
  1607. {
  1608. EnsureLocation();
  1609. UnicodeString Command = FORMAT(L"%s %s %s %s", (SiteCommand, SymlinkSiteCommand, PointTo, FileName));
  1610. SendCommand(Command);
  1611. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  1612. }
  1613. }
  1614. //---------------------------------------------------------------------------
  1615. void __fastcall TFTPFileSystem::DeleteFile(const UnicodeString AFileName,
  1616. const TRemoteFile * File, int Params, TRmSessionAction & Action)
  1617. {
  1618. UnicodeString FileName = AbsolutePath(AFileName, false);
  1619. UnicodeString FileNameOnly = UnixExtractFileName(FileName);
  1620. UnicodeString FilePath = RemoteExtractFilePath(FileName);
  1621. bool Dir = FTerminal->DeleteContentsIfDirectory(FileName, File, Params, Action);
  1622. {
  1623. // ignore file list
  1624. TFileListHelper Helper(this, NULL, true);
  1625. if (Dir)
  1626. {
  1627. // If current remote directory is in the directory being removed,
  1628. // some servers may refuse to delete it
  1629. // This is common as ProcessDirectory above would CWD to
  1630. // the directory to LIST it.
  1631. // EnsureLocation should reset actual current directory to user's working directory.
  1632. // If user's working directory is still below deleted directory, it is
  1633. // perfectly correct to report an error.
  1634. if (UnixIsChildPath(ActualCurrentDirectory(), FileName))
  1635. {
  1636. EnsureLocation();
  1637. }
  1638. FFileZillaIntf->RemoveDir(FileNameOnly.c_str(), FilePath.c_str());
  1639. }
  1640. else
  1641. {
  1642. if (EnsureLocationWhenWorkFromCwd(FilePath))
  1643. {
  1644. FFileZillaIntf->Delete(FileNameOnly.c_str(), L"", true);
  1645. }
  1646. else
  1647. {
  1648. FFileZillaIntf->Delete(FileNameOnly.c_str(), FilePath.c_str(), false);
  1649. }
  1650. }
  1651. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  1652. }
  1653. }
  1654. //---------------------------------------------------------------------------
  1655. void __fastcall TFTPFileSystem::CustomCommandOnFile(const UnicodeString /*FileName*/,
  1656. const TRemoteFile * /*File*/, UnicodeString /*Command*/, int /*Params*/,
  1657. TCaptureOutputEvent /*OutputEvent*/)
  1658. {
  1659. // if ever implemented, do not forget to add EnsureLocation,
  1660. // see AnyCommand for a reason why
  1661. DebugFail();
  1662. }
  1663. //---------------------------------------------------------------------------
  1664. void __fastcall TFTPFileSystem::DoStartup()
  1665. {
  1666. TStrings * PostLoginCommands = new TStringList();
  1667. try
  1668. {
  1669. PostLoginCommands->Text = FTerminal->SessionData->PostLoginCommands;
  1670. for (int Index = 0; Index < PostLoginCommands->Count; Index++)
  1671. {
  1672. UnicodeString Command = PostLoginCommands->Strings[Index];
  1673. if (!Command.IsEmpty())
  1674. {
  1675. SendCommand(Command);
  1676. GotReply(WaitForCommandReply(), REPLY_2XX_CODE | REPLY_3XX_CODE);
  1677. }
  1678. }
  1679. }
  1680. __finally
  1681. {
  1682. delete PostLoginCommands;
  1683. }
  1684. if (SupportsCommand(CsidCommand))
  1685. {
  1686. UnicodeString NameFact = L"Name";
  1687. UnicodeString VersionFact = L"Version";
  1688. UnicodeString Command =
  1689. FORMAT(L"%s %s=%s;%s=%s;", (CsidCommand, NameFact, AppNameString(), VersionFact, FTerminal->Configuration->Version));
  1690. SendCommand(Command);
  1691. TStrings * Response = NULL;
  1692. std::unique_ptr<TStrings> ResponseOwner(Response);
  1693. try
  1694. {
  1695. GotReply(WaitForCommandReply(), REPLY_2XX_CODE | REPLY_SINGLE_LINE, EmptyStr, NULL, &Response);
  1696. ResponseOwner.reset(Response);
  1697. }
  1698. catch (...)
  1699. {
  1700. if (FTerminal->Active)
  1701. {
  1702. FTerminal->LogEvent(FORMAT(L"%s command failed", (CsidCommand)));
  1703. }
  1704. else
  1705. {
  1706. throw;
  1707. }
  1708. }
  1709. if (ResponseOwner.get() != NULL)
  1710. {
  1711. UnicodeString ResponseText = Response->Strings[0];
  1712. UnicodeString Name, Version;
  1713. while (!ResponseText.IsEmpty())
  1714. {
  1715. UnicodeString Token = CutToChar(ResponseText, L';', true);
  1716. UnicodeString Fact = CutToChar(Token, L'=', true);
  1717. if (SameText(Fact, NameFact))
  1718. {
  1719. Name = Token;
  1720. }
  1721. else if (SameText(Fact, VersionFact))
  1722. {
  1723. Version = Token;
  1724. }
  1725. }
  1726. if (!Name.IsEmpty())
  1727. {
  1728. FServerID = Name;
  1729. AddToList(FServerID, Version, L" ");
  1730. FTerminal->LogEvent(FORMAT("Server: %s", (FServerID)));
  1731. }
  1732. }
  1733. }
  1734. // retrieve initialize working directory to save it as home directory
  1735. ReadCurrentDirectory();
  1736. FHomeDirectory = FCurrentDirectory;
  1737. }
  1738. //---------------------------------------------------------------------------
  1739. void __fastcall TFTPFileSystem::HomeDirectory()
  1740. {
  1741. // FHomeDirectory is an absolute path, so avoid unnecessary overhead
  1742. // of ChangeDirectory, such as EnsureLocation
  1743. DoChangeDirectory(FHomeDirectory);
  1744. FCurrentDirectory = FHomeDirectory;
  1745. FReadCurrentDirectory = false;
  1746. // make sure FZAPI is aware that we changed current working directory
  1747. FFileZillaIntf->SetCurrentPath(FCurrentDirectory.c_str());
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. bool __fastcall TFTPFileSystem::IsCapable(int Capability) const
  1751. {
  1752. DebugAssert(FTerminal);
  1753. switch (Capability)
  1754. {
  1755. case fcResolveSymlink: // sic
  1756. case fcTextMode:
  1757. case fcModeChanging: // but not fcModeChangingUpload
  1758. case fcNewerOnlyUpload:
  1759. case fcAnyCommand: // but not fcShellAnyCommand
  1760. case fcRename:
  1761. case fcRemoteMove:
  1762. case fcRemoveBOMUpload:
  1763. case fcMoveToQueue:
  1764. case fcSkipTransfer:
  1765. case fcParallelTransfers:
  1766. case fcTransferOut:
  1767. case fcTransferIn:
  1768. return true;
  1769. case fcPreservingTimestampUpload:
  1770. return (FServerCapabilities->GetCapability(mfmt_command) == yes);
  1771. case fcRemoteCopy:
  1772. return SupportsSiteCommand(CopySiteCommand);
  1773. case fcSymbolicLink:
  1774. return SupportsSiteCommand(SymlinkSiteCommand);
  1775. case fcCalculatingChecksum:
  1776. return FSupportsAnyChecksumFeature;
  1777. case fcCheckingSpaceAvailable:
  1778. return FBytesAvailableSupported || SupportsCommand(AvblCommand) || SupportsCommand(XQuotaCommand);
  1779. case fcMoveOverExistingFile:
  1780. return !FIIS;
  1781. case fcAclChangingFiles:
  1782. case fcModeChangingUpload:
  1783. case fcLoadingAdditionalProperties:
  1784. case fcShellAnyCommand:
  1785. case fcHardLink:
  1786. case fcUserGroupListing:
  1787. case fcGroupChanging:
  1788. case fcOwnerChanging:
  1789. case fcGroupOwnerChangingByID:
  1790. case fcSecondaryShell:
  1791. case fcNativeTextMode:
  1792. case fcTimestampChanging:
  1793. case fcIgnorePermErrors:
  1794. case fcRemoveCtrlZUpload:
  1795. case fcLocking:
  1796. case fcPreservingTimestampDirs:
  1797. case fcResumeSupport:
  1798. case fcChangePassword:
  1799. case fcParallelFileTransfers:
  1800. return false;
  1801. default:
  1802. DebugFail();
  1803. return false;
  1804. }
  1805. }
  1806. //---------------------------------------------------------------------------
  1807. void __fastcall TFTPFileSystem::LookupUsersGroups()
  1808. {
  1809. DebugFail();
  1810. }
  1811. //---------------------------------------------------------------------------
  1812. void __fastcall TFTPFileSystem::ReadCurrentDirectory()
  1813. {
  1814. // ask the server for current directory on startup only
  1815. // and immediately after call to CWD,
  1816. // later our current directory may be not synchronized with FZAPI current
  1817. // directory anyway, see comments in EnsureLocation
  1818. if (FReadCurrentDirectory || DebugAlwaysFalse(FCurrentDirectory.IsEmpty()))
  1819. {
  1820. UnicodeString Command = L"PWD";
  1821. SendCommand(Command);
  1822. unsigned int Code;
  1823. TStrings * Response = NULL;
  1824. GotReply(WaitForCommandReply(), REPLY_2XX_CODE, L"", &Code, &Response);
  1825. try
  1826. {
  1827. DebugAssert(Response != NULL);
  1828. bool Result = false;
  1829. // The 257 is the only allowed 2XX code to "PWD"
  1830. if (((Code == 257) || FTerminal->SessionData->FtpAnyCodeForPwd) &&
  1831. (Response->Count == 1))
  1832. {
  1833. UnicodeString Path = Response->Text;
  1834. int P = Path.Pos(L"\"");
  1835. if (P == 0)
  1836. {
  1837. // some systems use single quotes, be tolerant
  1838. P = Path.Pos(L"'");
  1839. }
  1840. if (P != 0)
  1841. {
  1842. Path.Delete(1, P - 1);
  1843. if (Unquote(Path))
  1844. {
  1845. Result = true;
  1846. }
  1847. }
  1848. else
  1849. {
  1850. P = Path.Pos(L" ");
  1851. Path.Delete(P, Path.Length() - P + 1);
  1852. Result = true;
  1853. }
  1854. if (Result)
  1855. {
  1856. if ((Path.Length() > 0) && !UnixIsAbsolutePath(Path))
  1857. {
  1858. Path = L"/" + Path;
  1859. }
  1860. FCurrentDirectory = UnixExcludeTrailingBackslash(Path);
  1861. FReadCurrentDirectory = false;
  1862. }
  1863. }
  1864. if (Result)
  1865. {
  1866. FFileZillaIntf->SetCurrentPath(FCurrentDirectory.c_str());
  1867. }
  1868. else
  1869. {
  1870. throw Exception(FMTLOAD(FTP_RESPONSE_ERROR, (Command, Response->Text)));
  1871. }
  1872. }
  1873. __finally
  1874. {
  1875. delete Response;
  1876. }
  1877. }
  1878. }
  1879. //---------------------------------------------------------------------------
  1880. void __fastcall TFTPFileSystem::DoReadDirectory(TRemoteFileList * FileList)
  1881. {
  1882. UnicodeString Directory;
  1883. if (!EnsureLocationWhenWorkFromCwd(FileList->Directory))
  1884. {
  1885. Directory = AbsolutePath(FileList->Directory, false);
  1886. }
  1887. FBytesAvailable = -1;
  1888. FileList->Reset();
  1889. // FZAPI does not list parent directory, add it
  1890. FileList->AddFile(new TRemoteParentDirectory(FTerminal));
  1891. FLastReadDirectoryProgress = 0;
  1892. TFileListHelper Helper(this, FileList, false);
  1893. // always specify path to list, do not attempt to list "current" dir as:
  1894. // 1) List() lists again the last listed directory, not the current working directory
  1895. // 2) we handle this way the cached directory change
  1896. FFileZillaIntf->List(Directory.c_str());
  1897. GotReply(WaitForCommandReply(), REPLY_2XX_CODE | REPLY_ALLOW_CANCEL);
  1898. AutoDetectTimeDifference(FileList);
  1899. if (!IsEmptyFileList(FileList))
  1900. {
  1901. CheckTimeDifference();
  1902. if ((FTimeDifference != 0) || !FUploadedTimes.empty())// optimization
  1903. {
  1904. for (int Index = 0; Index < FileList->Count; Index++)
  1905. {
  1906. ApplyTimeDifference(FileList->Files[Index]);
  1907. }
  1908. }
  1909. }
  1910. FLastDataSent = Now();
  1911. FAnyTransferSucceeded = true;
  1912. }
  1913. //---------------------------------------------------------------------------
  1914. void __fastcall TFTPFileSystem::CheckTimeDifference()
  1915. {
  1916. if (NeedAutoDetectTimeDifference())
  1917. {
  1918. FTerminal->LogEvent("Warning: Timezone difference was not detected yet, timestamps may be incorrect");
  1919. }
  1920. }
  1921. //---------------------------------------------------------------------------
  1922. void __fastcall TFTPFileSystem::ApplyTimeDifference(TRemoteFile * File)
  1923. {
  1924. DebugAssert(File->Modification == File->LastAccess);
  1925. File->ShiftTimeInSeconds(FTimeDifference);
  1926. TDateTime Modification = File->Modification;
  1927. if (LookupUploadModificationTime(File->FullFileName, Modification, File->ModificationFmt))
  1928. {
  1929. // implicitly sets ModificationFmt to mfFull
  1930. File->Modification = Modification;
  1931. }
  1932. }
  1933. //---------------------------------------------------------------------------
  1934. void __fastcall TFTPFileSystem::ApplyTimeDifference(
  1935. const UnicodeString & FileName, TDateTime & Modification, TModificationFmt & ModificationFmt)
  1936. {
  1937. CheckTimeDifference();
  1938. TRemoteFile::ShiftTimeInSeconds(Modification, ModificationFmt, FTimeDifference);
  1939. if (LookupUploadModificationTime(FileName, Modification, ModificationFmt))
  1940. {
  1941. ModificationFmt = mfFull;
  1942. }
  1943. }
  1944. //---------------------------------------------------------------------------
  1945. bool __fastcall TFTPFileSystem::LookupUploadModificationTime(
  1946. const UnicodeString & FileName, TDateTime & Modification, TModificationFmt ModificationFmt)
  1947. {
  1948. bool Result = false;
  1949. if (ModificationFmt != mfFull)
  1950. {
  1951. TUploadedTimes::iterator Iterator = FUploadedTimes.find(AbsolutePath(FileName, false));
  1952. if (Iterator != FUploadedTimes.end())
  1953. {
  1954. TDateTime UploadModification = Iterator->second;
  1955. TDateTime UploadModificationReduced = ReduceDateTimePrecision(UploadModification, ModificationFmt);
  1956. if (UploadModificationReduced == Modification)
  1957. {
  1958. if ((FTerminal->Configuration->ActualLogProtocol >= 2))
  1959. {
  1960. FTerminal->LogEvent(
  1961. FORMAT(L"Enriching modification time of \"%s\" from [%s] to [%s]",
  1962. (FileName, StandardTimestamp(Modification), StandardTimestamp(UploadModification))));
  1963. }
  1964. Modification = UploadModification;
  1965. Result = true;
  1966. }
  1967. else
  1968. {
  1969. if ((FTerminal->Configuration->ActualLogProtocol >= 2))
  1970. {
  1971. FTerminal->LogEvent(
  1972. FORMAT(L"Remembered modification time [%s]/[%s] of \"%s\" is obsolete, keeping [%s]",
  1973. (StandardTimestamp(UploadModification), StandardTimestamp(UploadModificationReduced), FileName, StandardTimestamp(Modification))));
  1974. }
  1975. FUploadedTimes.erase(Iterator);
  1976. }
  1977. }
  1978. }
  1979. return Result;
  1980. }
  1981. //---------------------------------------------------------------------------
  1982. bool __fastcall TFTPFileSystem::NeedAutoDetectTimeDifference()
  1983. {
  1984. return
  1985. FDetectTimeDifference &&
  1986. // Does not support MLST/MLSD, but supports MDTM at least
  1987. !FFileZillaIntf->UsingMlsd() && SupportsReadingFile();
  1988. }
  1989. //---------------------------------------------------------------------------
  1990. bool __fastcall TFTPFileSystem::IsEmptyFileList(TRemoteFileList * FileList)
  1991. {
  1992. return
  1993. // (note that it's actually never empty here, there's always at least parent directory,
  1994. // added explicitly by DoReadDirectory)
  1995. (FileList->Count == 0) ||
  1996. ((FileList->Count == 1) && FileList->Files[0]->IsParentDirectory);
  1997. }
  1998. //---------------------------------------------------------------------------
  1999. void __fastcall TFTPFileSystem::AutoDetectTimeDifference(TRemoteFileList * FileList)
  2000. {
  2001. if (NeedAutoDetectTimeDifference())
  2002. {
  2003. FTerminal->LogEvent(L"Detecting timezone difference...");
  2004. for (int Index = 0; Index < FileList->Count; Index++)
  2005. {
  2006. TRemoteFile * File = FileList->Files[Index];
  2007. // For directories, we do not do MDTM in ReadFile
  2008. // (it should not be problem to use them otherwise).
  2009. // We are also not interested in files with day precision only.
  2010. if (!File->IsDirectory && !File->IsSymLink &&
  2011. File->IsTimeShiftingApplicable())
  2012. {
  2013. TRemoteFile * UtcFile = NULL;
  2014. try
  2015. {
  2016. ReadFile(File->FullFileName, UtcFile);
  2017. }
  2018. catch (Exception & E)
  2019. {
  2020. FDetectTimeDifference = false;
  2021. if (!FTerminal->Active)
  2022. {
  2023. throw;
  2024. }
  2025. FTerminal->LogEvent(FORMAT(L"Failed to retrieve file %s attributes to detect timezone difference", (File->FullFileName)));
  2026. break;
  2027. }
  2028. TDateTime UtcModification = UtcFile->Modification;
  2029. delete UtcFile;
  2030. if (UtcModification > Now())
  2031. {
  2032. FTerminal->LogEvent(
  2033. FORMAT(L"Not using file %s to detect timezone difference as it has the timestamp in the future [%s]",
  2034. (File->FullFileName, StandardTimestamp(UtcModification))));
  2035. }
  2036. // "SecureLink FTP Proxy" succeeds CWD for a file, so we never get a timestamp here
  2037. else if (UtcModification == TDateTime())
  2038. {
  2039. FTerminal->LogEvent(
  2040. FORMAT(L"Not using file %s to detect timezone difference as its timestamp was not resolved",
  2041. (File->FullFileName)));
  2042. }
  2043. else
  2044. {
  2045. FDetectTimeDifference = false;
  2046. // MDTM returns seconds, trim those
  2047. UtcModification = ReduceDateTimePrecision(UtcModification, File->ModificationFmt);
  2048. // Time difference between timestamp retrieved using MDTM (UTC converted to local timezone)
  2049. // and using LIST (no conversion, expecting the server uses the same timezone as the client).
  2050. // Note that FormatTimeZone reverses the value.
  2051. FTimeDifference = static_cast<__int64>(SecsPerDay * (UtcModification - File->Modification));
  2052. double Hours = TTimeSpan::FromSeconds(FTimeDifference).TotalHours;
  2053. UnicodeString FileLog =
  2054. FORMAT(L"%s (Listing: %s, UTC: %s)", (File->FullFileName, StandardTimestamp(File->Modification), StandardTimestamp(UtcModification)));
  2055. UnicodeString LogMessage;
  2056. if (FTimeDifference == 0)
  2057. {
  2058. LogMessage = FORMAT(L"No timezone difference detected using file %s", (FileLog));
  2059. }
  2060. // Seen with "GamingDeluxe FTP Server", which returns "213 00010101000000"
  2061. else if (fabs(Hours) >= 48)
  2062. {
  2063. FTimeDifference = 0;
  2064. LogMessage = FORMAT(L"Ignoring suspicious timezone difference of %s hours, detected using file %s", (IntToStr(__int64(Hours)), FileLog));
  2065. }
  2066. else
  2067. {
  2068. LogMessage = FORMAT(L"Timezone difference of %s detected using file %s", (FormatTimeZone(FTimeDifference), FileLog));
  2069. }
  2070. FTerminal->LogEvent(LogMessage);
  2071. break;
  2072. }
  2073. }
  2074. }
  2075. if (FDetectTimeDifference)
  2076. {
  2077. FTerminal->LogEvent(L"Found no file to use for detecting timezone difference");
  2078. }
  2079. }
  2080. }
  2081. //---------------------------------------------------------------------------
  2082. void __fastcall TFTPFileSystem::AutoDetectTimeDifference(
  2083. const UnicodeString & Directory, const TCopyParamType * CopyParam, int Params)
  2084. {
  2085. if (NeedAutoDetectTimeDifference() &&
  2086. // do we need FTimeDifference for the operation?
  2087. // (tmAutomatic - AsciiFileMask can theoretically include time constraints, while it is unlikely)
  2088. (!FLAGSET(Params, cpNoConfirmation) ||
  2089. CopyParam->NewerOnly || (!CopyParam->TransferMode == tmAutomatic) || !CopyParam->IncludeFileMask.Masks.IsEmpty()))
  2090. {
  2091. FTerminal->LogEvent(L"Retrieving listing to detect timezone difference");
  2092. DummyReadDirectory(Directory);
  2093. }
  2094. }
  2095. //---------------------------------------------------------------------------
  2096. void __fastcall TFTPFileSystem::ReadDirectory(TRemoteFileList * FileList)
  2097. {
  2098. // whole below "-a" logic is for LIST,
  2099. // if we know we are going to use MLSD, skip it
  2100. if (FFileZillaIntf->UsingMlsd())
  2101. {
  2102. DoReadDirectory(FileList);
  2103. }
  2104. else
  2105. {
  2106. bool GotNoFilesForAll = false;
  2107. bool Repeat;
  2108. do
  2109. {
  2110. Repeat = false;
  2111. try
  2112. {
  2113. FDoListAll = (FListAll == asAuto) || (FListAll == asOn);
  2114. DoReadDirectory(FileList);
  2115. // We got no files with "-a", but again no files w/o "-a",
  2116. // so it was not "-a"'s problem, revert to auto and let it decide the next time
  2117. if (GotNoFilesForAll && (FileList->Count == 0))
  2118. {
  2119. DebugAssert(FListAll == asOff);
  2120. FListAll = asAuto;
  2121. }
  2122. else if (FListAll == asAuto)
  2123. {
  2124. // some servers take "-a" as a mask and return empty directory listing
  2125. if (IsEmptyFileList(FileList))
  2126. {
  2127. Repeat = true;
  2128. FListAll = asOff;
  2129. GotNoFilesForAll = true;
  2130. FTerminal->LogEvent(L"LIST with -a switch returned empty directory listing, will try pure LIST");
  2131. }
  2132. else
  2133. {
  2134. // reading first directory has succeeded, always use "-a"
  2135. FListAll = asOn;
  2136. }
  2137. }
  2138. // use "-a" even for implicit directory reading by FZAPI?
  2139. // (e.g. before file transfer)
  2140. // Note that FZAPI ignores this for VMS/MVS.
  2141. FDoListAll = (FListAll == asOn);
  2142. }
  2143. catch(Exception & E)
  2144. {
  2145. FDoListAll = false;
  2146. // reading the first directory has failed,
  2147. // further try without "-a" only as the server may not support it
  2148. if (FListAll == asAuto)
  2149. {
  2150. FTerminal->LogEvent(L"LIST with -a failed, will try pure LIST");
  2151. if (!FTerminal->Active)
  2152. {
  2153. FTerminal->Reopen(ropNoReadDirectory);
  2154. }
  2155. FListAll = asOff;
  2156. Repeat = true;
  2157. }
  2158. else
  2159. {
  2160. throw;
  2161. }
  2162. }
  2163. }
  2164. while (Repeat);
  2165. }
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. void __fastcall TFTPFileSystem::DoReadFile(const UnicodeString & AFileName,
  2169. TRemoteFile *& AFile)
  2170. {
  2171. UnicodeString FileName = AbsolutePath(AFileName, false);
  2172. UnicodeString FileNameOnly;
  2173. UnicodeString FilePath;
  2174. if (IsUnixRootPath(FileName))
  2175. {
  2176. FileNameOnly = FileName;
  2177. FilePath = FileName;
  2178. }
  2179. else
  2180. {
  2181. FileNameOnly = UnixExtractFileName(FileName);
  2182. FilePath = RemoteExtractFilePath(FileName);
  2183. }
  2184. TRemoteFileList * FileList = new TRemoteFileList();
  2185. try
  2186. {
  2187. // Duplicate() call below would use this to compose FullFileName
  2188. FileList->Directory = FilePath;
  2189. TFileListHelper Helper(this, FileList, false);
  2190. FFileZillaIntf->ListFile(FileNameOnly.c_str(), FilePath.c_str());
  2191. GotReply(WaitForCommandReply(), REPLY_2XX_CODE | REPLY_ALLOW_CANCEL);
  2192. TRemoteFile * File = FileList->FindFile(FileNameOnly);
  2193. if (File != NULL)
  2194. {
  2195. AFile = File->Duplicate();
  2196. }
  2197. FLastDataSent = Now();
  2198. }
  2199. __finally
  2200. {
  2201. delete FileList;
  2202. }
  2203. }
  2204. //---------------------------------------------------------------------------
  2205. bool __fastcall TFTPFileSystem::SupportsReadingFile()
  2206. {
  2207. return
  2208. FFileZillaIntf->UsingMlsd() ||
  2209. (SupportsCommand(MdtmCommand) && SupportsCommand(SizeCommand));
  2210. }
  2211. //---------------------------------------------------------------------------
  2212. void __fastcall TFTPFileSystem::ReadFile(const UnicodeString FileName,
  2213. TRemoteFile *& File)
  2214. {
  2215. File = NULL;
  2216. if (SupportsReadingFile())
  2217. {
  2218. DoReadFile(FileName, File);
  2219. }
  2220. else
  2221. {
  2222. if (IsUnixRootPath(FileName))
  2223. {
  2224. FTerminal->LogEvent(FORMAT(L"%s is a root path", (FileName)));
  2225. File = new TRemoteDirectoryFile();
  2226. File->FullFileName = FileName;
  2227. File->FileName = L"";
  2228. }
  2229. else
  2230. {
  2231. UnicodeString Path = RemoteExtractFilePath(FileName);
  2232. UnicodeString NameOnly;
  2233. int P;
  2234. bool MVSPath =
  2235. FMVS && Path.IsEmpty() &&
  2236. (FileName.SubString(1, 1) == L"'") && (FileName.SubString(FileName.Length(), 1) == L"'") &&
  2237. ((P = FileName.Pos(L".")) > 0);
  2238. if (!MVSPath)
  2239. {
  2240. NameOnly = UnixExtractFileName(FileName);
  2241. }
  2242. else
  2243. {
  2244. NameOnly = FileName.SubString(P + 1, FileName.Length() - P - 1);
  2245. }
  2246. DebugAssert(!FVMSAllRevisions);
  2247. TAutoFlag VMSAllRevisionsFlag(FVMSAllRevisions);
  2248. if (FVMS && (NameOnly.Pos(L";") > 2))
  2249. {
  2250. FTerminal->LogEvent(L"VMS versioned file detected, asking for all revisions");
  2251. FVMSAllRevisions = true;
  2252. }
  2253. TRemoteFile * AFile = NULL;
  2254. // FZAPI does not have efficient way to read properties of one file.
  2255. // In case we need properties of set of files from the same directory,
  2256. // cache the file list for future
  2257. if ((FFileListCache != NULL) &&
  2258. UnixSamePath(Path, FFileListCache->Directory) &&
  2259. (UnixIsAbsolutePath(FFileListCache->Directory) ||
  2260. (FFileListCachePath == CurrentDirectory)))
  2261. {
  2262. AFile = FFileListCache->FindFile(NameOnly);
  2263. }
  2264. // if cache is invalid or file is not in cache, (re)read the directory
  2265. if (AFile == NULL)
  2266. {
  2267. TRemoteFileList * FileListCache = new TRemoteFileList();
  2268. FileListCache->Directory = Path;
  2269. try
  2270. {
  2271. ReadDirectory(FileListCache);
  2272. }
  2273. catch(...)
  2274. {
  2275. delete FileListCache;
  2276. throw;
  2277. }
  2278. // set only after we successfully read the directory,
  2279. // otherwise, when we reconnect from ReadDirectory,
  2280. // the FFileListCache is reset from ResetCache.
  2281. delete FFileListCache;
  2282. FFileListCache = FileListCache;
  2283. FFileListCachePath = GetCurrentDirectory();
  2284. AFile = FFileListCache->FindFile(NameOnly);
  2285. }
  2286. VMSAllRevisionsFlag.Release();
  2287. if (AFile != NULL)
  2288. {
  2289. File = AFile->Duplicate();
  2290. if (MVSPath)
  2291. {
  2292. File->FileName = FileName;
  2293. File->FullFileName = FileName;
  2294. }
  2295. if (File->IsSymLink)
  2296. {
  2297. TAutoFlag AutoFlag(FForceReadSymlink);
  2298. File->Complete();
  2299. }
  2300. }
  2301. }
  2302. }
  2303. if (File == NULL)
  2304. {
  2305. throw Exception(FMTLOAD(FILE_NOT_EXISTS, (FileName)));
  2306. }
  2307. }
  2308. //---------------------------------------------------------------------------
  2309. void __fastcall TFTPFileSystem::ReadSymlink(TRemoteFile * SymlinkFile,
  2310. TRemoteFile *& File)
  2311. {
  2312. if (FForceReadSymlink && DebugAlwaysTrue(!SymlinkFile->LinkTo.IsEmpty()) && DebugAlwaysTrue(SymlinkFile->HaveFullFileName))
  2313. {
  2314. // When we get here from TFTPFileSystem::ReadFile, it's likely the second time ReadSymlink has been called for the link.
  2315. // The first time getting to the later branch, so IsDirectory is true and hence FullFileName ends with a slash.
  2316. UnicodeString SymlinkDir = UnixExtractFileDir(UnixExcludeTrailingBackslash(SymlinkFile->FullFileName));
  2317. UnicodeString LinkTo = ::AbsolutePath(SymlinkDir, SymlinkFile->LinkTo);
  2318. ReadFile(LinkTo, File);
  2319. }
  2320. else
  2321. {
  2322. // Resolving symlinks over FTP is big overhead
  2323. // (involves opening TCPIP connection for retrieving "directory listing").
  2324. // Moreover FZAPI does not support that anyway.
  2325. // Though nowadays we could use MLST to read the symlink.
  2326. std::unique_ptr<TRemoteFile> AFile(new TRemoteFile(SymlinkFile));
  2327. AFile->Terminal = FTerminal;
  2328. AFile->FileName = UnixExtractFileName(SymlinkFile->LinkTo);
  2329. // FZAPI treats all symlink target as directories
  2330. AFile->Type = FILETYPE_DIRECTORY;
  2331. File = AFile.release();
  2332. }
  2333. }
  2334. //---------------------------------------------------------------------------
  2335. void __fastcall TFTPFileSystem::RenameFile(
  2336. const UnicodeString & AFileName, const TRemoteFile *, const UnicodeString & ANewName, bool DebugUsedArg(Overwrite))
  2337. {
  2338. UnicodeString FileName = AbsolutePath(AFileName, false);
  2339. UnicodeString NewName = AbsolutePath(ANewName, false);
  2340. UnicodeString FileNameOnly = UnixExtractFileName(FileName);
  2341. UnicodeString FilePathOnly = RemoteExtractFilePath(FileName);
  2342. UnicodeString NewNameOnly = UnixExtractFileName(NewName);
  2343. UnicodeString NewPathOnly = RemoteExtractFilePath(NewName);
  2344. {
  2345. // ignore file list
  2346. TFileListHelper Helper(this, NULL, true);
  2347. FFileZillaIntf->Rename(FileNameOnly.c_str(), NewNameOnly.c_str(),
  2348. FilePathOnly.c_str(), NewPathOnly.c_str());
  2349. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  2350. }
  2351. }
  2352. //---------------------------------------------------------------------------
  2353. void __fastcall TFTPFileSystem::CopyFile(
  2354. const UnicodeString & FileName, const TRemoteFile *, const UnicodeString & NewName, bool DebugUsedArg(Overwrite))
  2355. {
  2356. DebugAssert(SupportsSiteCommand(CopySiteCommand));
  2357. EnsureLocation();
  2358. UnicodeString Command;
  2359. Command = FORMAT(L"%s CPFR %s", (SiteCommand, FileName));
  2360. SendCommand(Command);
  2361. GotReply(WaitForCommandReply(), REPLY_3XX_CODE);
  2362. Command = FORMAT(L"%s CPTO %s", (SiteCommand, NewName));
  2363. SendCommand(Command);
  2364. GotReply(WaitForCommandReply(), REPLY_2XX_CODE);
  2365. }
  2366. //---------------------------------------------------------------------------
  2367. TStrings * __fastcall TFTPFileSystem::GetFixedPaths()
  2368. {
  2369. return NULL;
  2370. }
  2371. //---------------------------------------------------------------------------
  2372. void __fastcall TFTPFileSystem::SpaceAvailable(const UnicodeString Path,
  2373. TSpaceAvailable & ASpaceAvailable)
  2374. {
  2375. if (FBytesAvailableSupported)
  2376. {
  2377. std::unique_ptr<TRemoteFileList> DummyFileList(new TRemoteFileList());
  2378. DummyFileList->Directory = Path;
  2379. ReadDirectory(DummyFileList.get());
  2380. ASpaceAvailable.UnusedBytesAvailableToUser = FBytesAvailable;
  2381. }
  2382. else if (SupportsCommand(XQuotaCommand))
  2383. {
  2384. // WS_FTP:
  2385. // XQUOTA
  2386. // 213-File and disk usage
  2387. // File count: 3
  2388. // File limit: 10000
  2389. // Disk usage: 1532791
  2390. // Disk limit: 2048000
  2391. // 213 File and disk usage end
  2392. // XQUOTA is global not path-specific
  2393. UnicodeString Command = XQuotaCommand;
  2394. SendCommand(Command);
  2395. TStrings * Response = NULL;
  2396. GotReply(WaitForCommandReply(), REPLY_2XX_CODE, L"", NULL, &Response);
  2397. std::unique_ptr<TStrings> ResponseOwner(Response);
  2398. __int64 UsedBytes = -1;
  2399. for (int Index = 0; Index < Response->Count; Index++)
  2400. {
  2401. // trimming padding
  2402. UnicodeString Line = Trim(Response->Strings[Index]);
  2403. UnicodeString Label = CutToChar(Line, L':', true);
  2404. if (SameText(Label, L"Disk usage"))
  2405. {
  2406. UsedBytes = StrToInt64(Line);
  2407. }
  2408. else if (SameText(Label, L"Disk limit") && !SameText(Line, L"unlimited"))
  2409. {
  2410. ASpaceAvailable.BytesAvailableToUser = StrToInt64(Line);
  2411. }
  2412. }
  2413. if ((UsedBytes >= 0) && (ASpaceAvailable.BytesAvailableToUser > 0))
  2414. {
  2415. ASpaceAvailable.UnusedBytesAvailableToUser = ASpaceAvailable.BytesAvailableToUser - UsedBytes;
  2416. }
  2417. }
  2418. else if (SupportsCommand(AvblCommand))
  2419. {
  2420. // draft-peterson-streamlined-ftp-command-extensions-10
  2421. // Implemented by Serv-U.
  2422. UnicodeString Command = FORMAT(L"%s %s", (AvblCommand, Path));
  2423. SendCommand(Command);
  2424. UnicodeString Response = GotReply(WaitForCommandReply(), REPLY_2XX_CODE | REPLY_SINGLE_LINE);
  2425. ASpaceAvailable.UnusedBytesAvailableToUser = StrToInt64(Response);
  2426. }
  2427. }
  2428. //---------------------------------------------------------------------------
  2429. const TSessionInfo & __fastcall TFTPFileSystem::GetSessionInfo()
  2430. {
  2431. return FSessionInfo;
  2432. }
  2433. //---------------------------------------------------------------------------
  2434. const TFileSystemInfo & __fastcall TFTPFileSystem::GetFileSystemInfo(bool /*Retrieve*/)
  2435. {
  2436. if (!FFileSystemInfoValid)
  2437. {
  2438. UnicodeString RemoteSystem = FSystem;
  2439. AddToList(RemoteSystem, FServerID, L", ");
  2440. RemoteSystem.Unique();
  2441. FFileSystemInfo.RemoteSystem = RemoteSystem;
  2442. if (FFeatures->Count == 0)
  2443. {
  2444. FFileSystemInfo.AdditionalInfo = LoadStr(FTP_NO_FEATURE_INFO);
  2445. }
  2446. else
  2447. {
  2448. FFileSystemInfo.AdditionalInfo =
  2449. FORMAT(L"%s\r\n", (LoadStr(FTP_FEATURE_INFO)));
  2450. for (int Index = 0; Index < FFeatures->Count; Index++)
  2451. {
  2452. FFileSystemInfo.AdditionalInfo += FORMAT(L" %s\r\n", (FFeatures->Strings[Index]));
  2453. }
  2454. }
  2455. FTerminal->SaveCapabilities(FFileSystemInfo);
  2456. FFileSystemInfoValid = true;
  2457. }
  2458. return FFileSystemInfo;
  2459. }
  2460. //---------------------------------------------------------------------------
  2461. bool __fastcall TFTPFileSystem::TemporaryTransferFile(const UnicodeString & /*FileName*/)
  2462. {
  2463. return false;
  2464. }
  2465. //---------------------------------------------------------------------------
  2466. bool __fastcall TFTPFileSystem::GetStoredCredentialsTried()
  2467. {
  2468. return FStoredPasswordTried;
  2469. }
  2470. //---------------------------------------------------------------------------
  2471. UnicodeString __fastcall TFTPFileSystem::GetUserName()
  2472. {
  2473. return FUserName;
  2474. }
  2475. //---------------------------------------------------------------------------
  2476. UnicodeString __fastcall TFTPFileSystem::GetCurrentDirectory()
  2477. {
  2478. return FCurrentDirectory;
  2479. }
  2480. //---------------------------------------------------------------------------
  2481. const wchar_t * __fastcall TFTPFileSystem::GetOption(int OptionID) const
  2482. {
  2483. TSessionData * Data = FTerminal->SessionData;
  2484. switch (OptionID)
  2485. {
  2486. case OPTION_PROXYHOST:
  2487. case OPTION_FWHOST:
  2488. FOptionScratch = Data->ProxyHost;
  2489. break;
  2490. case OPTION_PROXYUSER:
  2491. case OPTION_FWUSER:
  2492. FOptionScratch = Data->ProxyUsername;
  2493. break;
  2494. case OPTION_PROXYPASS:
  2495. case OPTION_FWPASS:
  2496. FOptionScratch = Data->ProxyPassword;
  2497. break;
  2498. case OPTION_TRANSFERIP:
  2499. FOptionScratch = FTerminal->Configuration->ExternalIpAddress;
  2500. break;
  2501. case OPTION_ANONPWD:
  2502. case OPTION_TRANSFERIP6:
  2503. FOptionScratch = L"";
  2504. break;
  2505. case OPTION_MPEXT_CERT_STORAGE:
  2506. FOptionScratch = FTerminal->Configuration->CertificateStorageExpanded;
  2507. break;
  2508. default:
  2509. DebugFail();
  2510. FOptionScratch = L"";
  2511. }
  2512. return FOptionScratch.c_str();
  2513. }
  2514. //---------------------------------------------------------------------------
  2515. int __fastcall TFTPFileSystem::GetOptionVal(int OptionID) const
  2516. {
  2517. TSessionData * Data = FTerminal->SessionData;
  2518. int Result;
  2519. switch (OptionID)
  2520. {
  2521. case OPTION_PROXYTYPE:
  2522. switch (Data->ProxyMethod)
  2523. {
  2524. case ::pmNone:
  2525. Result = 0; // PROXYTYPE_NOPROXY;
  2526. break;
  2527. case pmSocks4:
  2528. Result = 2; // PROXYTYPE_SOCKS4A
  2529. break;
  2530. case pmSocks5:
  2531. Result = 3; // PROXYTYPE_SOCKS5
  2532. break;
  2533. case pmHTTP:
  2534. Result = 4; // PROXYTYPE_HTTP11
  2535. break;
  2536. case pmTelnet:
  2537. case pmCmd:
  2538. default:
  2539. DebugFail();
  2540. Result = 0; // PROXYTYPE_NOPROXY;
  2541. break;
  2542. }
  2543. break;
  2544. case OPTION_PROXYPORT:
  2545. case OPTION_FWPORT:
  2546. Result = Data->ProxyPort;
  2547. break;
  2548. case OPTION_PROXYUSELOGON:
  2549. Result = !Data->ProxyUsername.IsEmpty();
  2550. break;
  2551. case OPTION_LOGONTYPE:
  2552. Result = Data->FtpProxyLogonType;
  2553. break;
  2554. case OPTION_TIMEOUTLENGTH:
  2555. Result = Data->Timeout;
  2556. break;
  2557. case OPTION_DEBUGSHOWLISTING:
  2558. Result = (FTerminal->Configuration->ActualLogProtocol >= 0);
  2559. break;
  2560. case OPTION_PASV:
  2561. // should never get here t_server.nPasv being nonzero
  2562. DebugFail();
  2563. Result = FALSE;
  2564. break;
  2565. case OPTION_PRESERVEDOWNLOADFILETIME:
  2566. case OPTION_MPEXT_PRESERVEUPLOADFILETIME:
  2567. Result = FFileTransferPreserveTime ? TRUE : FALSE;
  2568. break;
  2569. case OPTION_LIMITPORTRANGE:
  2570. Result = !FTerminal->SessionData->FtpPasvMode && FTerminal->Configuration->HasLocalPortNumberLimits();
  2571. break;
  2572. case OPTION_PORTRANGELOW:
  2573. Result = FTerminal->Configuration->LocalPortNumberMin;
  2574. break;
  2575. case OPTION_PORTRANGEHIGH:
  2576. Result = FTerminal->Configuration->LocalPortNumberMax;
  2577. break;
  2578. case OPTION_ENABLE_IPV6:
  2579. Result = ((Data->AddressFamily != afIPv4) ? TRUE : FALSE);
  2580. break;
  2581. case OPTION_KEEPALIVE:
  2582. Result = ((Data->FtpPingType != fptOff) ? TRUE : FALSE);
  2583. break;
  2584. case OPTION_INTERVALLOW:
  2585. case OPTION_INTERVALHIGH:
  2586. Result = Data->FtpPingInterval;
  2587. break;
  2588. case OPTION_VMSALLREVISIONS:
  2589. Result = FVMSAllRevisions ? TRUE : FALSE;
  2590. break;
  2591. case OPTION_SPEEDLIMIT_DOWNLOAD_TYPE:
  2592. case OPTION_SPEEDLIMIT_UPLOAD_TYPE:
  2593. Result = (FFileTransferCPSLimit == 0 ? 0 : 1);
  2594. break;
  2595. case OPTION_SPEEDLIMIT_DOWNLOAD_VALUE:
  2596. case OPTION_SPEEDLIMIT_UPLOAD_VALUE:
  2597. Result = (FFileTransferCPSLimit / 1024); // FZAPI expects KB/s
  2598. break;
  2599. case OPTION_MPEXT_SHOWHIDDEN:
  2600. Result = (FDoListAll ? TRUE : FALSE);
  2601. break;
  2602. case OPTION_MPEXT_SSLSESSIONREUSE:
  2603. Result = (Data->SslSessionReuse ? TRUE : FALSE);
  2604. break;
  2605. case OPTION_MPEXT_SNDBUF:
  2606. Result = Data->SendBuf;
  2607. break;
  2608. case OPTION_MPEXT_TRANSFER_ACTIVE_IMMEDIATELY:
  2609. Result = FTransferActiveImmediately;
  2610. break;
  2611. case OPTION_MPEXT_REMOVE_BOM:
  2612. Result = FFileTransferRemoveBOM ? TRUE : FALSE;
  2613. break;
  2614. case OPTION_MPEXT_LOG_SENSITIVE:
  2615. Result = FTerminal->Configuration->LogSensitive ? TRUE : FALSE;
  2616. break;
  2617. case OPTION_MPEXT_HOST:
  2618. Result = (Data->FtpHost == asOn);
  2619. break;
  2620. case OPTION_MPEXT_NODELAY:
  2621. Result = Data->TcpNoDelay;
  2622. break;
  2623. case OPTION_MPEXT_NOLIST:
  2624. Result = FFileTransferNoList ? TRUE : FALSE;
  2625. break;
  2626. case OPTION_MPEXT_COMPLETE_TLS_SHUTDOWN:
  2627. if (Data->CompleteTlsShutdown == asAuto)
  2628. {
  2629. // As of FileZilla Server 1.6.1 this does not seem to be needed. It's still needed with 1.5.1.
  2630. // It was possibly fixed by 1.6.0 (2022-12-06) change:
  2631. // Fixed an issue in the networking code when dealing with TLS close_notify alerts
  2632. Result = FFileZilla ? -1 : 0;
  2633. }
  2634. else
  2635. {
  2636. Result = (Data->CompleteTlsShutdown == asOn) ? 1 : -1;
  2637. }
  2638. break;
  2639. case OPTION_MPEXT_WORK_FROM_CWD:
  2640. Result = (FWorkFromCwd == asOn);
  2641. break;
  2642. case OPTION_MPEXT_TRANSFER_SIZE:
  2643. {
  2644. __int64 TransferSize = 0;
  2645. if ((FTerminal->OperationProgress != NULL) &&
  2646. (FTerminal->OperationProgress->Operation == foCopy) &&
  2647. (FTerminal->OperationProgress->Side == osLocal))
  2648. {
  2649. TransferSize = FTerminal->OperationProgress->TransferSize - FTerminal->OperationProgress->TransferredSize;
  2650. }
  2651. Result = static_cast<int>(static_cast<unsigned int>(TransferSize & std::numeric_limits<unsigned int>::max()));
  2652. }
  2653. break;
  2654. default:
  2655. DebugFail();
  2656. Result = FALSE;
  2657. break;
  2658. }
  2659. return Result;
  2660. }
  2661. //---------------------------------------------------------------------------
  2662. bool __fastcall TFTPFileSystem::PostMessage(unsigned int Type, WPARAM wParam, LPARAM lParam)
  2663. {
  2664. if (Type == TFileZillaIntf::MSG_TRANSFERSTATUS)
  2665. {
  2666. // Stop here if FileTransferProgress is proceeding,
  2667. // it makes "pause" in queue work.
  2668. // Paused queue item stops in some of the TFileOperationProgressType
  2669. // methods called from FileTransferProgress
  2670. TGuard Guard(FTransferStatusCriticalSection);
  2671. }
  2672. TGuard Guard(FQueueCriticalSection);
  2673. FQueue->push_back(TMessageQueue::value_type(wParam, lParam));
  2674. SetEvent(FQueueEvent);
  2675. return true;
  2676. }
  2677. //---------------------------------------------------------------------------
  2678. bool __fastcall TFTPFileSystem::ProcessMessage()
  2679. {
  2680. bool Result;
  2681. TMessageQueue::value_type Message;
  2682. {
  2683. TGuard Guard(FQueueCriticalSection);
  2684. Result = !FQueue->empty();
  2685. if (Result)
  2686. {
  2687. Message = FQueue->front();
  2688. FQueue->pop_front();
  2689. }
  2690. else
  2691. {
  2692. // now we are perfectly sure that the queue is empty as it is locked,
  2693. // so reset the event
  2694. ResetEvent(FQueueEvent);
  2695. }
  2696. }
  2697. if (Result)
  2698. {
  2699. FFileZillaIntf->HandleMessage(Message.first, Message.second);
  2700. }
  2701. return Result;
  2702. }
  2703. //---------------------------------------------------------------------------
  2704. void __fastcall TFTPFileSystem::DiscardMessages()
  2705. {
  2706. try
  2707. {
  2708. while (ProcessMessage());
  2709. }
  2710. __finally
  2711. {
  2712. FReply = 0;
  2713. FCommandReply = 0;
  2714. }
  2715. }
  2716. //---------------------------------------------------------------------------
  2717. void __fastcall TFTPFileSystem::WaitForMessages()
  2718. {
  2719. unsigned int Result;
  2720. do
  2721. {
  2722. Result = WaitForSingleObject(FQueueEvent, GUIUpdateInterval);
  2723. FTerminal->ProcessGUI();
  2724. } while (Result == WAIT_TIMEOUT);
  2725. if (Result != WAIT_OBJECT_0)
  2726. {
  2727. FTerminal->FatalError(NULL, FMTLOAD(INTERNAL_ERROR, (L"ftp#1", IntToStr(int(Result)))));
  2728. }
  2729. }
  2730. //---------------------------------------------------------------------------
  2731. void __fastcall TFTPFileSystem::PoolForFatalNonCommandReply()
  2732. {
  2733. DebugAssert(FReply == 0);
  2734. DebugAssert(FCommandReply == 0);
  2735. DebugAssert(!FWaitingForReply);
  2736. FWaitingForReply = true;
  2737. unsigned int Reply;
  2738. try
  2739. {
  2740. // discard up to one reply
  2741. // (it should not happen here that two replies are posted anyway)
  2742. while (ProcessMessage() && (FReply == 0));
  2743. Reply = FReply;
  2744. }
  2745. __finally
  2746. {
  2747. FReply = 0;
  2748. DebugAssert(FCommandReply == 0);
  2749. FCommandReply = 0;
  2750. DebugAssert(FWaitingForReply);
  2751. FWaitingForReply = false;
  2752. }
  2753. if (Reply != 0)
  2754. {
  2755. // throws
  2756. GotNonCommandReply(Reply);
  2757. }
  2758. }
  2759. //---------------------------------------------------------------------------
  2760. bool __fastcall TFTPFileSystem::NoFinalLastCode()
  2761. {
  2762. return (FLastCodeClass == 0) || (FLastCodeClass == 1);
  2763. }
  2764. //---------------------------------------------------------------------------
  2765. bool __fastcall TFTPFileSystem::KeepWaitingForReply(unsigned int & ReplyToAwait, bool WantLastCode)
  2766. {
  2767. // To keep waiting,
  2768. // non-command reply must be unset,
  2769. // the reply we wait for must be unset or
  2770. // last code must be unset (if we wait for it).
  2771. // Though make sure that disconnect makes it through always. As for example when connection is closed already,
  2772. // when sending commands, we may get REPLY_DISCONNECTED as a command response and no other response after,
  2773. // which would cause a hang.
  2774. return
  2775. (FReply == 0) &&
  2776. ((ReplyToAwait == 0) ||
  2777. (WantLastCode && NoFinalLastCode() && FLAGCLEAR(ReplyToAwait, TFileZillaIntf::REPLY_DISCONNECTED)));
  2778. }
  2779. //---------------------------------------------------------------------------
  2780. void __fastcall TFTPFileSystem::DoWaitForReply(unsigned int & ReplyToAwait, bool WantLastCode)
  2781. {
  2782. try
  2783. {
  2784. while (KeepWaitingForReply(ReplyToAwait, WantLastCode))
  2785. {
  2786. WaitForMessages();
  2787. // wait for the first reply only,
  2788. // i.e. in case two replies are posted get the first only.
  2789. // e.g. when server closes the connection, but posts error message before,
  2790. // sometime it happens that command (like download) fails because of the error
  2791. // and does not catch the disconnection. then asynchronous "disconnect reply"
  2792. // is posted immediately afterwards. leave detection of that to Idle()
  2793. while (ProcessMessage() && KeepWaitingForReply(ReplyToAwait, WantLastCode));
  2794. }
  2795. if (FReply != 0)
  2796. {
  2797. // throws
  2798. GotNonCommandReply(FReply);
  2799. }
  2800. }
  2801. catch(...)
  2802. {
  2803. // even if non-fatal error happens, we must process pending message,
  2804. // so that we "eat" the reply message, and it doesn't get mistakenly
  2805. // associated with future connect
  2806. if (FTerminal->Active)
  2807. {
  2808. DoWaitForReply(ReplyToAwait, WantLastCode);
  2809. }
  2810. throw;
  2811. }
  2812. }
  2813. //---------------------------------------------------------------------------
  2814. unsigned int __fastcall TFTPFileSystem::WaitForReply(bool Command, bool WantLastCode)
  2815. {
  2816. DebugAssert(FReply == 0);
  2817. DebugAssert(FCommandReply == 0);
  2818. DebugAssert(!FWaitingForReply);
  2819. ResetReply();
  2820. FWaitingForReply = true;
  2821. unsigned int Reply;
  2822. try
  2823. {
  2824. unsigned int & ReplyToAwait = (Command ? FCommandReply : FReply);
  2825. DoWaitForReply(ReplyToAwait, WantLastCode);
  2826. Reply = ReplyToAwait;
  2827. }
  2828. __finally
  2829. {
  2830. FReply = 0;
  2831. FCommandReply = 0;
  2832. DebugAssert(FWaitingForReply);
  2833. FWaitingForReply = false;
  2834. }
  2835. return Reply;
  2836. }
  2837. //---------------------------------------------------------------------------
  2838. unsigned int __fastcall TFTPFileSystem::WaitForCommandReply(bool WantLastCode)
  2839. {
  2840. return WaitForReply(true, WantLastCode);
  2841. }
  2842. //---------------------------------------------------------------------------
  2843. void __fastcall TFTPFileSystem::WaitForFatalNonCommandReply()
  2844. {
  2845. WaitForReply(false, false);
  2846. DebugFail();
  2847. }
  2848. //---------------------------------------------------------------------------
  2849. void __fastcall TFTPFileSystem::ResetReply()
  2850. {
  2851. FLastCode = 0;
  2852. FLastCodeClass = 0;
  2853. FMultiLineResponse = false;
  2854. DebugAssert(FLastResponse != NULL);
  2855. FLastResponse->Clear();
  2856. DebugAssert(FLastErrorResponse != NULL);
  2857. FLastErrorResponse->Clear();
  2858. DebugAssert(FLastError != NULL);
  2859. FLastError->Clear();
  2860. }
  2861. //---------------------------------------------------------------------------
  2862. void __fastcall TFTPFileSystem::GotNonCommandReply(unsigned int Reply)
  2863. {
  2864. DebugAssert(FLAGSET(Reply, TFileZillaIntf::REPLY_DISCONNECTED));
  2865. GotReply(Reply);
  2866. // should never get here as GotReply should raise fatal exception
  2867. DebugFail();
  2868. }
  2869. //---------------------------------------------------------------------------
  2870. void __fastcall TFTPFileSystem::Disconnect()
  2871. {
  2872. Discard();
  2873. FTerminal->Closed();
  2874. }
  2875. //---------------------------------------------------------------------------
  2876. UnicodeString __fastcall TFTPFileSystem::GotReply(unsigned int Reply, unsigned int Flags,
  2877. UnicodeString Error, unsigned int * Code, TStrings ** Response)
  2878. {
  2879. UnicodeString Result;
  2880. try
  2881. {
  2882. if (FLAGSET(Reply, TFileZillaIntf::REPLY_OK))
  2883. {
  2884. DebugAssert(Reply == TFileZillaIntf::REPLY_OK);
  2885. // With REPLY_2XX_CODE treat "OK" non-2xx code like an error.
  2886. // REPLY_3XX_CODE has to be always used along with REPLY_2XX_CODE.
  2887. if ((FLAGSET(Flags, REPLY_2XX_CODE) && (FLastCodeClass != 2)) &&
  2888. ((FLAGCLEAR(Flags, REPLY_3XX_CODE) || (FLastCodeClass != 3))))
  2889. {
  2890. GotReply(TFileZillaIntf::REPLY_ERROR, Flags, Error);
  2891. }
  2892. }
  2893. else if (FLAGSET(Reply, TFileZillaIntf::REPLY_CANCEL) &&
  2894. FLAGSET(Flags, REPLY_ALLOW_CANCEL))
  2895. {
  2896. DebugAssert(
  2897. (Reply == (TFileZillaIntf::REPLY_CANCEL | TFileZillaIntf::REPLY_ERROR)) ||
  2898. (Reply == (TFileZillaIntf::REPLY_ABORTED | TFileZillaIntf::REPLY_CANCEL | TFileZillaIntf::REPLY_ERROR)));
  2899. // noop
  2900. }
  2901. // we do not expect these with our usage of FZ
  2902. else if (Reply &
  2903. (TFileZillaIntf::REPLY_WOULDBLOCK | TFileZillaIntf::REPLY_OWNERNOTSET |
  2904. TFileZillaIntf::REPLY_INVALIDPARAM | TFileZillaIntf::REPLY_ALREADYCONNECTED |
  2905. TFileZillaIntf::REPLY_IDLE | TFileZillaIntf::REPLY_NOTINITIALIZED |
  2906. TFileZillaIntf::REPLY_ALREADYINIZIALIZED))
  2907. {
  2908. FTerminal->FatalError(NULL, FMTLOAD(INTERNAL_ERROR, (L"ftp#2", FORMAT(L"0x%x", (int(Reply))))));
  2909. }
  2910. else
  2911. {
  2912. // everything else must be an error or disconnect notification
  2913. DebugAssert(
  2914. FLAGSET(Reply, TFileZillaIntf::REPLY_ERROR) ||
  2915. FLAGSET(Reply, TFileZillaIntf::REPLY_DISCONNECTED));
  2916. // TODO: REPLY_CRITICALERROR ignored
  2917. // REPLY_NOTCONNECTED happens if connection is closed between moment
  2918. // when FZAPI interface method dispatches the command to FZAPI thread
  2919. // and moment when FZAPI thread receives the command
  2920. bool Disconnected =
  2921. FLAGSET(Reply, TFileZillaIntf::REPLY_DISCONNECTED) ||
  2922. FLAGSET(Reply, TFileZillaIntf::REPLY_NOTCONNECTED);
  2923. bool DoClose = false;
  2924. UnicodeString HelpKeyword;
  2925. TStrings * MoreMessages = new TStringList();
  2926. try
  2927. {
  2928. if (Disconnected)
  2929. {
  2930. if (FLAGCLEAR(Flags, REPLY_CONNECT))
  2931. {
  2932. MoreMessages->Add(LoadStr(LOST_CONNECTION));
  2933. Disconnect();
  2934. }
  2935. else
  2936. {
  2937. // For connection failure, do not report that connection was lost,
  2938. // its obvious.
  2939. // Also do not report to terminal that we are closed as
  2940. // that turns terminal into closed mode, but we want to
  2941. // pretend (at least with failed authentication) to retry
  2942. // with the same connection (as with SSH), so we explicitly
  2943. // close terminal in Open() only after we give up
  2944. Discard();
  2945. }
  2946. }
  2947. if (FLAGSET(Reply, TFileZillaIntf::REPLY_ABORTED))
  2948. {
  2949. MoreMessages->Add(LoadStr(USER_TERMINATED));
  2950. }
  2951. if (FLAGSET(Reply, TFileZillaIntf::REPLY_NOTSUPPORTED))
  2952. {
  2953. MoreMessages->Add(LoadStr(NOTSUPPORTED));
  2954. }
  2955. if (FLastCode == 530)
  2956. {
  2957. // Serv-U also uses this code in response to "SITE PSWD"
  2958. MoreMessages->Add(LoadStr(AUTHENTICATION_FAILED));
  2959. }
  2960. bool RetryTransfer = false;
  2961. if ((FLastCode == 425) || (FLastCode == 426))
  2962. {
  2963. if (FAnyTransferSucceeded)
  2964. {
  2965. FTerminal->LogEvent(FORMAT(L"Got %d after some previous data connections succeeded, retrying connection", (FLastCode)));
  2966. RetryTransfer = true;
  2967. }
  2968. else if (!FTerminal->SessionData->FtpPasvMode)
  2969. {
  2970. MoreMessages->Add(LoadStr(FTP_CANNOT_OPEN_ACTIVE_CONNECTION2));
  2971. HelpKeyword = HELP_FTP_CANNOT_OPEN_ACTIVE_CONNECTION;
  2972. }
  2973. }
  2974. if (FLastCode == DummyTimeoutCode)
  2975. {
  2976. HelpKeyword = HELP_ERRORMSG_TIMEOUT;
  2977. }
  2978. if (FLastCode == DummyDisconnectCode)
  2979. {
  2980. HelpKeyword = HELP_STATUSMSG_DISCONNECTED;
  2981. }
  2982. if (FAnyTransferSucceeded && (FLastError->Count > 0))
  2983. {
  2984. UnicodeString CantOpenTransferChannelMessage = LoadStr(IDS_ERRORMSG_CANTOPENTRANSFERCHANNEL);
  2985. int P = CantOpenTransferChannelMessage.Pos(L"%");
  2986. if (DebugAlwaysTrue(P > 0))
  2987. {
  2988. CantOpenTransferChannelMessage.SetLength(P - 1);
  2989. }
  2990. if (ContainsText(FLastError->Strings[0], CantOpenTransferChannelMessage))
  2991. {
  2992. FTerminal->LogEvent(L"Failed to connection data connection after some previous data connections succeeded, retrying connection");
  2993. RetryTransfer = true;
  2994. }
  2995. }
  2996. if (RetryTransfer)
  2997. {
  2998. Disconnected = true;
  2999. // Close only later, as we still need to use FLast* fields
  3000. DoClose = true;
  3001. }
  3002. MoreMessages->AddStrings(FLastError);
  3003. // already cleared from WaitForReply, but GotReply can be also called
  3004. // from Closed. then make sure that error from previous command not
  3005. // associated with session closure is not reused
  3006. FLastError->Clear();
  3007. MoreMessages->AddStrings(FLastErrorResponse);
  3008. // see comment for FLastError
  3009. FLastResponse->Clear();
  3010. FLastErrorResponse->Clear();
  3011. if (MoreMessages->Count == 0)
  3012. {
  3013. delete MoreMessages;
  3014. MoreMessages = NULL;
  3015. }
  3016. }
  3017. catch(...)
  3018. {
  3019. delete MoreMessages;
  3020. throw;
  3021. }
  3022. if (Error.IsEmpty() && (MoreMessages != NULL))
  3023. {
  3024. DebugAssert(MoreMessages->Count > 0);
  3025. // bit too generic assigning of main instructions, let's see how it works
  3026. Error = MainInstructions(MoreMessages->Strings[0]);
  3027. MoreMessages->Delete(0);
  3028. }
  3029. if (Disconnected)
  3030. {
  3031. if (DoClose)
  3032. {
  3033. Close();
  3034. }
  3035. // for fatal error, it is essential that there is some message
  3036. DebugAssert(!Error.IsEmpty());
  3037. ExtException * E = new ExtException(Error, MoreMessages, true, HelpKeyword);
  3038. try
  3039. {
  3040. FTerminal->FatalError(E, L"");
  3041. }
  3042. __finally
  3043. {
  3044. delete E;
  3045. }
  3046. }
  3047. else
  3048. {
  3049. throw ExtException(Error, MoreMessages, true, HelpKeyword);
  3050. }
  3051. }
  3052. if ((Code != NULL) && (FLastCodeClass != DummyCodeClass))
  3053. {
  3054. *Code = FLastCode;
  3055. }
  3056. if (FLAGSET(Flags, REPLY_SINGLE_LINE))
  3057. {
  3058. if (FLastResponse->Count != 1)
  3059. {
  3060. throw Exception(FMTLOAD(FTP_RESPONSE_ERROR, (FLastCommandSent, FLastResponse->Text)));
  3061. }
  3062. Result = FLastResponse->Strings[0];
  3063. }
  3064. if (Response != NULL)
  3065. {
  3066. *Response = FLastResponse;
  3067. FLastResponse = new TStringList();
  3068. // just to be consistent
  3069. delete FLastErrorResponse;
  3070. FLastErrorResponse = new TStringList();
  3071. }
  3072. }
  3073. __finally
  3074. {
  3075. ResetReply();
  3076. }
  3077. return Result;
  3078. }
  3079. //---------------------------------------------------------------------------
  3080. void __fastcall TFTPFileSystem::SendCommand(const UnicodeString & Command)
  3081. {
  3082. FFileZillaIntf->CustomCommand(Command.c_str());
  3083. FLastCommandSent = CopyToChar(Command, L' ', false);
  3084. }
  3085. //---------------------------------------------------------------------------
  3086. void __fastcall TFTPFileSystem::SetLastCode(int Code)
  3087. {
  3088. FLastCode = Code;
  3089. FLastCodeClass = (Code / 100);
  3090. }
  3091. //---------------------------------------------------------------------------
  3092. void __fastcall TFTPFileSystem::StoreLastResponse(const UnicodeString & Text)
  3093. {
  3094. FLastResponse->Add(Text);
  3095. if (FLastCodeClass >= 4)
  3096. {
  3097. FLastErrorResponse->Add(Text);
  3098. }
  3099. }
  3100. //---------------------------------------------------------------------------
  3101. void __fastcall TFTPFileSystem::HandleReplyStatus(UnicodeString Response)
  3102. {
  3103. int Code;
  3104. if (FOnCaptureOutput != NULL)
  3105. {
  3106. FOnCaptureOutput(Response, cotOutput);
  3107. }
  3108. if (FWelcomeMessage.IsEmpty() && StartsStr(L"SSH", Response))
  3109. {
  3110. FLastErrorResponse->Add(LoadStr(SFTP_AS_FTP_ERROR));
  3111. }
  3112. // Two forms of multiline responses were observed
  3113. // (the first is according to the RFC 959):
  3114. // 211-Features:
  3115. // MDTM
  3116. // REST STREAM
  3117. // SIZE
  3118. // 211 End
  3119. // This format is according to RFC 2228.
  3120. // Is used by ProFTPD when deprecated MultilineRFC2228 directive is enabled
  3121. // http://www.proftpd.org/docs/modules/mod_core.html#FileZillaNonASCII
  3122. // 211-Features:
  3123. // 211-MDTM
  3124. // 211-REST STREAM
  3125. // 211-SIZE
  3126. // 211-AUTH TLS
  3127. // 211-PBSZ
  3128. // 211-PROT
  3129. // 211 End
  3130. // IIS 2003:
  3131. // 211-FEAT
  3132. // SIZE
  3133. // MDTM
  3134. // 211 END
  3135. // Partially duplicated in CFtpControlSocket::OnReceive
  3136. bool HasCodePrefix =
  3137. (Response.Length() >= 3) &&
  3138. TryStrToInt(Response.SubString(1, 3), Code) &&
  3139. (Code >= 100) && (Code <= 599) &&
  3140. ((Response.Length() == 3) || (Response[4] == L' ') || (Response[4] == L'-'));
  3141. if (HasCodePrefix && !FMultiLineResponse)
  3142. {
  3143. FMultiLineResponse = (Response.Length() >= 4) && (Response[4] == L'-');
  3144. FLastResponse->Clear();
  3145. FLastErrorResponse->Clear();
  3146. SetLastCode(Code);
  3147. if (Response.Length() >= 5)
  3148. {
  3149. StoreLastResponse(Response.SubString(5, Response.Length() - 4));
  3150. }
  3151. }
  3152. else
  3153. {
  3154. int Start;
  3155. // response with code prefix
  3156. if (HasCodePrefix && (FLastCode == Code))
  3157. {
  3158. // End of multiline response?
  3159. if ((Response.Length() <= 3) || (Response[4] == L' '))
  3160. {
  3161. FMultiLineResponse = false;
  3162. }
  3163. Start = 5;
  3164. }
  3165. else
  3166. {
  3167. Start = (((Response.Length() >= 1) && (Response[1] == L' ')) ? 2 : 1);
  3168. }
  3169. // Intermediate empty lines are being added
  3170. if (FMultiLineResponse || (Response.Length() >= Start))
  3171. {
  3172. StoreLastResponse(Response.SubString(Start, Response.Length() - Start + 1));
  3173. }
  3174. }
  3175. if (StartsStr(DirectoryHasBytesPrefix, Response))
  3176. {
  3177. UnicodeString Buf = Response;
  3178. Buf.Delete(1, DirectoryHasBytesPrefix.Length());
  3179. Buf = Buf.TrimLeft();
  3180. UnicodeString BytesStr = CutToChar(Buf, L' ', true);
  3181. BytesStr = ReplaceStr(BytesStr, L",", L"");
  3182. FBytesAvailable = StrToInt64Def(BytesStr, -1);
  3183. if (FBytesAvailable >= 0)
  3184. {
  3185. FBytesAvailableSupported = true;
  3186. }
  3187. }
  3188. if (!FMultiLineResponse)
  3189. {
  3190. if (FLastCode == 220)
  3191. {
  3192. // HOST command also uses 220 response.
  3193. // Neither our use of welcome message is prepared for changing it
  3194. // during the session, so we keep the initial message only.
  3195. // Theoretically the welcome message can be host-specific,
  3196. // but IIS uses "220 Host accepted", and we are not interested in that anyway.
  3197. // Serv-U repeats the initial welcome message.
  3198. // WS_FTP uses "200 Command HOST succeed"
  3199. if (FWelcomeMessage.IsEmpty())
  3200. {
  3201. FWelcomeMessage = FLastResponse->Text;
  3202. if (FTerminal->Configuration->ShowFtpWelcomeMessage)
  3203. {
  3204. FTerminal->DisplayBanner(FWelcomeMessage);
  3205. }
  3206. // Idea FTP Server v0.80
  3207. if ((FTerminal->SessionData->FtpTransferActiveImmediately == asAuto) &&
  3208. FWelcomeMessage.Pos(L"Idea FTP Server") > 0)
  3209. {
  3210. FTerminal->LogEvent(L"The server requires TLS/SSL handshake on transfer connection before responding 1yz to STOR/APPE");
  3211. FTransferActiveImmediately = true;
  3212. }
  3213. if (ContainsText(FWelcomeMessage, L"Microsoft FTP Service") && !FIIS)
  3214. {
  3215. FTerminal->LogEvent(L"IIS detected.");
  3216. FIIS = true;
  3217. }
  3218. }
  3219. }
  3220. else if (FLastCommand == PASS)
  3221. {
  3222. FStoredPasswordTried = true;
  3223. // 530 = "Not logged in."
  3224. if (FLastCode == 530)
  3225. {
  3226. FPasswordFailed = true;
  3227. }
  3228. }
  3229. else if (FLastCommand == SYST)
  3230. {
  3231. DebugAssert(FSystem.IsEmpty());
  3232. // Positive reply to "SYST" should be 215, see RFC 959.
  3233. // But "VMS VAX/VMS V6.1 on node nsrp14" uses plain 200.
  3234. if (FLastCodeClass == 2)
  3235. {
  3236. FSystem = FLastResponse->Text.TrimRight();
  3237. // FZAPI has own detection of MVS/VMS
  3238. // Full name is "MVS is the operating system of this server. FTP Server is running on ..."
  3239. // (the ... can be "z/OS")
  3240. // https://www.ibm.com/docs/en/zos/latest?topic=2rc-215-mvs-is-operating-system-this-server-ftp-server-is-running-name
  3241. // FZPI has a different incompatible detection.
  3242. // MVS FTP servers have two separate MVS and Unix file systems coexisting in the same session.
  3243. FMVS = (FSystem.SubString(1, 3) == L"MVS");
  3244. if (FMVS)
  3245. {
  3246. FTerminal->LogEvent(L"MVS system detected.");
  3247. }
  3248. // The FWelcomeMessage usually contains "Microsoft FTP Service" but can be empty
  3249. if (ContainsText(FSystem, L"Windows_NT"))
  3250. {
  3251. FTerminal->LogEvent(L"The server is probably running Windows, assuming that directory listing timestamps are affected by DST.");
  3252. FWindowsServer = true;
  3253. if (!FIIS)
  3254. {
  3255. FTerminal->LogEvent(L"IIS detected.");
  3256. FIIS = true;
  3257. }
  3258. }
  3259. // VMS system type. VMS V5.5-2.
  3260. // VMS VAX/VMS V6.1 on node nsrp14
  3261. if (FSystem.SubString(1, 4) == L"VMS ")
  3262. {
  3263. FTerminal->LogEvent(L"VMS system detected.");
  3264. FVMS = true;
  3265. }
  3266. if ((FListAll == asAuto) &&
  3267. // full name is "Personal FTP Server PRO K6.0"
  3268. ((FSystem.Pos(L"Personal FTP Server") > 0) ||
  3269. FMVS || FVMS))
  3270. {
  3271. FTerminal->LogEvent(L"Server is known not to support LIST -a");
  3272. FListAll = asOff;
  3273. }
  3274. if ((FWorkFromCwd == asAuto) && FVMS)
  3275. {
  3276. FTerminal->LogEvent(L"Server is known to require use of relative paths");
  3277. FWorkFromCwd = asOn;
  3278. }
  3279. // 220-FileZilla Server 1.0.1
  3280. // 220 Please visit https://filezilla-project.org/
  3281. // SYST
  3282. // 215 UNIX emulated by FileZilla
  3283. // (Welcome message is configurable)
  3284. if (ContainsText(FSystem, L"FileZilla"))
  3285. {
  3286. FTerminal->LogEvent(L"FileZilla server detected.");
  3287. FFileZilla = true;
  3288. }
  3289. }
  3290. else
  3291. {
  3292. FSystem = L"";
  3293. }
  3294. }
  3295. else if (FLastCommand == FEAT)
  3296. {
  3297. HandleFeatReply();
  3298. }
  3299. }
  3300. }
  3301. //---------------------------------------------------------------------------
  3302. void __fastcall TFTPFileSystem::ResetFeatures()
  3303. {
  3304. FFeatures->Clear();
  3305. FSupportedCommands->Clear();
  3306. FSupportedSiteCommands->Clear();
  3307. FHashAlgs->Clear();
  3308. FSupportsAnyChecksumFeature = false;
  3309. }
  3310. //---------------------------------------------------------------------------
  3311. void TFTPFileSystem::ProcessFeatures()
  3312. {
  3313. std::unique_ptr<TStrings> Features(FTerminal->ProcessFeatures(FFeatures));
  3314. for (int Index = 0; Index < Features->Count; Index++)
  3315. {
  3316. UnicodeString Feature = Features->Strings[Index];
  3317. UnicodeString Args = Feature;
  3318. UnicodeString Command = CutToChar(Args, L' ', true);
  3319. // Serv-U lists Xalg commands like:
  3320. // XSHA1 filename;start;end
  3321. FSupportedCommands->Add(Command);
  3322. if (SameText(Command, SiteCommand))
  3323. {
  3324. // Serv-U lists all SITE commands in one line like:
  3325. // SITE PSWD;SET;ZONE;CHMOD;MSG;EXEC;HELP
  3326. // But ProFTPD lists them separately:
  3327. // SITE UTIME
  3328. // SITE RMDIR
  3329. // SITE COPY
  3330. // SITE MKDIR
  3331. // SITE SYMLINK
  3332. while (!Args.IsEmpty())
  3333. {
  3334. UnicodeString Arg = CutToChar(Args, L';', true);
  3335. FSupportedSiteCommands->Add(Arg);
  3336. }
  3337. }
  3338. else if (SameText(Command, HashCommand))
  3339. {
  3340. while (!Args.IsEmpty())
  3341. {
  3342. UnicodeString Alg = CutToChar(Args, L';', true);
  3343. if ((Alg.Length() > 0) && (Alg[Alg.Length()] == L'*'))
  3344. {
  3345. Alg.Delete(Alg.Length(), 1);
  3346. }
  3347. // FTP HASH alg names follow IANA as we do,
  3348. // but using uppercase and we use lowercase
  3349. FHashAlgs->Add(LowerCase(Alg));
  3350. FSupportsAnyChecksumFeature = true;
  3351. }
  3352. }
  3353. if (FChecksumCommands->IndexOf(Command) >= 0)
  3354. {
  3355. FSupportsAnyChecksumFeature = true;
  3356. }
  3357. }
  3358. }
  3359. //---------------------------------------------------------------------------
  3360. void __fastcall TFTPFileSystem::HandleFeatReply()
  3361. {
  3362. ResetFeatures();
  3363. // Response to FEAT must be multiline, where leading and trailing line
  3364. // is "meaningless". See RFC 2389.
  3365. if ((FLastCode == 211) && (FLastResponse->Count > 2))
  3366. {
  3367. FLastResponse->Delete(0);
  3368. FLastResponse->Delete(FLastResponse->Count - 1);
  3369. for (int Index = 0; Index < FLastResponse->Count; Index++)
  3370. {
  3371. FFeatures->Add(FLastResponse->Strings[Index].Trim());
  3372. }
  3373. }
  3374. }
  3375. //---------------------------------------------------------------------------
  3376. bool __fastcall TFTPFileSystem::HandleStatus(const wchar_t * AStatus, int Type)
  3377. {
  3378. TLogLineType LogType = (TLogLineType)-1;
  3379. UnicodeString Status(AStatus);
  3380. switch (Type)
  3381. {
  3382. case TFileZillaIntf::LOG_STATUS:
  3383. FTerminal->Information(Status, true);
  3384. LogType = llMessage;
  3385. break;
  3386. case TFileZillaIntf::LOG_COMMAND:
  3387. if (Status == L"SYST")
  3388. {
  3389. // not to trigger the assert in HandleReplyStatus,
  3390. // when SYST command is used by the user
  3391. FSystem = "";
  3392. FLastCommand = SYST;
  3393. }
  3394. else if (Status == L"FEAT")
  3395. {
  3396. FLastCommand = FEAT;
  3397. }
  3398. else if (Status.SubString(1, 5) == L"PASS ")
  3399. {
  3400. FLastCommand = PASS;
  3401. }
  3402. else
  3403. {
  3404. FLastCommand = CMD_UNKNOWN;
  3405. }
  3406. if (!FLoggedIn || (FTerminal->Configuration->ActualLogProtocol >= 0))
  3407. {
  3408. LogType = llInput;
  3409. }
  3410. break;
  3411. case TFileZillaIntf::LOG_ERROR:
  3412. case TFileZillaIntf::LOG_APIERROR:
  3413. case TFileZillaIntf::LOG_WARNING:
  3414. // when timeout message occurs, break loop waiting for response code
  3415. // by setting dummy one
  3416. if (Type == TFileZillaIntf::LOG_ERROR)
  3417. {
  3418. if (StartsStr(FTimeoutStatus, Status))
  3419. {
  3420. if (NoFinalLastCode())
  3421. {
  3422. SetLastCode(DummyTimeoutCode);
  3423. }
  3424. }
  3425. else if (Status == FDisconnectStatus)
  3426. {
  3427. if (NoFinalLastCode())
  3428. {
  3429. SetLastCode(DummyDisconnectCode);
  3430. }
  3431. }
  3432. }
  3433. // there can be multiple error messages associated with single failure
  3434. // (such as "cannot open local file..." followed by "download failed")
  3435. FLastError->Add(Status);
  3436. LogType = llMessage;
  3437. break;
  3438. case TFileZillaIntf::LOG_PROGRESS:
  3439. LogType = llMessage;
  3440. break;
  3441. case TFileZillaIntf::LOG_REPLY:
  3442. HandleReplyStatus(AStatus);
  3443. if (!FLoggedIn || (FTerminal->Configuration->ActualLogProtocol >= 0))
  3444. {
  3445. LogType = llOutput;
  3446. }
  3447. break;
  3448. case TFileZillaIntf::LOG_INFO:
  3449. LogType = llMessage;
  3450. break;
  3451. case TFileZillaIntf::LOG_DEBUG:
  3452. LogType = llMessage;
  3453. break;
  3454. default:
  3455. DebugFail();
  3456. break;
  3457. }
  3458. if (FTerminal->Log->Logging && (LogType != (TLogLineType)-1))
  3459. {
  3460. FTerminal->Log->Add(LogType, Status);
  3461. }
  3462. return true;
  3463. }
  3464. //---------------------------------------------------------------------------
  3465. TDateTime __fastcall TFTPFileSystem::ConvertLocalTimestamp(time_t Time)
  3466. {
  3467. // This reverses how FZAPI converts FILETIME to time_t,
  3468. // before passing it to FZ_ASYNCREQUEST_OVERWRITE.
  3469. __int64 Timestamp;
  3470. tm * Tm = localtime(&Time);
  3471. if (Tm != NULL)
  3472. {
  3473. SYSTEMTIME SystemTime;
  3474. SystemTime.wYear = static_cast<WORD>(Tm->tm_year + 1900);
  3475. SystemTime.wMonth = static_cast<WORD>(Tm->tm_mon + 1);
  3476. SystemTime.wDayOfWeek = 0;
  3477. SystemTime.wDay = static_cast<WORD>(Tm->tm_mday);
  3478. SystemTime.wHour = static_cast<WORD>(Tm->tm_hour);
  3479. SystemTime.wMinute = static_cast<WORD>(Tm->tm_min);
  3480. SystemTime.wSecond = static_cast<WORD>(Tm->tm_sec);
  3481. SystemTime.wMilliseconds = 0;
  3482. FILETIME LocalTime;
  3483. SystemTimeToFileTime(&SystemTime, &LocalTime);
  3484. FILETIME FileTime;
  3485. LocalFileTimeToFileTime(&LocalTime, &FileTime);
  3486. Timestamp = ConvertTimestampToUnixSafe(FileTime, dstmUnix);
  3487. }
  3488. else
  3489. {
  3490. // incorrect, but at least something
  3491. Timestamp = Time;
  3492. }
  3493. return UnixToDateTime(Timestamp, dstmUnix);
  3494. }
  3495. //---------------------------------------------------------------------------
  3496. bool __fastcall TFTPFileSystem::HandleAsynchRequestOverwrite(
  3497. wchar_t * FileName1, size_t FileName1Len, const wchar_t * FileName2,
  3498. const wchar_t * Path1, const wchar_t * Path2,
  3499. __int64 Size1, __int64 Size2, time_t LocalTime,
  3500. bool /*HasLocalTime*/, const TRemoteFileTime & RemoteTime, void * AUserData, int & RequestResult)
  3501. {
  3502. if (!FActive)
  3503. {
  3504. return false;
  3505. }
  3506. else
  3507. {
  3508. TFileTransferData & UserData = *((TFileTransferData *)AUserData);
  3509. if (UserData.OverwriteResult >= 0)
  3510. {
  3511. // on retry, use the same answer as on the first attempt
  3512. RequestResult = UserData.OverwriteResult;
  3513. }
  3514. else if ((UserData.CopyParam->OnTransferOut != NULL) || (UserData.CopyParam->OnTransferIn != NULL))
  3515. {
  3516. DebugFail();
  3517. RequestResult = TFileZillaIntf::FILEEXISTS_OVERWRITE;
  3518. }
  3519. else
  3520. {
  3521. TFileOperationProgressType * OperationProgress = FTerminal->OperationProgress;
  3522. UnicodeString TargetFileName = FileName1;
  3523. DebugAssert(UserData.FileName == TargetFileName);
  3524. UnicodeString SourceFullFileName = Path2;
  3525. UnicodeString TargetFullFileName = Path1;
  3526. if (OperationProgress->Side == osLocal)
  3527. {
  3528. SourceFullFileName = IncludeTrailingBackslash(SourceFullFileName);
  3529. TargetFullFileName = UnixIncludeTrailingBackslash(TargetFullFileName);
  3530. }
  3531. else
  3532. {
  3533. SourceFullFileName = UnixIncludeTrailingBackslash(SourceFullFileName);
  3534. TargetFullFileName = IncludeTrailingBackslash(TargetFullFileName);
  3535. }
  3536. SourceFullFileName += FileName2;
  3537. TargetFullFileName += FileName1;
  3538. TOverwriteMode OverwriteMode = omOverwrite;
  3539. TOverwriteFileParams FileParams;
  3540. bool NoFileParams =
  3541. (Size1 < 0) || (LocalTime == 0) ||
  3542. (Size2 < 0) || !RemoteTime.HasDate;
  3543. if (!NoFileParams)
  3544. {
  3545. FileParams.SourceSize = Size2;
  3546. FileParams.DestSize = Size1;
  3547. // Time is coming from LIST (not from MLSD or MDTM)
  3548. bool NeedApplyTimeDifference = !RemoteTime.Utc && DebugAlwaysTrue(!FFileZillaIntf->UsingMlsd());
  3549. if (OperationProgress->Side == osLocal)
  3550. {
  3551. FileParams.SourceTimestamp = ConvertLocalTimestamp(LocalTime);
  3552. RemoteFileTimeToDateTimeAndPrecision(RemoteTime, FileParams.DestTimestamp, FileParams.DestPrecision);
  3553. if (NeedApplyTimeDifference)
  3554. {
  3555. ApplyTimeDifference(TargetFullFileName, FileParams.DestTimestamp, FileParams.DestPrecision);
  3556. }
  3557. }
  3558. else
  3559. {
  3560. FileParams.DestTimestamp = ConvertLocalTimestamp(LocalTime);
  3561. RemoteFileTimeToDateTimeAndPrecision(RemoteTime, FileParams.SourceTimestamp, FileParams.SourcePrecision);
  3562. if (NeedApplyTimeDifference)
  3563. {
  3564. ApplyTimeDifference(SourceFullFileName, FileParams.SourceTimestamp, FileParams.SourcePrecision);
  3565. }
  3566. }
  3567. }
  3568. bool AllowResume = UserData.CopyParam->AllowResume(FileParams.SourceSize, UnicodeString());
  3569. bool AutoResume = UserData.AutoResume && AllowResume;
  3570. if (ConfirmOverwrite(SourceFullFileName, TargetFileName, OverwriteMode, OperationProgress,
  3571. (NoFileParams ? NULL : &FileParams), UserData.CopyParam, UserData.Params, AutoResume))
  3572. {
  3573. switch (OverwriteMode)
  3574. {
  3575. case omOverwrite:
  3576. if (TargetFileName != FileName1)
  3577. {
  3578. wcsncpy(FileName1, TargetFileName.c_str(), FileName1Len);
  3579. FileName1[FileName1Len - 1] = L'\0';
  3580. UserData.FileName = FileName1;
  3581. RequestResult = TFileZillaIntf::FILEEXISTS_RENAME;
  3582. }
  3583. else
  3584. {
  3585. RequestResult = TFileZillaIntf::FILEEXISTS_OVERWRITE;
  3586. }
  3587. break;
  3588. case omResume:
  3589. RequestResult = TFileZillaIntf::FILEEXISTS_RESUME;
  3590. break;
  3591. case omComplete:
  3592. FTerminal->LogEvent(L"File transfer was completed before disconnect");
  3593. RequestResult = TFileZillaIntf::FILEEXISTS_COMPLETE;
  3594. break;
  3595. default:
  3596. DebugFail();
  3597. RequestResult = TFileZillaIntf::FILEEXISTS_OVERWRITE;
  3598. break;
  3599. }
  3600. }
  3601. else
  3602. {
  3603. RequestResult = TFileZillaIntf::FILEEXISTS_SKIP;
  3604. }
  3605. }
  3606. // remember the answer for the retries
  3607. UserData.OverwriteResult = RequestResult;
  3608. if (RequestResult == TFileZillaIntf::FILEEXISTS_SKIP)
  3609. {
  3610. // when user chooses not to overwrite, break loop waiting for response code
  3611. // by setting dummy one, as FZAPI won't do anything then
  3612. SetLastCode(DummyTimeoutCode);
  3613. }
  3614. return true;
  3615. }
  3616. }
  3617. //---------------------------------------------------------------------------
  3618. UnicodeString __fastcall FormatContactList(UnicodeString Entry1, UnicodeString Entry2)
  3619. {
  3620. if (!Entry1.IsEmpty() && !Entry2.IsEmpty())
  3621. {
  3622. return FORMAT(L"%s, %s", (Entry1, Entry2));
  3623. }
  3624. else
  3625. {
  3626. return Entry1 + Entry2;
  3627. }
  3628. }
  3629. //---------------------------------------------------------------------------
  3630. UnicodeString __fastcall FormatContact(const TFtpsCertificateData::TContact & Contact)
  3631. {
  3632. UnicodeString Result =
  3633. FORMAT(LoadStrPart(VERIFY_CERT_CONTACT, 1),
  3634. (FormatContactList(FormatContactList(FormatContactList(
  3635. Contact.Organization, Contact.Unit), Contact.CommonName), Contact.Mail)));
  3636. if ((wcslen(Contact.Country) > 0) ||
  3637. (wcslen(Contact.StateProvince) > 0) ||
  3638. (wcslen(Contact.Town) > 0))
  3639. {
  3640. Result +=
  3641. FORMAT(LoadStrPart(VERIFY_CERT_CONTACT, 2),
  3642. (FormatContactList(FormatContactList(
  3643. Contact.Country, Contact.StateProvince), Contact.Town)));
  3644. }
  3645. if (wcslen(Contact.Other) > 0)
  3646. {
  3647. Result += FORMAT(LoadStrPart(VERIFY_CERT_CONTACT, 3), (Contact.Other));
  3648. }
  3649. return Result;
  3650. }
  3651. //---------------------------------------------------------------------------
  3652. UnicodeString __fastcall FormatValidityTime(const TFtpsCertificateData::TValidityTime & ValidityTime)
  3653. {
  3654. return FormatDateTime(L"ddddd tt",
  3655. EncodeDateVerbose(
  3656. (unsigned short)ValidityTime.Year, (unsigned short)ValidityTime.Month,
  3657. (unsigned short)ValidityTime.Day) +
  3658. EncodeTimeVerbose(
  3659. (unsigned short)ValidityTime.Hour, (unsigned short)ValidityTime.Min,
  3660. (unsigned short)ValidityTime.Sec, 0));
  3661. }
  3662. //---------------------------------------------------------------------------
  3663. bool __fastcall VerifyNameMask(UnicodeString Name, UnicodeString Mask)
  3664. {
  3665. bool Result = true;
  3666. int Pos;
  3667. while (Result && (Pos = Mask.Pos(L"*")) > 0)
  3668. {
  3669. // Pos will typically be 1 here, so not actual comparison is done
  3670. Result = SameText(Mask.SubString(1, Pos - 1), Name.SubString(1, Pos - 1));
  3671. if (Result)
  3672. {
  3673. Mask.Delete(1, Pos); // including *
  3674. Name.Delete(1, Pos - 1);
  3675. // remove everything until the next dot
  3676. Pos = Name.Pos(L".");
  3677. if (Pos == 0)
  3678. {
  3679. Pos = Name.Length() + 1;
  3680. }
  3681. Name.Delete(1, Pos - 1);
  3682. }
  3683. }
  3684. if (Result)
  3685. {
  3686. Result = SameText(Mask, Name);
  3687. }
  3688. return Result;
  3689. }
  3690. //---------------------------------------------------------------------------
  3691. bool __fastcall TFTPFileSystem::VerifyCertificateHostName(const TFtpsCertificateData & Data)
  3692. {
  3693. UnicodeString HostName = FTerminal->SessionData->HostNameExpanded;
  3694. UnicodeString CommonName = Data.Subject.CommonName;
  3695. bool NoMask = CommonName.IsEmpty();
  3696. bool Result = !NoMask && VerifyNameMask(HostName, CommonName);
  3697. if (Result)
  3698. {
  3699. FTerminal->LogEvent(FORMAT(L"Certificate common name \"%s\" matches hostname", (CommonName)));
  3700. }
  3701. else
  3702. {
  3703. if (!NoMask && (FTerminal->Configuration->ActualLogProtocol >= 1))
  3704. {
  3705. FTerminal->LogEvent(FORMAT(L"Certificate common name \"%s\" does not match hostname", (CommonName)));
  3706. }
  3707. UnicodeString SubjectAltName = Data.SubjectAltName;
  3708. while (!Result && !SubjectAltName.IsEmpty())
  3709. {
  3710. UnicodeString Entry = CutToChar(SubjectAltName, L',', true);
  3711. UnicodeString EntryName = CutToChar(Entry, L':', true);
  3712. if (SameText(EntryName, L"DNS"))
  3713. {
  3714. NoMask = false;
  3715. Result = VerifyNameMask(HostName, Entry);
  3716. if (Result)
  3717. {
  3718. FTerminal->LogEvent(FORMAT(L"Certificate subject alternative name \"%s\" matches hostname", (Entry)));
  3719. }
  3720. else
  3721. {
  3722. if (FTerminal->Configuration->ActualLogProtocol >= 1)
  3723. {
  3724. FTerminal->LogEvent(FORMAT(L"Certificate subject alternative name \"%s\" does not match hostname", (Entry)));
  3725. }
  3726. }
  3727. }
  3728. }
  3729. }
  3730. if (!Result && NoMask)
  3731. {
  3732. FTerminal->LogEvent(L"Certificate has no common name nor subject alternative name, not verifying hostname");
  3733. Result = true;
  3734. }
  3735. return Result;
  3736. }
  3737. //---------------------------------------------------------------------------
  3738. static bool __fastcall IsIPAddress(const UnicodeString & HostName)
  3739. {
  3740. bool IPv4 = true;
  3741. bool IPv6 = true;
  3742. bool AnyColon = false;
  3743. for (int Index = 1; Index <= HostName.Length(); Index++)
  3744. {
  3745. wchar_t C = HostName[Index];
  3746. if (!IsDigit(C) && (C != L'.'))
  3747. {
  3748. IPv4 = false;
  3749. }
  3750. if (!IsHex(C) && (C != L':'))
  3751. {
  3752. IPv6 = false;
  3753. }
  3754. if (C == L':')
  3755. {
  3756. AnyColon = true;
  3757. }
  3758. }
  3759. return IPv4 || (IPv6 && AnyColon);
  3760. }
  3761. //---------------------------------------------------------------------------
  3762. bool __fastcall TFTPFileSystem::HandleAsynchRequestVerifyCertificate(
  3763. const TFtpsCertificateData & Data, int & RequestResult)
  3764. {
  3765. if (!FActive)
  3766. {
  3767. return false;
  3768. }
  3769. else
  3770. {
  3771. FSessionInfo.CertificateFingerprintSHA1 =
  3772. BytesToHex(RawByteString((const char*)Data.HashSha1, Data.HashSha1Len), false, L':');
  3773. FSessionInfo.CertificateFingerprintSHA256 =
  3774. BytesToHex(RawByteString((const char*)Data.HashSha256, Data.HashSha256Len), false, L':');
  3775. if (FTerminal->SessionData->FingerprintScan)
  3776. {
  3777. RequestResult = 0;
  3778. }
  3779. else
  3780. {
  3781. UnicodeString CertificateSubject = Data.Subject.Organization;
  3782. FTerminal->LogEvent(FORMAT(L"Verifying certificate for \"%s\" with fingerprint %s and %d failures", (CertificateSubject, FSessionInfo.CertificateFingerprintSHA256, Data.VerificationResult)));
  3783. bool Trusted = false;
  3784. bool TryWindowsSystemCertificateStore = false;
  3785. UnicodeString VerificationResultStr;
  3786. switch (Data.VerificationResult)
  3787. {
  3788. case X509_V_OK:
  3789. Trusted = true;
  3790. break;
  3791. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  3792. VerificationResultStr = LoadStr(CERT_ERR_UNABLE_TO_GET_ISSUER_CERT);
  3793. TryWindowsSystemCertificateStore = true;
  3794. break;
  3795. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  3796. VerificationResultStr = LoadStr(CERT_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE);
  3797. break;
  3798. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  3799. VerificationResultStr = LoadStr(CERT_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY);
  3800. break;
  3801. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  3802. VerificationResultStr = LoadStr(CERT_ERR_CERT_SIGNATURE_FAILURE);
  3803. break;
  3804. case X509_V_ERR_CERT_NOT_YET_VALID:
  3805. VerificationResultStr = LoadStr(CERT_ERR_CERT_NOT_YET_VALID);
  3806. break;
  3807. case X509_V_ERR_CERT_HAS_EXPIRED:
  3808. VerificationResultStr = LoadStr(CERT_ERR_CERT_HAS_EXPIRED);
  3809. break;
  3810. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  3811. VerificationResultStr = LoadStr(CERT_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD);
  3812. break;
  3813. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  3814. VerificationResultStr = LoadStr(CERT_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD);
  3815. break;
  3816. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  3817. VerificationResultStr = LoadStr(CERT_ERR_DEPTH_ZERO_SELF_SIGNED_CERT);
  3818. TryWindowsSystemCertificateStore = true;
  3819. break;
  3820. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  3821. VerificationResultStr = LoadStr(CERT_ERR_SELF_SIGNED_CERT_IN_CHAIN);
  3822. TryWindowsSystemCertificateStore = true;
  3823. break;
  3824. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  3825. VerificationResultStr = LoadStr(CERT_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY);
  3826. TryWindowsSystemCertificateStore = true;
  3827. break;
  3828. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  3829. VerificationResultStr = LoadStr(CERT_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
  3830. TryWindowsSystemCertificateStore = true;
  3831. break;
  3832. case X509_V_ERR_INVALID_CA:
  3833. VerificationResultStr = LoadStr(CERT_ERR_INVALID_CA);
  3834. break;
  3835. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  3836. VerificationResultStr = LoadStr(CERT_ERR_PATH_LENGTH_EXCEEDED);
  3837. break;
  3838. case X509_V_ERR_INVALID_PURPOSE:
  3839. VerificationResultStr = LoadStr(CERT_ERR_INVALID_PURPOSE);
  3840. break;
  3841. case X509_V_ERR_CERT_UNTRUSTED:
  3842. VerificationResultStr = LoadStr(CERT_ERR_CERT_UNTRUSTED);
  3843. TryWindowsSystemCertificateStore = true;
  3844. break;
  3845. case X509_V_ERR_CERT_REJECTED:
  3846. VerificationResultStr = LoadStr(CERT_ERR_CERT_REJECTED);
  3847. break;
  3848. case X509_V_ERR_KEYUSAGE_NO_CERTSIGN:
  3849. VerificationResultStr = LoadStr(CERT_ERR_KEYUSAGE_NO_CERTSIGN);
  3850. break;
  3851. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  3852. VerificationResultStr = LoadStr(CERT_ERR_CERT_CHAIN_TOO_LONG);
  3853. break;
  3854. default:
  3855. VerificationResultStr =
  3856. FORMAT(L"%s (%s)",
  3857. (LoadStr(CERT_ERR_UNKNOWN), X509_verify_cert_error_string(Data.VerificationResult)));
  3858. break;
  3859. }
  3860. bool IsHostNameIPAddress = IsIPAddress(FTerminal->SessionData->HostNameExpanded);
  3861. bool CertificateHostNameVerified = !IsHostNameIPAddress && VerifyCertificateHostName(Data);
  3862. bool VerificationResult = Trusted;
  3863. if (IsHostNameIPAddress || !CertificateHostNameVerified)
  3864. {
  3865. VerificationResult = false;
  3866. TryWindowsSystemCertificateStore = false;
  3867. }
  3868. if (!VerificationResult)
  3869. {
  3870. if (FTerminal->VerifyCertificate(FtpsCertificateStorageKey, FTerminal->SessionData->SiteKey,
  3871. FSessionInfo.CertificateFingerprintSHA1, FSessionInfo.CertificateFingerprintSHA256,
  3872. CertificateSubject, Data.VerificationResult))
  3873. {
  3874. // certificate is trusted, but for not purposes of info dialog
  3875. VerificationResult = true;
  3876. FSessionInfo.CertificateVerifiedManually = true;
  3877. }
  3878. }
  3879. // TryWindowsSystemCertificateStore is set for the same set of failures
  3880. // as trigger NE_SSL_UNTRUSTED flag in ne_openssl.c's verify_callback().
  3881. // Use WindowsValidateCertificate only as a last resort (after checking the cached fingerprint)
  3882. // as it can take a very long time (up to 1 minute).
  3883. if (!VerificationResult && TryWindowsSystemCertificateStore)
  3884. {
  3885. UnicodeString WindowsCertificateError;
  3886. if (WindowsValidateCertificate(Data.Certificate, Data.CertificateLen, WindowsCertificateError))
  3887. {
  3888. FTerminal->LogEvent(L"Certificate verified against Windows certificate store");
  3889. VerificationResult = true;
  3890. // certificate is trusted for all purposes
  3891. Trusted = true;
  3892. }
  3893. else
  3894. {
  3895. FTerminal->LogEvent(
  3896. FORMAT(L"Certificate failed to verify against Windows certificate store: %s", (DefaultStr(WindowsCertificateError, L"no details"))));
  3897. }
  3898. }
  3899. const UnicodeString SummarySeparator = L"\n\n";
  3900. UnicodeString Summary;
  3901. // even if the fingerprint is cached, the certificate is still not trusted for a purposes of the info dialog.
  3902. if (!Trusted)
  3903. {
  3904. AddToList(Summary, VerificationResultStr + L" " + FMTLOAD(CERT_ERRDEPTH, (Data.VerificationDepth + 1)), SummarySeparator);
  3905. }
  3906. if (IsHostNameIPAddress)
  3907. {
  3908. AddToList(Summary, FMTLOAD(CERT_IP_CANNOT_VERIFY, (FTerminal->SessionData->HostNameExpanded)), SummarySeparator);
  3909. }
  3910. else if (!CertificateHostNameVerified)
  3911. {
  3912. AddToList(Summary, FMTLOAD(CERT_NAME_MISMATCH, (FTerminal->SessionData->HostNameExpanded)), SummarySeparator);
  3913. }
  3914. if (Summary.IsEmpty())
  3915. {
  3916. Summary = LoadStr(CERT_OK);
  3917. }
  3918. FSessionInfo.Certificate =
  3919. FMTLOAD(CERT_TEXT2, (
  3920. FormatContact(Data.Issuer),
  3921. FormatContact(Data.Subject),
  3922. FormatValidityTime(Data.ValidFrom),
  3923. FormatValidityTime(Data.ValidUntil),
  3924. FSessionInfo.CertificateFingerprintSHA256,
  3925. FSessionInfo.CertificateFingerprintSHA1,
  3926. Summary));
  3927. RequestResult = VerificationResult ? 1 : 0;
  3928. if (RequestResult == 0)
  3929. {
  3930. if (FTerminal->ConfirmCertificate(FSessionInfo, Data.VerificationResult, FtpsCertificateStorageKey, true))
  3931. {
  3932. // FZ's VerifyCertDlg.cpp returns 2 for "cached", what we do nto distinguish here,
  3933. // however FZAPI takes all non-zero values equally.
  3934. RequestResult = 1;
  3935. FSessionInfo.CertificateVerifiedManually = true;
  3936. }
  3937. }
  3938. }
  3939. return true;
  3940. }
  3941. }
  3942. //---------------------------------------------------------------------------
  3943. bool __fastcall TFTPFileSystem::HandleAsynchRequestNeedPass(
  3944. struct TNeedPassRequestData & Data, int & RequestResult)
  3945. {
  3946. if (!FActive)
  3947. {
  3948. return false;
  3949. }
  3950. else
  3951. {
  3952. UnicodeString Password;
  3953. if (FCertificate != NULL)
  3954. {
  3955. FTerminal->LogEvent(L"Server asked for password, but we are using certificate, and no password was specified upfront, using fake password");
  3956. Password = L"USINGCERT";
  3957. RequestResult = TFileZillaIntf::REPLY_OK;
  3958. }
  3959. else
  3960. {
  3961. if (FTerminal->PromptUser(FTerminal->SessionData, pkPassword, LoadStr(PASSWORD_TITLE), L"",
  3962. LoadStr(PASSWORD_PROMPT), false, 0, Password))
  3963. {
  3964. RequestResult = TFileZillaIntf::REPLY_OK;
  3965. }
  3966. else
  3967. {
  3968. RequestResult = TFileZillaIntf::REPLY_ABORTED;
  3969. }
  3970. }
  3971. // When returning REPLY_OK, we need to return an allocated password,
  3972. // even if we were returning and empty string we got on input.
  3973. if (RequestResult == TFileZillaIntf::REPLY_OK)
  3974. {
  3975. Data.Password = _wcsdup(Password.c_str());
  3976. }
  3977. return true;
  3978. }
  3979. }
  3980. //---------------------------------------------------------------------------
  3981. void __fastcall TFTPFileSystem::RemoteFileTimeToDateTimeAndPrecision(const TRemoteFileTime & Source, TDateTime & DateTime, TModificationFmt & ModificationFmt)
  3982. {
  3983. // ModificationFmt must be set after Modification
  3984. if (Source.HasDate)
  3985. {
  3986. DateTime =
  3987. EncodeDateVerbose((unsigned short)Source.Year, (unsigned short)Source.Month,
  3988. (unsigned short)Source.Day);
  3989. if (Source.HasTime)
  3990. {
  3991. DateTime = DateTime +
  3992. EncodeTimeVerbose((unsigned short)Source.Hour, (unsigned short)Source.Minute,
  3993. (unsigned short)Source.Second, 0);
  3994. ModificationFmt = Source.HasSeconds ? mfFull : (Source.HasYear ? mfYMDHM : mfMDHM);
  3995. // With IIS, the Utc should be false only for MDTM
  3996. if (FWindowsServer && !Source.Utc)
  3997. {
  3998. DateTime -= DSTDifferenceForTime(DateTime);
  3999. }
  4000. }
  4001. else
  4002. {
  4003. ModificationFmt = mfMDY;
  4004. }
  4005. if (Source.Utc)
  4006. {
  4007. DateTime = ConvertTimestampFromUTC(DateTime);
  4008. }
  4009. }
  4010. else
  4011. {
  4012. // With SCP we estimate date to be today, if we have at least time
  4013. DateTime = double(0);
  4014. ModificationFmt = mfNone;
  4015. }
  4016. }
  4017. //---------------------------------------------------------------------------
  4018. bool __fastcall TFTPFileSystem::HandleListData(const wchar_t * Path,
  4019. const TListDataEntry * Entries, unsigned int Count)
  4020. {
  4021. if (!FActive)
  4022. {
  4023. return false;
  4024. }
  4025. else if (FIgnoreFileList)
  4026. {
  4027. // directory listing provided implicitly by FZAPI during certain operations is ignored
  4028. DebugAssert(FFileList == NULL);
  4029. return false;
  4030. }
  4031. else
  4032. {
  4033. DebugAssert(FFileList != NULL);
  4034. DebugUsedParam(Path);
  4035. for (unsigned int Index = 0; Index < Count; Index++)
  4036. {
  4037. const TListDataEntry * Entry = &Entries[Index];
  4038. std::unique_ptr<TRemoteFile> File(new TRemoteFile());
  4039. try
  4040. {
  4041. File->Terminal = FTerminal;
  4042. File->FileName = Entry->Name;
  4043. try
  4044. {
  4045. int PermissionsLen = wcslen(Entry->Permissions);
  4046. if (PermissionsLen >= 10)
  4047. {
  4048. File->Rights->Text = Entry->Permissions + 1;
  4049. }
  4050. else if ((PermissionsLen == 3) || (PermissionsLen == 4))
  4051. {
  4052. File->Rights->Octal = Entry->Permissions;
  4053. }
  4054. }
  4055. catch(...)
  4056. {
  4057. // ignore permission errors with FTP
  4058. }
  4059. File->HumanRights = Entry->HumanPerm;
  4060. // deprecated, to be replaced with Owner/Group
  4061. if (wcslen(Entry->OwnerGroup) > 0)
  4062. {
  4063. const wchar_t * Space = wcschr(Entry->OwnerGroup, L' ');
  4064. if (Space != NULL)
  4065. {
  4066. File->Owner.Name = UnicodeString(Entry->OwnerGroup, Space - Entry->OwnerGroup);
  4067. File->Group.Name = Space + 1;
  4068. }
  4069. else
  4070. {
  4071. File->Owner.Name = Entry->OwnerGroup;
  4072. }
  4073. }
  4074. else
  4075. {
  4076. File->Owner.Name = Entry->Owner;
  4077. File->Group.Name = Entry->Group;
  4078. }
  4079. File->Size = Entry->Size;
  4080. if (Entry->Link)
  4081. {
  4082. File->Type = FILETYPE_SYMLINK;
  4083. }
  4084. else if (Entry->Dir)
  4085. {
  4086. File->Type = FILETYPE_DIRECTORY;
  4087. }
  4088. else
  4089. {
  4090. File->Type = FILETYPE_DEFAULT;
  4091. }
  4092. TDateTime Modification;
  4093. TModificationFmt ModificationFmt;
  4094. RemoteFileTimeToDateTimeAndPrecision(Entry->Time, Modification, ModificationFmt);
  4095. File->Modification = Modification;
  4096. File->ModificationFmt = ModificationFmt;
  4097. File->LastAccess = File->Modification;
  4098. File->LinkTo = Entry->LinkTarget;
  4099. File->Complete();
  4100. }
  4101. catch (Exception & E)
  4102. {
  4103. UnicodeString EntryData =
  4104. FORMAT(L"%s/%s/%s/%s/%s/%s/%s/%d/%d/%d/%d/%d/%d/%d/%d/%d/%d/%d",
  4105. (Entry->Name, Entry->Permissions, Entry->HumanPerm, Entry->Owner, Entry->Group, Entry->OwnerGroup, IntToStr(Entry->Size),
  4106. int(Entry->Dir), int(Entry->Link), Entry->Time.Year, Entry->Time.Month, Entry->Time.Day,
  4107. Entry->Time.Hour, Entry->Time.Minute, int(Entry->Time.HasTime),
  4108. int(Entry->Time.HasYear), int(Entry->Time.HasSeconds), int(Entry->Time.HasDate)));
  4109. throw ETerminal(&E, FMTLOAD(LIST_LINE_ERROR, (EntryData)), HELP_LIST_LINE_ERROR);
  4110. }
  4111. if (FTerminal->IsValidFile(File.get()))
  4112. {
  4113. FFileList->AddFile(File.release());
  4114. }
  4115. }
  4116. return true;
  4117. }
  4118. }
  4119. //---------------------------------------------------------------------------
  4120. bool __fastcall TFTPFileSystem::HandleTransferStatus(bool Valid, __int64 TransferSize,
  4121. __int64 Bytes, bool FileTransfer)
  4122. {
  4123. if (!FActive)
  4124. {
  4125. return false;
  4126. }
  4127. else if (!Valid)
  4128. {
  4129. }
  4130. else if (FileTransfer)
  4131. {
  4132. FileTransferProgress(TransferSize, Bytes);
  4133. }
  4134. else
  4135. {
  4136. ReadDirectoryProgress(Bytes);
  4137. }
  4138. return true;
  4139. }
  4140. //---------------------------------------------------------------------------
  4141. bool __fastcall TFTPFileSystem::HandleReply(int Command, unsigned int Reply)
  4142. {
  4143. if (!FActive)
  4144. {
  4145. return false;
  4146. }
  4147. else
  4148. {
  4149. if (FTerminal->Configuration->ActualLogProtocol >= 1)
  4150. {
  4151. FTerminal->LogEvent(FORMAT(L"Got reply %x to the command %d", (int(Reply), Command)));
  4152. }
  4153. // reply with Command 0 is not associated with current operation
  4154. // so do not treat it as a reply
  4155. // (it is typically used asynchronously to notify about disconnects)
  4156. if (Command != 0)
  4157. {
  4158. DebugAssert(FCommandReply == 0);
  4159. FCommandReply = Reply;
  4160. }
  4161. else
  4162. {
  4163. DebugAssert(FReply == 0);
  4164. FReply = Reply;
  4165. }
  4166. return true;
  4167. }
  4168. }
  4169. //---------------------------------------------------------------------------
  4170. bool __fastcall TFTPFileSystem::HandleCapabilities(
  4171. TFTPServerCapabilities * ServerCapabilities)
  4172. {
  4173. FServerCapabilities->Assign(ServerCapabilities);
  4174. FFileSystemInfoValid = false;
  4175. return true;
  4176. }
  4177. //---------------------------------------------------------------------------
  4178. bool __fastcall TFTPFileSystem::CheckError(int ReturnCode, const wchar_t * Context)
  4179. {
  4180. // we do not expect any FZAPI call to fail as it generally can fail only due to:
  4181. // - invalid parameters
  4182. // - busy FZAPI core
  4183. // the only exception is REPLY_NOTCONNECTED that can happen if
  4184. // connection is closed just between the last call to Idle()
  4185. // and call to any FZAPI command
  4186. // in such case reply without associated command is posted,
  4187. // which we are going to wait for unless we are already waiting
  4188. // on higher level (this typically happens if connection is lost while
  4189. // waiting for user interaction and is detected within call to
  4190. // SetAsyncRequestResult)
  4191. if (FLAGSET(ReturnCode, TFileZillaIntf::REPLY_NOTCONNECTED))
  4192. {
  4193. if (!FWaitingForReply)
  4194. {
  4195. // throws
  4196. WaitForFatalNonCommandReply();
  4197. }
  4198. }
  4199. else
  4200. {
  4201. FTerminal->FatalError(NULL,
  4202. FMTLOAD(INTERNAL_ERROR, (FORMAT(L"fz#%s", (Context)), IntToHex(ReturnCode, 4))));
  4203. DebugFail();
  4204. }
  4205. return false;
  4206. }
  4207. //---------------------------------------------------------------------------
  4208. bool __fastcall TFTPFileSystem::Unquote(UnicodeString & Str)
  4209. {
  4210. enum
  4211. {
  4212. INIT,
  4213. QUOTE,
  4214. QUOTED,
  4215. DONE
  4216. } State;
  4217. State = INIT;
  4218. DebugAssert((Str.Length() > 0) && ((Str[1] == L'"') || (Str[1] == L'\'')));
  4219. int Index = 1;
  4220. wchar_t Quote;
  4221. while (Index <= Str.Length())
  4222. {
  4223. switch (State)
  4224. {
  4225. case INIT:
  4226. if ((Str[Index] == L'"') || (Str[Index] == L'\''))
  4227. {
  4228. Quote = Str[Index];
  4229. State = QUOTED;
  4230. Str.Delete(Index, 1);
  4231. }
  4232. else
  4233. {
  4234. DebugFail();
  4235. // no quoted string
  4236. Str.SetLength(0);
  4237. }
  4238. break;
  4239. case QUOTED:
  4240. if (Str[Index] == Quote)
  4241. {
  4242. State = QUOTE;
  4243. Str.Delete(Index, 1);
  4244. }
  4245. else
  4246. {
  4247. Index++;
  4248. }
  4249. break;
  4250. case QUOTE:
  4251. if (Str[Index] == Quote)
  4252. {
  4253. Index++;
  4254. }
  4255. else
  4256. {
  4257. // end of quoted string, trim the rest
  4258. Str.SetLength(Index - 1);
  4259. State = DONE;
  4260. }
  4261. break;
  4262. }
  4263. }
  4264. return (State == DONE);
  4265. }
  4266. //---------------------------------------------------------------------------
  4267. void __fastcall TFTPFileSystem::PreserveDownloadFileTime(HANDLE Handle, void * UserData)
  4268. {
  4269. TFileTransferData * Data = static_cast<TFileTransferData *>(UserData);
  4270. DebugAssert(Data->CopyParam->OnTransferOut == NULL);
  4271. FTerminal->UpdateTargetTime(Handle, Data->Modification, mfFull, dstmUnix);
  4272. }
  4273. //---------------------------------------------------------------------------
  4274. bool __fastcall TFTPFileSystem::GetFileModificationTimeInUtc(const wchar_t * FileName, struct tm & Time)
  4275. {
  4276. bool Result;
  4277. try
  4278. {
  4279. // error-handling-free and DST-mode-inaware copy of TTerminal::OpenLocalFile
  4280. HANDLE Handle = CreateFile(ApiPath(FileName).c_str(), GENERIC_READ,
  4281. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
  4282. if (Handle == INVALID_HANDLE_VALUE)
  4283. {
  4284. Result = false;
  4285. }
  4286. else
  4287. {
  4288. FILETIME MTime;
  4289. if (!GetFileTime(Handle, NULL, NULL, &MTime))
  4290. {
  4291. Result = false;
  4292. }
  4293. else
  4294. {
  4295. TDateTime Modification = ConvertTimestampToUTC(FileTimeToDateTime(MTime));
  4296. unsigned short Year;
  4297. unsigned short Month;
  4298. unsigned short Day;
  4299. Modification.DecodeDate(&Year, &Month, &Day);
  4300. Time.tm_year = Year - 1900;
  4301. Time.tm_mon = Month - 1;
  4302. Time.tm_mday = Day;
  4303. unsigned short Hour;
  4304. unsigned short Min;
  4305. unsigned short Sec;
  4306. unsigned short MSec;
  4307. Modification.DecodeTime(&Hour, &Min, &Sec, &MSec);
  4308. Time.tm_hour = Hour;
  4309. Time.tm_min = Min;
  4310. Time.tm_sec = Sec;
  4311. Result = true;
  4312. }
  4313. CloseHandle(Handle);
  4314. }
  4315. }
  4316. catch (...)
  4317. {
  4318. Result = false;
  4319. }
  4320. return Result;
  4321. }
  4322. //---------------------------------------------------------------------------
  4323. void __fastcall TFTPFileSystem::RegisterChecksumAlgCommand(const UnicodeString & Alg, const UnicodeString & Command)
  4324. {
  4325. FChecksumAlgs->Add(Alg);
  4326. FChecksumCommands->Add(Command);
  4327. }
  4328. //---------------------------------------------------------------------------
  4329. void __fastcall TFTPFileSystem::GetSupportedChecksumAlgs(TStrings * Algs)
  4330. {
  4331. for (int Index = 0; Index < FHashAlgs->Count; Index++)
  4332. {
  4333. Algs->Add(FHashAlgs->Strings[Index]);
  4334. }
  4335. for (int Index = 0; Index < FChecksumAlgs->Count; Index++)
  4336. {
  4337. UnicodeString Alg = FChecksumAlgs->Strings[Index];
  4338. UnicodeString Command = FChecksumCommands->Strings[Index];
  4339. if (SupportsCommand(Command) && (Algs->IndexOf(Alg) < 0))
  4340. {
  4341. Algs->Add(Alg);
  4342. }
  4343. }
  4344. }
  4345. //---------------------------------------------------------------------------
  4346. bool __fastcall TFTPFileSystem::SupportsSiteCommand(const UnicodeString & Command) const
  4347. {
  4348. return (FSupportedSiteCommands->IndexOf(Command) >= 0);
  4349. }
  4350. //---------------------------------------------------------------------------
  4351. bool __fastcall TFTPFileSystem::SupportsCommand(const UnicodeString & Command) const
  4352. {
  4353. return (FSupportedCommands->IndexOf(Command) >= 0);
  4354. }
  4355. //---------------------------------------------------------------------------
  4356. void __fastcall TFTPFileSystem::LockFile(const UnicodeString & /*FileName*/, const TRemoteFile * /*File*/)
  4357. {
  4358. DebugFail();
  4359. }
  4360. //---------------------------------------------------------------------------
  4361. void __fastcall TFTPFileSystem::UnlockFile(const UnicodeString & /*FileName*/, const TRemoteFile * /*File*/)
  4362. {
  4363. DebugFail();
  4364. }
  4365. //---------------------------------------------------------------------------
  4366. void __fastcall TFTPFileSystem::UpdateFromMain(TCustomFileSystem * /*MainFileSystem*/)
  4367. {
  4368. // noop
  4369. }
  4370. //---------------------------------------------------------------------------
  4371. void __fastcall TFTPFileSystem::ClearCaches()
  4372. {
  4373. // noop
  4374. }
  4375. //---------------------------------------------------------------------------
  4376. UnicodeString __fastcall GetOpenSSLVersionText()
  4377. {
  4378. return OPENSSL_VERSION_TEXT;
  4379. }
  4380. //---------------------------------------------------------------------------