FtpFileSystem.cpp 157 KB

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