FtpControlSocket.cpp 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // CFtpControlSocket.cpp: Implementierungsdatei
  15. //
  16. #include "stdafx.h"
  17. #include "FtpControlSocket.h"
  18. #include "mainthread.h"
  19. #include "transfersocket.h"
  20. #ifndef MPEXT
  21. #include "fileexistsdlg.h"
  22. #endif
  23. #include "pathfunctions.h"
  24. #include "asyncproxysocketlayer.h"
  25. #ifndef MPEXT_NO_SSL
  26. #include "AsyncSslSocketLayer.h"
  27. #endif
  28. #ifndef MPEXT_NO_GSS
  29. #include "AsyncGssSocketLayer.h"
  30. #endif
  31. #include "filezillaapi.h"
  32. #include "misc/utf8.h"
  33. #ifdef _DEBUG
  34. #define new DEBUG_NEW
  35. #undef THIS_FILE
  36. static char THIS_FILE[] = __FILE__;
  37. #endif
  38. class CFtpControlSocket::CFileTransferData : public CFtpControlSocket::t_operation::COpData
  39. {
  40. public:
  41. CFileTransferData()
  42. {
  43. pDirectoryListing=0;
  44. nGotTransferEndReply=0;
  45. nWaitNextOpState=0;
  46. nMKDOpState=-1;
  47. pFileTime=0;
  48. pFileSize=0;
  49. bUseAbsolutePaths = FALSE;
  50. bTriedPortPasvOnce = FALSE;
  51. askOnResumeFail = false;
  52. };
  53. ~CFileTransferData()
  54. {
  55. if (pDirectoryListing)
  56. delete pDirectoryListing;
  57. pDirectoryListing=0;
  58. delete pFileSize;
  59. delete pFileTime;
  60. };
  61. CString rawpwd;
  62. t_transferfile transferfile;
  63. t_transferdata transferdata;
  64. CString host;
  65. int port;
  66. BOOL bPasv;
  67. int nGotTransferEndReply;
  68. t_directory *pDirectoryListing;
  69. int nWaitNextOpState;
  70. CServerPath MKDCurrent;
  71. std::list<CString> MKDSegments;
  72. int nMKDOpState;
  73. CTime ListStartTime;
  74. CTime *pFileTime; //Used when downloading and OPTION_PRESERVEDOWNLOADFILETIME is set or when LIST fails
  75. _int64 *pFileSize; //Used when LIST failes
  76. BOOL bUseAbsolutePaths;
  77. BOOL bTriedPortPasvOnce;
  78. #ifndef MPEXT_NO_ZLIB
  79. int newZlibLevel;
  80. #endif
  81. bool askOnResumeFail;
  82. };
  83. class CFtpControlSocket::CListData:public CFtpControlSocket::t_operation::COpData
  84. {
  85. public:
  86. CListData()
  87. {
  88. pDirectoryListing = 0;
  89. bTriedPortPasvOnce = FALSE;
  90. lastCmdSentCDUP = false;
  91. }
  92. virtual ~CListData()
  93. {
  94. if (pDirectoryListing)
  95. delete pDirectoryListing;
  96. }
  97. CString rawpwd;
  98. CServerPath path;
  99. CString subdir;
  100. int nListMode;
  101. BOOL bPasv;
  102. CString host;
  103. UINT port;
  104. int nFinish;
  105. t_directory *pDirectoryListing;
  106. CTime ListStartTime;
  107. BOOL bTriedPortPasvOnce;
  108. #ifndef MPEXT_NO_ZLIB
  109. int newZlibLevel;
  110. #endif
  111. bool lastCmdSentCDUP;
  112. };
  113. class CFtpControlSocket::CMakeDirData : public CFtpControlSocket::t_operation::COpData
  114. {
  115. public:
  116. CMakeDirData() {}
  117. virtual ~CMakeDirData() {}
  118. CServerPath path;
  119. CServerPath Current;
  120. std::list<CString> Segments;
  121. };
  122. #define MKD_INIT -1
  123. #define MKD_FINDPARENT 0
  124. #define MKD_MAKESUBDIRS 1
  125. #define MKD_CHANGETOSUBDIR 2
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CFtpControlSocket
  128. CFtpControlSocket::CFtpControlSocket(CMainThread *pMainThread) : CControlSocket(pMainThread)
  129. {
  130. ASSERT(pMainThread);
  131. m_Operation.nOpMode=0;
  132. m_Operation.nOpState=-1;
  133. m_Operation.pData=0;
  134. m_pTransferSocket=0;
  135. m_pDataFile=0;
  136. m_pDirectoryListing=0;
  137. m_pOwner=pMainThread;
  138. srand( (unsigned)time( NULL ) );
  139. m_bKeepAliveActive=FALSE;
  140. m_bCheckForTimeout=TRUE;
  141. #ifndef MPEXT_NO_SSL
  142. m_bDidRejectCertificate = FALSE;
  143. #endif
  144. #ifndef MPEXT_NO_ZLIB
  145. m_useZlib = false;
  146. m_zlibSupported = false;
  147. m_zlibLevel = 8;
  148. #endif
  149. m_bUTF8 = true;
  150. m_hasClntCmd = false;
  151. m_awaitsReply = false;
  152. m_skipReply = false;
  153. m_sendBuffer = 0;
  154. m_sendBufferLen = 0;
  155. m_bProtP = false;
  156. m_mayBeMvsFilesystem = false;
  157. m_mayBeBS2000Filesystem = false;
  158. }
  159. CFtpControlSocket::~CFtpControlSocket()
  160. {
  161. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("~CFtpControlSocket()"));
  162. DoClose();
  163. if (m_pTransferSocket)
  164. {
  165. m_pTransferSocket->Close();
  166. delete m_pTransferSocket;
  167. m_pTransferSocket=0;
  168. }
  169. if (m_pDataFile)
  170. {
  171. delete m_pDataFile;
  172. m_pDataFile=0;
  173. }
  174. }
  175. /////////////////////////////////////////////////////////////////////////////
  176. // Member-Funktion CFtpControlSocket
  177. #define CONNECT_INIT -1
  178. #ifndef MPEXT_NO_GSS
  179. #define CONNECT_GSS_INIT -2
  180. #define CONNECT_GSS_AUTHDONE -3
  181. #define CONNECT_GSS_CWD -4
  182. #define CONNECT_GSS_FAILED -5
  183. #define CONNECT_GSS_NEEDPASS -6
  184. #define CONNECT_GSS_NEEDUSER -7
  185. #endif
  186. #ifndef MPEXT_NO_SSL
  187. #define CONNECT_SSL_INIT -8
  188. #define CONNECT_SSL_NEGOTIATE -9
  189. #define CONNECT_SSL_WAITDONE -10
  190. #define CONNECT_SSL_PBSZ -11
  191. #define CONNECT_SSL_PROT -12
  192. #endif
  193. #define CONNECT_FEAT -13
  194. #define CONNECT_SYST -14
  195. #define CONNECT_OPTSUTF8 -15
  196. #define CONNECT_CLNT -16
  197. bool CFtpControlSocket::InitConnect()
  198. {
  199. USES_CONVERSION;
  200. // Reset detected capabilities
  201. m_bAnnouncesUTF8 = false;
  202. m_hasClntCmd = false;
  203. m_isFileZilla = false;
  204. if (m_CurrentServer.nUTF8 == 2)
  205. m_bUTF8 = false;
  206. else
  207. m_bUTF8 = true;
  208. // Some sanity checks
  209. if (m_pOwner->IsConnected())
  210. {
  211. ShowStatus(_T("Internal error: Connect called while still connected"), 1);
  212. if (!m_Operation.nOpMode)
  213. m_Operation.nOpMode = CSMODE_CONNECT;
  214. DoClose(FZ_REPLY_CRITICALERROR);
  215. return false;
  216. }
  217. #ifndef MPEXT_NO_SSL
  218. if (m_pSslLayer)
  219. {
  220. ShowStatus(_T("Internal error: m_pSslLayer not zero in Connect"), 1);
  221. DoClose(FZ_REPLY_CRITICALERROR);
  222. return false;
  223. }
  224. #endif
  225. if (m_pProxyLayer)
  226. {
  227. ShowStatus(_T("Internal error: m_pProxyLayer not zero in Connect"), 1);
  228. DoClose(FZ_REPLY_CRITICALERROR);
  229. return false;
  230. }
  231. #ifndef MPEXT_NO_SSL
  232. if (m_CurrentServer.nServerType & FZ_SERVERTYPE_LAYER_SSL_IMPLICIT ||
  233. m_CurrentServer.nServerType & FZ_SERVERTYPE_LAYER_SSL_EXPLICIT ||
  234. m_CurrentServer.nServerType & FZ_SERVERTYPE_LAYER_TLS_EXPLICIT)
  235. {
  236. m_pSslLayer = new CAsyncSslSocketLayer;
  237. AddLayer(m_pSslLayer);
  238. TCHAR buffer[1000];
  239. GetModuleFileName(NULL, buffer, 1000);
  240. CString filename = buffer;
  241. int pos = filename.ReverseFind('\\');
  242. if (pos != -1)
  243. {
  244. filename = filename.Left(pos + 1);
  245. filename += _T("cacert.pem");
  246. m_pSslLayer->SetCertStorage(filename);
  247. }
  248. else
  249. filename = "cacert.pem";
  250. }
  251. #endif
  252. if (!m_CurrentServer.fwbypass)
  253. {
  254. int nProxyType = COptions::GetOptionVal(OPTION_PROXYTYPE);
  255. if (nProxyType != PROXYTYPE_NOPROXY)
  256. {
  257. m_pProxyLayer = new CAsyncProxySocketLayer;
  258. if (nProxyType == PROXYTYPE_SOCKS4)
  259. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4, T2CA(COptions::GetOption(OPTION_PROXYHOST)), COptions::GetOptionVal(OPTION_PROXYPORT));
  260. else if (nProxyType==PROXYTYPE_SOCKS4A)
  261. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS4A, T2CA(COptions::GetOption(OPTION_PROXYHOST)),COptions::GetOptionVal(OPTION_PROXYPORT));
  262. else if (nProxyType==PROXYTYPE_SOCKS5)
  263. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  264. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  265. COptions::GetOptionVal(OPTION_PROXYPORT),
  266. T2CA(COptions::GetOption(OPTION_PROXYUSER)),
  267. T2CA(CCrypt::decrypt(COptions::GetOption(OPTION_PROXYPASS))));
  268. else
  269. m_pProxyLayer->SetProxy(PROXYTYPE_SOCKS5, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  270. COptions::GetOptionVal(OPTION_PROXYPORT));
  271. else if (nProxyType==PROXYTYPE_HTTP11)
  272. if (COptions::GetOptionVal(OPTION_PROXYUSELOGON))
  273. m_pProxyLayer->SetProxy(PROXYTYPE_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)),
  274. COptions::GetOptionVal(OPTION_PROXYPORT),
  275. T2CA(COptions::GetOption(OPTION_PROXYUSER)),
  276. T2CA(CCrypt::decrypt(COptions::GetOption(OPTION_PROXYPASS))));
  277. else
  278. m_pProxyLayer->SetProxy(PROXYTYPE_HTTP11, T2CA(COptions::GetOption(OPTION_PROXYHOST)) ,COptions::GetOptionVal(OPTION_PROXYPORT));
  279. else
  280. ASSERT(FALSE);
  281. AddLayer(m_pProxyLayer);
  282. }
  283. }
  284. #ifndef MPEXT_NO_GSS
  285. BOOL bUseGSS = FALSE;
  286. if (COptions::GetOptionVal(OPTION_USEGSS)
  287. #ifndef MPEXT_NO_SSL
  288. && !m_pSslLayer
  289. #endif
  290. )
  291. {
  292. CString GssServers=COptions::GetOption(OPTION_GSSSERVERS);
  293. LPCSTR lpszAscii=T2CA(m_CurrentServer.host);
  294. hostent *fullname=gethostbyname(lpszAscii);
  295. CString host;
  296. if (fullname)
  297. host=fullname->h_name;
  298. else
  299. host=m_CurrentServer.host;
  300. host.MakeLower();
  301. int i;
  302. while ((i=GssServers.Find(_T(";")))!=-1)
  303. {
  304. if (("."+GssServers.Left(i))==host.Right(GssServers.Left(i).GetLength()+1) || GssServers.Left(i)==host)
  305. {
  306. bUseGSS=TRUE;
  307. break;
  308. }
  309. GssServers=GssServers.Mid(i+1);
  310. }
  311. }
  312. if (bUseGSS)
  313. {
  314. m_pGssLayer = new CAsyncGssSocketLayer;
  315. AddLayer(m_pGssLayer);
  316. if (!m_pGssLayer->InitGSS())
  317. {
  318. ShowStatus(_T("Unable to initialize GSS api"), 1);
  319. DoClose(FZ_REPLY_CRITICALERROR);
  320. return false;
  321. }
  322. }
  323. #endif
  324. return true;
  325. }
  326. void CFtpControlSocket::Connect(t_server &server)
  327. {
  328. USES_CONVERSION;
  329. if (m_Operation.nOpMode)
  330. {
  331. ShowStatus(_T("Internal error: m_Operation.nOpMode not zero in Connect"), 1);
  332. m_Operation.nOpMode = CSMODE_CONNECT;
  333. DoClose(FZ_REPLY_CRITICALERROR);
  334. return;
  335. }
  336. m_Operation.nOpMode = CSMODE_CONNECT;
  337. m_CurrentServer = server;
  338. if (!InitConnect())
  339. return;
  340. if (!Create())
  341. {
  342. DoClose();
  343. return;
  344. }
  345. AsyncSelect();
  346. m_Operation.nOpState =
  347. #ifndef MPEXT_NO_GSS
  348. m_pGssLayer ? CONNECT_GSS_INIT :
  349. #endif
  350. CONNECT_INIT;
  351. #ifndef MPEXT_NO_SSL
  352. if (server.nServerType & FZ_SERVERTYPE_LAYER_SSL_IMPLICIT)
  353. {
  354. if (!m_pSslLayer)
  355. {
  356. ShowStatus(_T("Internal error: m_pSslLayer not initialized"), 1);
  357. DoClose(FZ_REPLY_CRITICALERROR);
  358. return;
  359. }
  360. int res = m_pSslLayer->InitSSLConnection(true);
  361. if (res == SSL_FAILURE_LOADDLLS)
  362. ShowStatus(IDS_ERRORMSG_CANTLOADSSLDLLS, 1);
  363. else if (res == SSL_FAILURE_INITSSL)
  364. ShowStatus(IDS_ERRORMSG_CANTINITSSL, 1);
  365. if (!res)
  366. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SECURESERVER, 1), 0);
  367. else
  368. {
  369. DoClose();
  370. return;
  371. }
  372. }
  373. #endif
  374. int logontype = COptions::GetOptionVal(OPTION_LOGONTYPE);
  375. int port;
  376. CString buf,temp;
  377. if (server.fwbypass)
  378. logontype=0;
  379. // are we connecting directly to the host (logon type 0) or via a firewall? (logon type>0)
  380. CString fwhost;
  381. int fwport;
  382. if(!logontype)
  383. {
  384. temp=server.host;
  385. port=server.port;
  386. }
  387. else
  388. {
  389. fwhost=COptions::GetOption(OPTION_FWHOST);
  390. fwport=COptions::GetOptionVal(OPTION_FWPORT);
  391. temp=fwhost;
  392. port=fwport;
  393. if(fwport!=21)
  394. fwhost.Format( _T("%s:%d"), fwhost, fwport); // add port to fwhost (only if port is not 21)
  395. }
  396. CString hostname = server.host;
  397. if(server.port!=21)
  398. hostname.Format( _T("%s:%d"), hostname, server.port); // add port to hostname (only if port is not 21)
  399. CString str;
  400. str.Format(IDS_STATUSMSG_CONNECTING, hostname);
  401. ShowStatus(str, 0);
  402. #ifndef MPEXT_NO_SSL
  403. if ((server.nServerType & FZ_SERVERTYPE_LAYERMASK) & (FZ_SERVERTYPE_LAYER_SSL_EXPLICIT | FZ_SERVERTYPE_LAYER_TLS_EXPLICIT))
  404. m_Operation.nOpState = CONNECT_SSL_INIT;
  405. #endif
  406. if (!CControlSocket::Connect(temp, port))
  407. {
  408. if (WSAGetLastError() != WSAEWOULDBLOCK)
  409. {
  410. DoClose();
  411. return;
  412. }
  413. }
  414. m_ServerName = logontype?fwhost:hostname;
  415. m_LastRecvTime = m_LastSendTime = CTime::GetCurrentTime();
  416. }
  417. void CFtpControlSocket::LogOnToServer(BOOL bSkipReply /*=FALSE*/)
  418. {
  419. int logontype =
  420. #ifndef MPEXT_NO_GSS
  421. m_pGssLayer ? 0 :
  422. #endif
  423. COptions::GetOptionVal(OPTION_LOGONTYPE);
  424. const int LO = -2, ER = -1;
  425. CString buf, temp;
  426. const int NUMLOGIN = 9; // currently supports 9 different login sequences
  427. int logonseq[NUMLOGIN][20] = {
  428. // this array stores all of the logon sequences for the various firewalls
  429. // in blocks of 3 nums. 1st num is command to send, 2nd num is next point in logon sequence array
  430. // if 200 series response is rec'd from server as the result of the command, 3rd num is next
  431. // point in logon sequence if 300 series rec'd
  432. {0,LO,3, 1,LO,6, 12,LO,ER}, // no firewall
  433. {3,6,3, 4,6,ER, 5,9,9, 0,LO,12, 1,LO,15, 12,LO,ER}, // SITE hostname
  434. {3,6,3, 4,6,ER, 6,LO,9, 1,LO,12, 12,LO,ER}, // USER after logon
  435. {7,3,3, 0,LO,6, 1,LO,9, 12,LO,ER}, //proxy OPEN
  436. {3,6,3, 4,6,ER, 0,LO,9, 1,LO,12, 12,LO,ER}, // Transparent
  437. {6,LO,3, 1,LO,6, 12,LO,ER}, // USER remoteID@remotehost
  438. {8,6,3, 4,6,ER, 0,LO,9, 1,LO,12, 12,LO,ER}, //USER fireID@remotehost
  439. {9,ER,3, 1,LO,6, 2,LO,ER}, //USER remoteID@remotehost fireID
  440. {10,LO,3,11,LO,6,2,LO,ER} // USER remoteID@fireID@remotehost
  441. };
  442. if (m_CurrentServer.fwbypass)
  443. logontype = 0;
  444. #ifndef MPEXT_NO_SSL
  445. if (m_Operation.nOpState == CONNECT_SSL_INIT)
  446. {
  447. if (m_CurrentServer.nServerType & FZ_SERVERTYPE_LAYER_SSL_EXPLICIT)
  448. {
  449. if (!Send("AUTH SSL"))
  450. return;
  451. }
  452. else
  453. {
  454. if (!Send("AUTH TLS"))
  455. return;
  456. }
  457. m_Operation.nOpState = CONNECT_SSL_NEGOTIATE;
  458. return;
  459. }
  460. else if (m_Operation.nOpState == CONNECT_SSL_NEGOTIATE)
  461. {
  462. int res = GetReplyCode();
  463. if (res!=2 && res!=3)
  464. {
  465. DoClose();
  466. return;
  467. }
  468. else
  469. {
  470. if (!m_pSslLayer)
  471. {
  472. ShowStatus(_T("Internal error: m_pSslLayer not initialized"), 1);
  473. DoClose(FZ_REPLY_CRITICALERROR);
  474. return;
  475. }
  476. int res = m_pSslLayer->InitSSLConnection(true);
  477. if (res == SSL_FAILURE_LOADDLLS)
  478. ShowStatus(IDS_ERRORMSG_CANTLOADSSLDLLS, 1);
  479. else if (res == SSL_FAILURE_INITSSL)
  480. ShowStatus(IDS_ERRORMSG_CANTINITSSL, 1);
  481. if (res)
  482. {
  483. DoClose();
  484. return;
  485. }
  486. }
  487. m_Operation.nOpState = CONNECT_SSL_WAITDONE;
  488. return;
  489. }
  490. else if (m_Operation.nOpState == CONNECT_SSL_WAITDONE)
  491. {
  492. m_Operation.nOpState = CONNECT_INIT;
  493. }
  494. else if (m_Operation.nOpState == CONNECT_SSL_PBSZ)
  495. {
  496. if (!Send("PROT P"))
  497. return;
  498. m_Operation.nOpState = CONNECT_SSL_PROT;
  499. return;
  500. }
  501. else if (m_Operation.nOpState == CONNECT_SSL_PROT)
  502. {
  503. int code = GetReplyCode();
  504. if (code == 2 || code == 3)
  505. m_bProtP = true;
  506. ShowStatus(IDS_STATUSMSG_CONNECTED, 0);
  507. m_pOwner->SetConnected(TRUE);
  508. ResetOperation(FZ_REPLY_OK);
  509. return;
  510. }
  511. #ifndef MPEXT_NO_GSS
  512. else
  513. #endif
  514. #endif
  515. #ifndef MPEXT_NO_GSS
  516. if (m_Operation.nOpState==CONNECT_GSS_FAILED ||
  517. m_Operation.nOpState == CONNECT_GSS_NEEDPASS ||
  518. m_Operation.nOpState == CONNECT_GSS_NEEDUSER)
  519. {
  520. if (!m_RecvBuffer.empty() && m_RecvBuffer.front() != "")
  521. {
  522. //Incoming reply from server during async is not allowed
  523. DoClose();
  524. return;
  525. }
  526. }
  527. #endif
  528. else if (m_Operation.nOpState == CONNECT_FEAT)
  529. {
  530. if (!m_bAnnouncesUTF8 && !m_CurrentServer.nUTF8)
  531. m_bUTF8 = false;
  532. if (m_bUTF8 && m_hasClntCmd && !m_isFileZilla)
  533. {
  534. // Some servers refuse to enable UTF8 if client does not send CLNT command
  535. // to fix compatibility with Internet Explorer, but in the process breaking
  536. // compatibility with other clients.
  537. // Rather than forcing MS to fix Internet Explorer, letting other clients
  538. // suffer is a questionable decision in my opinion.
  539. if (Send("CLNT FileZilla"))
  540. m_Operation.nOpState = CONNECT_CLNT;
  541. return;
  542. }
  543. if (m_bUTF8 && !m_isFileZilla)
  544. {
  545. // Handle servers that disobey RFC 2640 that have UTF8 in the FEAT
  546. // response but do not use UTF8 unless OPTS UTF8 ON gets send.
  547. // However these servers obey a conflicting ietf draft:
  548. // http://www.ietf.org/proceedings/02nov/I-D/draft-ietf-ftpext-utf-8-option-00.txt
  549. // servers are, amongst others, G6 FTP Server and RaidenFTPd.
  550. if (Send("OPTS UTF8 ON"))
  551. m_Operation.nOpState = CONNECT_OPTSUTF8;
  552. return;
  553. }
  554. #ifndef MPEXT_NO_SSL
  555. if ((m_CurrentServer.nServerType & FZ_SERVERTYPE_LAYERMASK) & (FZ_SERVERTYPE_LAYER_SSL_IMPLICIT | FZ_SERVERTYPE_LAYER_SSL_EXPLICIT | FZ_SERVERTYPE_LAYER_TLS_EXPLICIT))
  556. {
  557. m_Operation.nOpState = CONNECT_SSL_PBSZ;
  558. Send("PBSZ 0");
  559. return;
  560. }
  561. #endif
  562. ShowStatus(IDS_STATUSMSG_CONNECTED, 0);
  563. m_pOwner->SetConnected(TRUE);
  564. ResetOperation(FZ_REPLY_OK);
  565. return;
  566. }
  567. else if (m_Operation.nOpState == CONNECT_CLNT)
  568. {
  569. // See above why we send this command
  570. if (Send("OPTS UTF8 ON"))
  571. m_Operation.nOpState = CONNECT_OPTSUTF8;
  572. return;
  573. }
  574. else if (m_Operation.nOpState == CONNECT_OPTSUTF8)
  575. {
  576. #ifndef MPEXT_NO_SSL
  577. if ((m_CurrentServer.nServerType & FZ_SERVERTYPE_LAYERMASK) & (FZ_SERVERTYPE_LAYER_SSL_IMPLICIT | FZ_SERVERTYPE_LAYER_SSL_EXPLICIT | FZ_SERVERTYPE_LAYER_TLS_EXPLICIT))
  578. {
  579. m_Operation.nOpState = CONNECT_SSL_PBSZ;
  580. Send("PBSZ 0");
  581. return;
  582. }
  583. #endif
  584. ShowStatus(IDS_STATUSMSG_CONNECTED, 0);
  585. m_pOwner->SetConnected(TRUE);
  586. ResetOperation(FZ_REPLY_OK);
  587. return;
  588. }
  589. else if (m_Operation.nOpState == CONNECT_SYST)
  590. {
  591. if (GetReplyCode() == 2)
  592. {
  593. const CString reply = m_RecvBuffer.front();
  594. if (reply.GetLength() > 7 && reply.Mid(3, 4) == " MVS")
  595. m_mayBeMvsFilesystem = true;
  596. else if (reply.GetLength() >= 11 && reply.Mid(3, 8) == " BS-2000")
  597. m_mayBeBS2000Filesystem = true;
  598. if (reply.Find(_T("FileZilla")) != -1)
  599. m_isFileZilla = true;
  600. }
  601. if (Send("FEAT"))
  602. m_Operation.nOpState = CONNECT_FEAT;
  603. return;
  604. }
  605. else if (!bSkipReply)
  606. {
  607. int res = GetReplyCode();
  608. if (res != 2 && res != 3 && m_Operation.nOpState >= 0) // get initial connect msg off server
  609. {
  610. int nError = 0;
  611. if (m_bUTF8 && m_CurrentServer.nUTF8 != 1)
  612. {
  613. // Fall back to local charset for the case that the server might not
  614. // support UTF8 and the login data contains non-ascii characters.
  615. bool asciiOnly = true;
  616. for (int i = 0; i < m_CurrentServer.user.GetLength(); i++)
  617. if (m_CurrentServer.user.GetAt(i) > 127)
  618. asciiOnly = false;
  619. for (int i = 0; i < m_CurrentServer.pass.GetLength(); i++)
  620. if (m_CurrentServer.pass.GetAt(i) > 127)
  621. asciiOnly = false;
  622. for (int i = 0; i < m_CurrentServer.account.GetLength(); i++)
  623. if (m_CurrentServer.account.GetAt(i) > 127)
  624. asciiOnly = false;
  625. if (!asciiOnly)
  626. {
  627. ShowStatus(_T("Login data contains non-ascii characters and server might not be UTF-8 aware. Trying local charset."), 0);
  628. m_bUTF8 = false;
  629. m_Operation.nOpState = CONNECT_INIT;
  630. }
  631. else
  632. nError = FZ_REPLY_ERROR;
  633. }
  634. else
  635. nError = FZ_REPLY_ERROR;
  636. if (nError)
  637. {
  638. if (res==5 && logonseq[logontype][m_Operation.nOpState]==1)
  639. nError|=FZ_REPLY_CRITICALERROR;
  640. DoClose(nError);
  641. return;
  642. }
  643. }
  644. }
  645. CString hostname = m_CurrentServer.host;
  646. if (m_CurrentServer.port != 21)
  647. hostname.Format(hostname+ ":%d", m_CurrentServer.port); // add port to hostname (only if port is not 21)
  648. USES_CONVERSION;
  649. #ifndef MPEXT_NO_GSS
  650. //**** GSS Authentication ****
  651. if (m_Operation.nOpState==CONNECT_GSS_INIT) //authenticate
  652. {
  653. int i = m_pGssLayer->GetClientAuth(T2CA(m_CurrentServer.host));
  654. if (i==-1)
  655. m_Operation.nOpState = CONNECT_GSS_AUTHDONE;
  656. else if (i != GSSAPI_AUTHENTICATION_SUCCEEDED)
  657. {
  658. m_Operation.nOpState = CONNECT_GSS_FAILED;
  659. CAsyncRequestData *pData=new CAsyncRequestData;
  660. pData->nRequestType=FZ_ASYNCREQUEST_GSS_AUTHFAILED;
  661. pData->nRequestID=m_pOwner->GetNextAsyncRequestID();
  662. if (!PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_ASYNCREQUEST, FZ_ASYNCREQUEST_GSS_AUTHFAILED), (LPARAM)pData))
  663. delete pData;
  664. }
  665. else
  666. {
  667. // we got authentication, we need to check whether we have forwardable tickets
  668. //ShowStatus(IDS_STATUSMSG_GSSAUTH, 0);
  669. PostMessage(m_pOwner->m_hOwnerWnd,m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SECURESERVER, TRUE), 0);
  670. if (Send("CWD ."))
  671. m_Operation.nOpState = CONNECT_GSS_CWD;
  672. }
  673. return;
  674. }
  675. else if (m_Operation.nOpState == CONNECT_GSS_AUTHDONE)
  676. {
  677. if (!m_pGssLayer->AuthSuccessful())
  678. {
  679. m_Operation.nOpState = CONNECT_GSS_FAILED;
  680. CAsyncRequestData *pData=new CAsyncRequestData;
  681. pData->nRequestType = FZ_ASYNCREQUEST_GSS_AUTHFAILED;
  682. pData->nRequestID = m_pOwner->GetNextAsyncRequestID();
  683. if (!PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_ASYNCREQUEST, FZ_ASYNCREQUEST_GSS_AUTHFAILED), (LPARAM)pData))
  684. delete pData;
  685. return;
  686. }
  687. else
  688. {
  689. // we got authentication, we need to check whether we have forwardable tickets
  690. //ShowStatus(IDS_STATUSMSG_GSSAUTH, 0);
  691. PostMessage(m_pOwner->m_hOwnerWnd,m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SECURESERVER, TRUE), 0);
  692. if (Send("CWD ."))
  693. m_Operation.nOpState = CONNECT_GSS_CWD;
  694. return;
  695. }
  696. }
  697. else if (m_Operation.nOpState == CONNECT_GSS_CWD)
  698. { // authentication succeeded, we're now get the response to the CWD command
  699. if (GetReplyCode() == 2) // we're logged on
  700. {
  701. if (Send("SYST"))
  702. m_Operation.nOpState = CONNECT_SYST;
  703. return;
  704. }
  705. else
  706. //GSS authentication complete but we still have to go through the standard logon procedure
  707. m_Operation.nOpState = CONNECT_INIT;
  708. }
  709. #endif
  710. if (m_Operation.nOpState==CONNECT_INIT)
  711. {
  712. if (logontype)
  713. {
  714. CString str;
  715. str.Format(IDS_STATUSMSG_FWCONNECT,hostname);
  716. ShowStatus(str,0);
  717. }
  718. m_Operation.nOpState++;
  719. }
  720. else if (m_Operation.nOpState >= 0 && !bSkipReply)
  721. {
  722. m_Operation.nOpState=logonseq[logontype][m_Operation.nOpState+GetReplyCode()-1]; //get next command from array
  723. switch(m_Operation.nOpState)
  724. {
  725. case ER: // ER means summat has gone wrong
  726. DoClose();
  727. return;
  728. case LO: //LO means we are logged on
  729. if (Send("SYST"))
  730. m_Operation.nOpState = CONNECT_SYST;
  731. return;
  732. }
  733. }
  734. // go through appropriate logon procedure
  735. #ifndef MPEXT_NO_GSS
  736. int i = logonseq[logontype][m_Operation.nOpState];
  737. if (m_pGssLayer)
  738. {
  739. if ((i == 0 || i == 6 || i == 9 || i == 10) &&
  740. (m_CurrentServer.user == "anonymous" || m_CurrentServer.user == ""))
  741. {
  742. //Extract user from kerberos ticket
  743. char str[256];
  744. if (m_pGssLayer->GetUserFromKrbTicket(str))
  745. m_CurrentServer.user = str;
  746. if (m_CurrentServer.user == "")
  747. {
  748. CGssNeedUserRequestData *pData = new CGssNeedUserRequestData;
  749. pData->nRequestID = m_pOwner->GetNextAsyncRequestID();
  750. pData->nOldOpState = m_Operation.nOpState;
  751. m_Operation.nOpState = CONNECT_GSS_NEEDUSER;
  752. if (!PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_ASYNCREQUEST, FZ_ASYNCREQUEST_GSS_NEEDUSER), (LPARAM)pData))
  753. delete pData;
  754. return;
  755. }
  756. }
  757. else if ((i == 1 || i == 11) && (m_CurrentServer.pass == COptions::GetOption(OPTION_ANONPWD) || m_CurrentServer.pass == ""))
  758. {
  759. CGssNeedPassRequestData *pData=new CGssNeedPassRequestData;
  760. pData->nRequestID=m_pOwner->GetNextAsyncRequestID();
  761. pData->nOldOpState = m_Operation.nOpState;
  762. m_Operation.nOpState = CONNECT_GSS_NEEDPASS;
  763. if (!PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_ASYNCREQUEST, FZ_ASYNCREQUEST_GSS_NEEDPASS), (LPARAM)pData))
  764. delete pData;
  765. return;
  766. }
  767. }
  768. #endif
  769. switch(logonseq[logontype][m_Operation.nOpState])
  770. {
  771. case 0:
  772. temp="USER "+m_CurrentServer.user;
  773. break;
  774. case 1:
  775. temp="PASS "+m_CurrentServer.pass;
  776. break;
  777. case 2:
  778. temp="ACCT "+CCrypt::decrypt(COptions::GetOption(OPTION_FWPASS));
  779. break;
  780. case 3:
  781. temp="USER "+COptions::GetOption(OPTION_FWUSER);
  782. break;
  783. case 4:
  784. temp="PASS "+CCrypt::decrypt(COptions::GetOption(OPTION_FWPASS));
  785. break;
  786. case 5:
  787. temp="SITE "+hostname;
  788. break;
  789. case 6:
  790. temp="USER "+m_CurrentServer.user+"@"+hostname;
  791. break;
  792. case 7:
  793. temp="OPEN "+hostname;
  794. break;
  795. case 8:
  796. temp="USER "+COptions::GetOption(OPTION_FWUSER)+"@"+hostname;
  797. break;
  798. case 9:
  799. temp="USER "+m_CurrentServer.user+"@"+hostname+" "+COptions::GetOption(OPTION_FWUSER);
  800. break;
  801. case 10:
  802. temp="USER "+m_CurrentServer.user+"@"+COptions::GetOption(OPTION_FWUSER)+"@"+hostname;
  803. break;
  804. case 11:
  805. temp="PASS "+m_CurrentServer.pass+"@"+CCrypt::decrypt(COptions::GetOption(OPTION_FWPASS));
  806. break;
  807. case 12:
  808. if (m_CurrentServer.account == _T(""))
  809. temp = _T("ACCT default");
  810. else
  811. temp = _T("ACCT ") + m_CurrentServer.account;
  812. break;
  813. }
  814. // send command, get response
  815. if(!Send(temp))
  816. return;
  817. }
  818. #define BUFFERSIZE 4096
  819. void CFtpControlSocket::OnReceive(int nErrorCode)
  820. {
  821. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("OnReceive(%d) OpMode=%d OpState=%d"), nErrorCode, m_Operation.nOpMode, m_Operation.nOpState);
  822. m_LastRecvTime = CTime::GetCurrentTime();
  823. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_RECV), 0);
  824. if (!m_pOwner->IsConnected())
  825. {
  826. if (!m_Operation.nOpMode)
  827. {
  828. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("Socket has been closed, don't process receive") );
  829. return;
  830. }
  831. m_MultiLine = "";
  832. CString str;
  833. str.Format(IDS_STATUSMSG_CONNECTEDWITH, m_ServerName);
  834. ShowStatus(str, 0);
  835. m_pOwner->SetConnected(TRUE);
  836. }
  837. char *buffer = new char[BUFFERSIZE];
  838. int numread = Receive(buffer, BUFFERSIZE);
  839. if (numread == SOCKET_ERROR)
  840. {
  841. delete [] buffer;
  842. if (GetLastError() != WSAEWOULDBLOCK)
  843. {
  844. ShowStatus(IDS_STATUSMSG_DISCONNECTED, 1);
  845. DoClose();
  846. }
  847. return;
  848. }
  849. if (!numread)
  850. {
  851. delete [] buffer;
  852. ShowStatus(IDS_STATUSMSG_DISCONNECTED, 1);
  853. DoClose();
  854. }
  855. for (int i=0; i < numread; i++)
  856. {
  857. if ((buffer[i] == '\r') || (buffer[i] == '\n') || (buffer[i] == 0))
  858. {
  859. if (!m_RecvBuffer.empty() && m_RecvBuffer.back() != "")
  860. {
  861. USES_CONVERSION;
  862. if (m_bUTF8)
  863. {
  864. // convert from UTF-8 to ANSI
  865. LPCSTR utf8 = (LPCSTR)m_RecvBuffer.back();
  866. if (!utf8_valid((const unsigned char*)utf8, strlen(utf8)))
  867. {
  868. if (m_CurrentServer.nUTF8 != 1)
  869. {
  870. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Server does not send proper UTF-8, falling back to local charset"));
  871. m_bUTF8 = false;
  872. }
  873. ShowStatus(A2CT(utf8), 3);
  874. }
  875. else
  876. {
  877. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
  878. if (!len)
  879. m_RecvBuffer.back() = "";
  880. else
  881. {
  882. LPWSTR p1 = new WCHAR[len + 1];
  883. MultiByteToWideChar(CP_UTF8, 0, utf8, -1 , (LPWSTR)p1, len + 1);
  884. ShowStatus(W2CT(p1), 3);
  885. delete [] p1;
  886. }
  887. }
  888. }
  889. else
  890. ShowStatus(A2CT(m_RecvBuffer.back()), 3);
  891. //Check for multi-line responses
  892. if (m_RecvBuffer.back().GetLength() > 3)
  893. {
  894. if (m_MultiLine != "")
  895. {
  896. if (m_RecvBuffer.back().Left(4) != m_MultiLine)
  897. {
  898. DiscardLine(m_RecvBuffer.back());
  899. m_RecvBuffer.pop_back();
  900. }
  901. else // end of multi-line found
  902. {
  903. m_MultiLine = "";
  904. m_pOwner->PostThreadMessage(m_pOwner->m_nInternalMessageID, FZAPI_THREADMSG_PROCESSREPLY, 0);
  905. }
  906. }
  907. // start of new multi-line
  908. else if (m_RecvBuffer.back()[3] == '-')
  909. {
  910. // DDD<SP> is the end of a multi-line response
  911. m_MultiLine = m_RecvBuffer.back().Left(3) + ' ';
  912. m_RecvBuffer.pop_back();
  913. }
  914. else
  915. m_pOwner->PostThreadMessage(m_pOwner->m_nInternalMessageID, FZAPI_THREADMSG_PROCESSREPLY, 0);
  916. }
  917. else
  918. m_RecvBuffer.pop_back();
  919. m_RecvBuffer.push_back("");
  920. }
  921. }
  922. else
  923. {
  924. //The command may only be 2000 chars long. This ensures that a malicious user can't
  925. //send extremely large commands to fill the memory of the server
  926. if (m_RecvBuffer.empty())
  927. m_RecvBuffer.push_back("");
  928. if (m_RecvBuffer.back().GetLength() < 2000)
  929. m_RecvBuffer.back() += buffer[i];
  930. }
  931. }
  932. delete [] buffer;
  933. }
  934. void CFtpControlSocket::ProcessReply()
  935. {
  936. if (m_RecvBuffer.empty())
  937. return;
  938. if (m_awaitsReply)
  939. {
  940. if (m_sendBuffer)
  941. TriggerEvent(FD_WRITE);
  942. m_awaitsReply = false;
  943. }
  944. CString reply = GetReply();
  945. if ( reply == _T("") )
  946. return;
  947. // After Cancel, we might have to skip a reply
  948. if (m_skipReply)
  949. {
  950. m_skipReply = false;
  951. m_RecvBuffer.pop_front();
  952. return;
  953. }
  954. if (m_bKeepAliveActive)
  955. {
  956. m_bKeepAliveActive = FALSE;
  957. m_pOwner->PostThreadMessage(m_pOwner->m_nInternalMessageID,FZAPI_THREADMSG_POSTKEEPALIVE,0);
  958. }
  959. else if (m_Operation.nOpMode&CSMODE_CONNECT)
  960. LogOnToServer();
  961. else if (m_Operation.nOpMode& (CSMODE_COMMAND|CSMODE_CHMOD) )
  962. {
  963. if (GetReplyCode()== 2 || GetReplyCode()== 3)
  964. ResetOperation(FZ_REPLY_OK);
  965. else
  966. ResetOperation(FZ_REPLY_ERROR);
  967. }
  968. else if (m_Operation.nOpMode&CSMODE_TRANSFER)
  969. {
  970. FileTransfer(0);
  971. }
  972. else if (m_Operation.nOpMode&CSMODE_LIST)
  973. List(FALSE);
  974. else if (m_Operation.nOpMode&CSMODE_DELETE)
  975. Delete( _T(""),CServerPath());
  976. else if (m_Operation.nOpMode&CSMODE_RMDIR)
  977. RemoveDir( _T(""),CServerPath());
  978. else if (m_Operation.nOpMode&CSMODE_MKDIR)
  979. MakeDir(CServerPath());
  980. else if (m_Operation.nOpMode&CSMODE_RENAME)
  981. Rename(_T(""), _T(""), CServerPath(), CServerPath());
  982. if (!m_RecvBuffer.empty())
  983. m_RecvBuffer.pop_front();
  984. }
  985. void CFtpControlSocket::OnConnect(int nErrorCode)
  986. {
  987. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("OnConnect(%d) OpMode=%d OpState=%d"), nErrorCode, m_Operation.nOpMode, m_Operation.nOpState);
  988. if (!m_Operation.nOpMode)
  989. {
  990. if (!m_pOwner->IsConnected())
  991. DoClose();
  992. return;
  993. }
  994. if (!nErrorCode)
  995. {
  996. if (!m_pOwner->IsConnected())
  997. {
  998. m_MultiLine = "";
  999. m_pOwner->SetConnected(TRUE);
  1000. CString str;
  1001. str.Format(
  1002. #ifndef MPEXT_NO_SSL
  1003. m_pSslLayer ? IDS_STATUSMSG_CONNECTEDWITHSSL :
  1004. #endif
  1005. IDS_STATUSMSG_CONNECTEDWITH, m_ServerName);
  1006. ShowStatus(str,0);
  1007. }
  1008. }
  1009. else
  1010. {
  1011. if (nErrorCode == WSAHOST_NOT_FOUND)
  1012. ShowStatus(IDS_ERRORMSG_CANTRESOLVEHOST, 1);
  1013. DoClose();
  1014. }
  1015. }
  1016. BOOL CFtpControlSocket::Send(CString str)
  1017. {
  1018. USES_CONVERSION;
  1019. ShowStatus(str, 2);
  1020. str += "\r\n";
  1021. int res = 0;
  1022. if (m_bUTF8)
  1023. {
  1024. LPCWSTR unicode = T2CW(str);
  1025. int len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, 0, 0, 0, 0);
  1026. if (!len)
  1027. {
  1028. ShowStatus(IDS_ERRORMSG_CANTSENDCOMMAND, 1);
  1029. DoClose();
  1030. return FALSE;
  1031. }
  1032. char* utf8 = new char[len + 1];
  1033. WideCharToMultiByte(CP_UTF8, 0, unicode, -1, utf8, len + 1, 0, 0);
  1034. int sendLen = strlen(utf8);
  1035. if (!m_awaitsReply && !m_sendBuffer)
  1036. res = CAsyncSocketEx::Send(utf8, strlen(utf8));
  1037. else
  1038. res = -2;
  1039. if ((res == SOCKET_ERROR && GetLastError() != WSAEWOULDBLOCK) || !res)
  1040. {
  1041. delete [] utf8;
  1042. ShowStatus(IDS_ERRORMSG_CANTSENDCOMMAND, 1);
  1043. DoClose();
  1044. return FALSE;
  1045. }
  1046. if (res != sendLen)
  1047. {
  1048. if (res == -2)
  1049. res = 0;
  1050. if (!m_sendBuffer)
  1051. {
  1052. m_sendBuffer = new char[sendLen - res];
  1053. memcpy(m_sendBuffer, utf8 + res, sendLen - res);
  1054. m_sendBufferLen = sendLen - res;
  1055. }
  1056. else
  1057. {
  1058. char* tmp = new char[m_sendBufferLen + sendLen - res];
  1059. memcpy(tmp, m_sendBuffer, m_sendBufferLen);
  1060. memcpy(tmp + m_sendBufferLen, utf8 + res, sendLen - res);
  1061. delete [] m_sendBuffer;
  1062. m_sendBuffer = tmp;
  1063. m_sendBufferLen += sendLen - res;
  1064. }
  1065. }
  1066. delete [] utf8;
  1067. }
  1068. else
  1069. {
  1070. LPCSTR lpszAsciiSend = T2CA(str);
  1071. int sendLen = strlen(lpszAsciiSend);
  1072. if (!m_awaitsReply && !m_sendBuffer)
  1073. res = CAsyncSocketEx::Send(lpszAsciiSend, strlen(lpszAsciiSend));
  1074. else
  1075. res = -2;
  1076. if ((res == SOCKET_ERROR && GetLastError() != WSAEWOULDBLOCK) || !res)
  1077. {
  1078. ShowStatus(IDS_ERRORMSG_CANTSENDCOMMAND, 1);
  1079. DoClose();
  1080. return FALSE;
  1081. }
  1082. if (res != sendLen)
  1083. {
  1084. if (res == -2)
  1085. res = 0;
  1086. if (!m_sendBuffer)
  1087. {
  1088. m_sendBuffer = new char[sendLen - res];
  1089. memcpy(m_sendBuffer, lpszAsciiSend, sendLen - res);
  1090. m_sendBufferLen = sendLen - res;
  1091. }
  1092. else
  1093. {
  1094. char* tmp = new char[m_sendBufferLen + sendLen - res];
  1095. memcpy(tmp, m_sendBuffer, m_sendBufferLen);
  1096. memcpy(tmp + m_sendBufferLen, lpszAsciiSend + res, sendLen - res);
  1097. delete [] m_sendBuffer;
  1098. m_sendBuffer = tmp;
  1099. m_sendBufferLen += sendLen - res;
  1100. }
  1101. }
  1102. }
  1103. if (res > 0)
  1104. {
  1105. m_awaitsReply = true;
  1106. m_LastSendTime = CTime::GetCurrentTime();
  1107. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_SEND), 0);
  1108. }
  1109. return TRUE;
  1110. }
  1111. int CFtpControlSocket::GetReplyCode()
  1112. {
  1113. if (m_RecvBuffer.empty())
  1114. return 0;
  1115. CStringA str = m_RecvBuffer.front();
  1116. if (str == "")
  1117. return 0;
  1118. else
  1119. return str[0]-'0';
  1120. }
  1121. void CFtpControlSocket::DoClose(int nError /*=0*/)
  1122. {
  1123. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("DoClose(%d) OpMode=%d OpState=%d"), nError, m_Operation.nOpMode, m_Operation.nOpState);
  1124. m_bCheckForTimeout=TRUE;
  1125. m_pOwner->SetConnected(FALSE);
  1126. m_bKeepAliveActive=FALSE;
  1127. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SECURESERVER, FALSE), 0);
  1128. if (nError & FZ_REPLY_CRITICALERROR)
  1129. nError |= FZ_REPLY_ERROR;
  1130. ResetOperation(FZ_REPLY_ERROR|FZ_REPLY_DISCONNECTED|nError);
  1131. m_RecvBuffer.clear();
  1132. m_MultiLine = "";
  1133. #ifndef MPEXT_NO_SSL
  1134. m_bDidRejectCertificate = FALSE;
  1135. #endif
  1136. #ifndef MPEXT_NO_ZLIB
  1137. m_useZlib = false;
  1138. m_zlibSupported = false;
  1139. m_zlibLevel = 0;
  1140. #endif
  1141. m_bUTF8 = false;
  1142. m_hasClntCmd = false;
  1143. m_awaitsReply = false;
  1144. m_skipReply = false;
  1145. delete [] m_sendBuffer;
  1146. m_sendBuffer = 0;
  1147. m_sendBufferLen = 0;
  1148. m_bProtP = false;
  1149. m_mayBeMvsFilesystem = false;
  1150. m_mayBeBS2000Filesystem = false;
  1151. CControlSocket::Close();
  1152. }
  1153. void CFtpControlSocket::Disconnect()
  1154. {
  1155. ASSERT(!m_Operation.nOpMode);
  1156. m_Operation.nOpMode=CSMODE_DISCONNECT;
  1157. DoClose();
  1158. ShowStatus(IDS_STATUSMSG_DISCONNECTED,0); //Send the disconnected message to the message log
  1159. }
  1160. void CFtpControlSocket::CheckForTimeout()
  1161. {
  1162. if (!m_Operation.nOpMode && !m_bKeepAliveActive)
  1163. return;
  1164. if (!m_bCheckForTimeout)
  1165. return;
  1166. int delay=COptions::GetOptionVal(OPTION_TIMEOUTLENGTH);
  1167. if (m_pTransferSocket)
  1168. {
  1169. int res=m_pTransferSocket->CheckForTimeout(delay);
  1170. if (res)
  1171. return;
  1172. }
  1173. CTimeSpan span=CTime::GetCurrentTime()-m_LastRecvTime;
  1174. if (span.GetTotalSeconds()>=delay)
  1175. {
  1176. ShowStatus(IDS_ERRORMSG_TIMEOUT, 1);
  1177. DoClose();
  1178. }
  1179. }
  1180. void CFtpControlSocket::FtpCommand(LPCTSTR pCommand)
  1181. {
  1182. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("FtpCommand(%s) OpMode=%d OpState=%d"), pCommand, m_Operation.nOpMode, m_Operation.nOpState);
  1183. m_Operation.nOpMode=CSMODE_COMMAND;
  1184. Send(pCommand);
  1185. }
  1186. void CFtpControlSocket::List(BOOL bFinish, int nError /*=FALSE*/, CServerPath path /*=CServerPath()*/, CString subdir /*=""*/,int nListMode/*=0*/)
  1187. {
  1188. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("List(%s,%d,\"%s\",\"%s\",%d) OpMode=%d OpState=%d"), bFinish?_T("TRUE"):_T("FALSE"), nError, path.GetPath(), subdir, nListMode,
  1189. m_Operation.nOpMode, m_Operation.nOpState);
  1190. USES_CONVERSION;
  1191. #define LIST_INIT -1
  1192. #define LIST_PWD 0
  1193. #define LIST_CWD 1
  1194. #define LIST_PWD2 2
  1195. #define LIST_CWD2 3
  1196. #define LIST_PWD3 4
  1197. #define LIST_MODE 5
  1198. #define LIST_OPTS 6
  1199. #define LIST_PORT_PASV 7
  1200. #define LIST_TYPE 8
  1201. #define LIST_LIST 9
  1202. #define LIST_WAITFINISH 10
  1203. ASSERT(!m_Operation.nOpMode || m_Operation.nOpMode&CSMODE_LIST);
  1204. m_Operation.nOpMode|=CSMODE_LIST;
  1205. if (!m_pOwner->IsConnected())
  1206. {
  1207. ResetOperation(FZ_REPLY_ERROR|FZ_REPLY_NOTCONNECTED);
  1208. return;
  1209. }
  1210. if (bFinish || nError)
  1211. if (m_Operation.nOpMode!=CSMODE_LIST)
  1212. return; //Old message coming in
  1213. if (nError)
  1214. {
  1215. delete m_pTransferSocket;
  1216. m_pTransferSocket=0;
  1217. if (nError&CSMODE_TRANSFERTIMEOUT)
  1218. DoClose();
  1219. else
  1220. ResetOperation(FZ_REPLY_ERROR);
  1221. return;
  1222. }
  1223. CListData *pData = static_cast<CListData *>(m_Operation.pData);
  1224. if (bFinish)
  1225. {
  1226. if (!m_pTransferSocket || m_pTransferSocket->m_bListening)
  1227. {
  1228. delete m_pDirectoryListing;
  1229. m_pDirectoryListing = 0;
  1230. delete m_pTransferSocket;
  1231. m_pTransferSocket = 0;
  1232. ResetOperation(FZ_REPLY_ERROR);
  1233. return;
  1234. }
  1235. int num = 0;
  1236. pData->pDirectoryListing = new t_directory;
  1237. if (COptions::GetOptionVal(OPTION_DEBUGSHOWLISTING))
  1238. m_pTransferSocket->m_pListResult->SendToMessageLog(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID);
  1239. pData->pDirectoryListing->direntry = m_pTransferSocket->m_pListResult->getList(num, pData->ListStartTime);
  1240. pData->pDirectoryListing->num = num;
  1241. if (m_pTransferSocket->m_pListResult->m_server.nServerType & FZ_SERVERTYPE_SUB_FTP_VMS && m_CurrentServer.nServerType & FZ_SERVERTYPE_FTP)
  1242. m_CurrentServer.nServerType |= FZ_SERVERTYPE_SUB_FTP_VMS;
  1243. pData->pDirectoryListing->server = m_CurrentServer;
  1244. pData->pDirectoryListing->path.SetServer(pData->pDirectoryListing->server);
  1245. if (pData->rawpwd != "")
  1246. {
  1247. if (!pData->pDirectoryListing->path.SetPath(pData->rawpwd))
  1248. {
  1249. delete m_pDirectoryListing;
  1250. m_pDirectoryListing=0;
  1251. delete m_pTransferSocket;
  1252. m_pTransferSocket=0;
  1253. ResetOperation(FZ_REPLY_ERROR);
  1254. return;
  1255. }
  1256. m_pOwner->SetCurrentPath(pData->pDirectoryListing->path);
  1257. }
  1258. else
  1259. pData->pDirectoryListing->path = m_pOwner->GetCurrentPath();
  1260. if (m_Operation.nOpState!=LIST_WAITFINISH)
  1261. return;
  1262. else
  1263. {
  1264. delete m_pTransferSocket;
  1265. m_pTransferSocket=0;
  1266. }
  1267. }
  1268. if (m_Operation.nOpState==LIST_WAITFINISH)
  1269. {
  1270. if (!bFinish)
  1271. {
  1272. if (pData->nFinish==-1)
  1273. {
  1274. int code=GetReplyCode();
  1275. if (code== 2)
  1276. {
  1277. pData->nFinish=1;
  1278. }
  1279. else
  1280. pData->nFinish=0;
  1281. }
  1282. }
  1283. else
  1284. {
  1285. if (m_pTransferSocket)
  1286. delete m_pTransferSocket;
  1287. m_pTransferSocket=0;
  1288. }
  1289. if (pData->nFinish==0)
  1290. {
  1291. ResetOperation(FZ_REPLY_ERROR);
  1292. return;
  1293. }
  1294. else if (pData->pDirectoryListing && pData->nFinish==1)
  1295. {
  1296. ShowStatus(IDS_STATUSMSG_DIRLISTSUCCESSFUL,0);
  1297. #ifndef MPEXT_NO_CACHE
  1298. CDirectoryCache cache;
  1299. cache.Lock();
  1300. t_directory dir;
  1301. if (!pData->path.IsEmpty() && pData->subdir!="")
  1302. {
  1303. if (cache.Lookup(pData->pDirectoryListing->path, pData->pDirectoryListing->server, dir))
  1304. pData->pDirectoryListing->Merge(dir, pData->ListStartTime);
  1305. cache.Store(*pData->pDirectoryListing,pData->path,pData->subdir);
  1306. }
  1307. else
  1308. {
  1309. if (cache.Lookup(pData->pDirectoryListing->path, pData->pDirectoryListing->server, dir))
  1310. pData->pDirectoryListing->Merge(dir, pData->ListStartTime);
  1311. cache.Store(*pData->pDirectoryListing);
  1312. }
  1313. cache.Unlock();
  1314. #endif
  1315. SetDirectoryListing(pData->pDirectoryListing);
  1316. ResetOperation(FZ_REPLY_OK);
  1317. return;
  1318. }
  1319. return;
  1320. }
  1321. else if (m_Operation.nOpState != LIST_INIT)
  1322. {
  1323. CString retmsg = GetReply();
  1324. BOOL error = FALSE;
  1325. int code = GetReplyCode();
  1326. switch (m_Operation.nOpState)
  1327. {
  1328. case LIST_PWD: //Reply to PWD command
  1329. if (code != 2 && code !=3 )
  1330. {
  1331. error = TRUE;
  1332. break;
  1333. }
  1334. pData->rawpwd = retmsg;
  1335. if ((m_mayBeMvsFilesystem || m_mayBeBS2000Filesystem) && m_CurrentServer.nServerType & FZ_SERVERTYPE_FTP &&
  1336. pData->rawpwd[0] != '/')
  1337. {
  1338. m_mayBeMvsFilesystem = false;
  1339. m_mayBeBS2000Filesystem = false;
  1340. if (m_mayBeBS2000Filesystem)
  1341. m_CurrentServer.nServerType |= FZ_SERVERTYPE_SUB_FTP_BS2000;
  1342. else
  1343. m_CurrentServer.nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  1344. if (!pData->path.IsEmpty())
  1345. pData->path.SetServer(m_CurrentServer);
  1346. }
  1347. if (!ParsePwdReply(pData->rawpwd))
  1348. return;
  1349. if (pData->path.IsEmpty() || pData->path == m_pOwner->GetCurrentPath())
  1350. {
  1351. #ifndef MPEXT_NO_CACHE
  1352. if (pData->nListMode & FZ_LIST_USECACHE)
  1353. {
  1354. t_directory dir;
  1355. CDirectoryCache cache;
  1356. BOOL res = cache.Lookup(m_pOwner->GetCurrentPath(), m_CurrentServer, dir);
  1357. if (res)
  1358. {
  1359. BOOL bExact = TRUE;
  1360. if (pData->nListMode & FZ_LIST_EXACT)
  1361. for (int i=0; i<dir.num; i++)
  1362. if (dir.direntry[i].bUnsure || (dir.direntry[i].size==-1 && !dir.direntry[i].dir))
  1363. {
  1364. bExact = FALSE;
  1365. break;
  1366. }
  1367. if (bExact)
  1368. {
  1369. ShowStatus(IDS_STATUSMSG_DIRLISTSUCCESSFUL, 0);
  1370. SetDirectoryListing(&dir);
  1371. ResetOperation(FZ_REPLY_OK);
  1372. return;
  1373. }
  1374. }
  1375. }
  1376. #endif
  1377. m_Operation.nOpState = NeedModeCommand() ? LIST_MODE : (NeedOptsCommand() ? LIST_OPTS : LIST_TYPE);
  1378. }
  1379. else
  1380. m_Operation.nOpState = LIST_CWD;
  1381. break;
  1382. case LIST_CWD:
  1383. if (code != 2 && code != 3)
  1384. error = TRUE;
  1385. m_Operation.nOpState = LIST_PWD2;
  1386. break;
  1387. case LIST_PWD2: //Reply to PWD command
  1388. if (code !=2 && code != 3)
  1389. error = TRUE;
  1390. else
  1391. {
  1392. pData->rawpwd = retmsg;
  1393. if (!ParsePwdReply(pData->rawpwd))
  1394. return;
  1395. }
  1396. if (pData->subdir != "")
  1397. {
  1398. if (pData->path != m_pOwner->GetCurrentPath())
  1399. {
  1400. ResetOperation(FZ_REPLY_ERROR);
  1401. return;
  1402. }
  1403. m_Operation.nOpState = LIST_CWD2;
  1404. }
  1405. else
  1406. {
  1407. #ifndef MPEXT_NO_CACHE
  1408. if (pData->nListMode & FZ_LIST_USECACHE)
  1409. {
  1410. t_directory dir;
  1411. CDirectoryCache cache;
  1412. BOOL res = cache.Lookup(m_pOwner->GetCurrentPath(), m_CurrentServer, dir);
  1413. if (res)
  1414. {
  1415. BOOL bExact = TRUE;
  1416. if (pData->nListMode & FZ_LIST_EXACT)
  1417. for (int i=0; i<dir.num; i++)
  1418. if (dir.direntry[i].bUnsure || (dir.direntry[i].size==-1 && !dir.direntry[i].dir))
  1419. {
  1420. bExact = FALSE;
  1421. break;
  1422. }
  1423. if (bExact)
  1424. {
  1425. ShowStatus(IDS_STATUSMSG_DIRLISTSUCCESSFUL, 0);
  1426. SetDirectoryListing(&dir);
  1427. ResetOperation(FZ_REPLY_OK);
  1428. return;
  1429. }
  1430. }
  1431. }
  1432. #endif
  1433. m_Operation.nOpState = NeedModeCommand() ? LIST_MODE : (NeedOptsCommand() ? LIST_OPTS : LIST_TYPE);
  1434. }
  1435. break;
  1436. case LIST_CWD2:
  1437. if (pData->lastCmdSentCDUP)
  1438. {
  1439. CString reply = GetReply().Left(3);
  1440. int replycode = _ttoi(reply);
  1441. if (replycode >= 500 && replycode < 505)
  1442. break;
  1443. pData->lastCmdSentCDUP = false;
  1444. }
  1445. if (code != 2 && code != 3)
  1446. error = TRUE;
  1447. m_Operation.nOpState = LIST_PWD3;
  1448. break;
  1449. case LIST_PWD3: //Reply to PWD command
  1450. if (code != 2 && code != 3)
  1451. error = TRUE;
  1452. else
  1453. {
  1454. pData->rawpwd = retmsg;
  1455. if (!ParsePwdReply(pData->rawpwd))
  1456. return;
  1457. #ifndef MPEXT_NO_CACHE
  1458. if (pData->nListMode & FZ_LIST_USECACHE)
  1459. {
  1460. t_directory dir;
  1461. CDirectoryCache cache;
  1462. BOOL res = cache.Lookup(m_pOwner->GetCurrentPath(), m_CurrentServer, dir);
  1463. if (res)
  1464. {
  1465. BOOL bExact = TRUE;
  1466. if (pData->nListMode & FZ_LIST_EXACT)
  1467. for (int i=0; i<dir.num; i++)
  1468. if (dir.direntry[i].bUnsure || (dir.direntry[i].size==-1 && !dir.direntry[i].dir))
  1469. {
  1470. bExact = FALSE;
  1471. break;
  1472. }
  1473. if (bExact)
  1474. {
  1475. ShowStatus(IDS_STATUSMSG_DIRLISTSUCCESSFUL, 0);
  1476. SetDirectoryListing(&dir);
  1477. ResetOperation(FZ_REPLY_OK);
  1478. return;
  1479. }
  1480. }
  1481. }
  1482. #endif
  1483. }
  1484. m_Operation.nOpState = NeedModeCommand() ? LIST_MODE : (NeedOptsCommand() ? LIST_OPTS : LIST_TYPE);
  1485. break;
  1486. case LIST_MODE:
  1487. #ifndef MPEXT_NO_ZLIB
  1488. if (code == 2 || code == 3)
  1489. m_useZlib = !m_useZlib;
  1490. #endif
  1491. m_Operation.nOpState = NeedOptsCommand() ? LIST_OPTS : LIST_TYPE;
  1492. break;
  1493. case LIST_OPTS:
  1494. #ifndef MPEXT_NO_ZLIB
  1495. if (code == 2 || code == 3)
  1496. m_zlibLevel = pData->newZlibLevel;
  1497. #endif
  1498. m_Operation.nOpState = LIST_TYPE;
  1499. break;
  1500. case LIST_TYPE:
  1501. if (code!=2 && code!=3)
  1502. error=TRUE;
  1503. m_Operation.nOpState = LIST_PORT_PASV;
  1504. break;
  1505. case LIST_PORT_PASV:
  1506. if (code!=2 && code!=3)
  1507. {
  1508. error=TRUE;
  1509. break;
  1510. }
  1511. if (pData->bPasv)
  1512. {
  1513. CString temp;
  1514. int i,j;
  1515. if((i=retmsg.Find(_T("(")))==-1||(j=retmsg.Find(_T(")")))==-1)
  1516. {
  1517. if (!pData->bTriedPortPasvOnce)
  1518. {
  1519. pData->bTriedPortPasvOnce = TRUE;
  1520. pData->bPasv = !pData->bPasv;
  1521. }
  1522. else
  1523. error=TRUE;
  1524. break;
  1525. }
  1526. if (GetFamily() == AF_INET)
  1527. {
  1528. temp = retmsg.Mid(i+1,(j-i)-1);
  1529. i=temp.ReverseFind(',');
  1530. pData->port=atol( T2CA( temp.Right(temp.GetLength()-(i+1)) ) ); //get ls byte of server socket
  1531. temp=temp.Left(i);
  1532. i=temp.ReverseFind(',');
  1533. pData->port+=256*atol( T2CA( temp.Right(temp.GetLength()-(i+1)) ) ); // add ms byte to server socket
  1534. pData->host = temp.Left(i);
  1535. pData->host.Replace(',', '.');
  1536. }
  1537. else if (GetFamily() == AF_INET6)
  1538. {
  1539. temp = temp.Mid(3);
  1540. pData->port = atol( T2CA(temp.Left(temp.GetLength() - 1) ) );
  1541. if (pData->port < 0 || pData->port > 65535)
  1542. {
  1543. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Port %u not valid"), pData->port);
  1544. error = TRUE;
  1545. break;
  1546. }
  1547. unsigned int tmpPort;
  1548. if (!GetPeerName(pData->host, tmpPort))
  1549. {
  1550. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("GetPeerName failed"));
  1551. error = TRUE;
  1552. break;
  1553. }
  1554. }
  1555. else
  1556. {
  1557. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Protocol %d not supported"), GetFamily());
  1558. error = TRUE;
  1559. break;
  1560. }
  1561. }
  1562. m_Operation.nOpState = LIST_LIST;
  1563. break;
  1564. case LIST_LIST:
  1565. if (IsMisleadingListResponse())
  1566. {
  1567. ShowStatus(IDS_STATUSMSG_DIRLISTSUCCESSFUL, 0);
  1568. t_directory listing;
  1569. listing.server = m_CurrentServer;
  1570. listing.path = m_pOwner->GetCurrentPath();
  1571. #ifndef MPEXT_NO_CACHE
  1572. CDirectoryCache cache;
  1573. cache.Lock();
  1574. t_directory dir;
  1575. if (!pData->path.IsEmpty() && pData->subdir != "")
  1576. {
  1577. if (cache.Lookup(listing.path, listing.server, dir))
  1578. listing.Merge(dir, pData->ListStartTime);
  1579. cache.Store(listing, pData->path, pData->subdir);
  1580. }
  1581. else
  1582. {
  1583. if (cache.Lookup(listing.path, listing.server, dir))
  1584. listing.Merge(dir, pData->ListStartTime);
  1585. cache.Store(listing);
  1586. }
  1587. cache.Unlock();
  1588. #endif
  1589. SetDirectoryListing(&listing);
  1590. ResetOperation(FZ_REPLY_OK);
  1591. return;
  1592. }
  1593. else if (code != 1)
  1594. error = TRUE;
  1595. else
  1596. m_Operation.nOpState = LIST_WAITFINISH;
  1597. break;
  1598. default:
  1599. error = TRUE;
  1600. }
  1601. if (error)
  1602. {
  1603. ResetOperation(FZ_REPLY_ERROR);
  1604. return;
  1605. }
  1606. }
  1607. if (m_Operation.nOpState==LIST_INIT)
  1608. { //Initialize some variables
  1609. pData=new CListData;
  1610. pData->nListMode=nListMode;
  1611. pData->path=path;
  1612. pData->subdir=subdir;
  1613. m_Operation.pData=pData;
  1614. ShowStatus(IDS_STATUSMSG_RETRIEVINGDIRLIST, 0);
  1615. pData->nFinish=-1;
  1616. if (m_pDirectoryListing)
  1617. {
  1618. delete m_pDirectoryListing;
  1619. m_pDirectoryListing=0;
  1620. }
  1621. if (COptions::GetOptionVal(OPTION_PROXYTYPE)!=PROXYTYPE_NOPROXY && !m_CurrentServer.fwbypass)
  1622. pData->bPasv = TRUE;
  1623. else if (m_CurrentServer.nPasv == 1)
  1624. pData->bPasv = TRUE;
  1625. else if (m_CurrentServer.nPasv == 2)
  1626. pData->bPasv = FALSE;
  1627. else
  1628. pData->bPasv = COptions::GetOptionVal(OPTION_PASV);
  1629. CServerPath path = pData->path;
  1630. CServerPath realpath = m_pOwner->GetCurrentPath();
  1631. if (!realpath.IsEmpty())
  1632. {
  1633. if (!pData->path.IsEmpty() && pData->path != realpath)
  1634. m_Operation.nOpState=LIST_CWD;
  1635. else if (!pData->path.IsEmpty() && pData->subdir!="")
  1636. m_Operation.nOpState=LIST_CWD2;
  1637. else
  1638. {
  1639. if (pData->nListMode & FZ_LIST_REALCHANGE)
  1640. {
  1641. if (pData->subdir == "")
  1642. m_Operation.nOpState = LIST_CWD;
  1643. else
  1644. m_Operation.nOpState = LIST_CWD2;
  1645. }
  1646. else
  1647. {
  1648. #ifndef MPEXT_NO_CACHE
  1649. if (pData->nListMode&FZ_LIST_USECACHE)
  1650. {
  1651. t_directory dir;
  1652. CDirectoryCache cache;
  1653. BOOL res = cache.Lookup(realpath, m_CurrentServer,dir);
  1654. if (res)
  1655. {
  1656. BOOL bExact = TRUE;
  1657. if (pData->nListMode & FZ_LIST_EXACT)
  1658. for (int i = 0; i < dir.num; i++)
  1659. if (dir.direntry[i].bUnsure || (dir.direntry[i].size == -1 && !dir.direntry[i].dir))
  1660. {
  1661. bExact = FALSE;
  1662. break;
  1663. }
  1664. if (bExact)
  1665. {
  1666. ShowStatus(IDS_STATUSMSG_DIRLISTSUCCESSFUL,0);
  1667. SetDirectoryListing(&dir);
  1668. ResetOperation(FZ_REPLY_OK);
  1669. return;
  1670. }
  1671. }
  1672. }
  1673. #endif
  1674. m_Operation.nOpState = NeedModeCommand() ? LIST_MODE : (NeedOptsCommand() ? LIST_OPTS : LIST_TYPE);;
  1675. }
  1676. }
  1677. }
  1678. else
  1679. m_Operation.nOpState = LIST_PWD;
  1680. }
  1681. CString cmd;
  1682. if (m_Operation.nOpState == LIST_PWD)
  1683. cmd=_T("PWD");
  1684. else if (m_Operation.nOpState==LIST_CWD)
  1685. cmd=_T("CWD ") + pData->path.GetPath(); //Command to retrieve the current directory
  1686. else if (m_Operation.nOpState==LIST_PWD2)
  1687. cmd=_T("PWD");
  1688. else if (m_Operation.nOpState==LIST_CWD2)
  1689. {
  1690. if (!pData->subdir)
  1691. {
  1692. ResetOperation(FZ_REPLY_ERROR);
  1693. return;
  1694. }
  1695. if (pData->subdir != _T("..") )
  1696. {
  1697. if (m_CurrentServer.nServerType & FZ_SERVERTYPE_SUB_FTP_VMS)
  1698. {
  1699. CServerPath path = m_pOwner->GetCurrentPath();
  1700. path.AddSubdir(pData->subdir);
  1701. cmd = _T("CWD ") + path.GetPath();
  1702. }
  1703. else
  1704. cmd = _T("CWD ") + pData->subdir;
  1705. }
  1706. else
  1707. {
  1708. if (pData->lastCmdSentCDUP)
  1709. {
  1710. pData->lastCmdSentCDUP = false;
  1711. cmd = _T("CWD ..");
  1712. }
  1713. else
  1714. {
  1715. pData->lastCmdSentCDUP = true;
  1716. cmd = _T("CDUP");
  1717. }
  1718. }
  1719. }
  1720. else if (m_Operation.nOpState == LIST_PWD3)
  1721. cmd=_T("PWD");
  1722. else if (m_Operation.nOpState == LIST_MODE)
  1723. {
  1724. #ifdef MPEXT_NO_ZLIB
  1725. ASSERT(false);
  1726. #else
  1727. if (m_useZlib)
  1728. #endif
  1729. cmd = _T("MODE S");
  1730. #ifndef MPEXT_NO_ZLIB
  1731. else
  1732. cmd = _T("MODE Z");
  1733. #endif
  1734. }
  1735. else if (m_Operation.nOpState == LIST_OPTS)
  1736. {
  1737. #ifdef MPEXT_NO_ZLIB
  1738. ASSERT(false);
  1739. #else
  1740. pData->newZlibLevel = COptions::GetOptionVal(OPTION_MODEZ_LEVEL);
  1741. cmd.Format(_T("OPTS MODE Z LEVEL %d"), pData->newZlibLevel);
  1742. #endif
  1743. }
  1744. else if (m_Operation.nOpState == LIST_PORT_PASV)
  1745. {
  1746. m_pTransferSocket = new CTransferSocket(this, m_Operation.nOpMode);
  1747. #ifndef MPEXT_NO_ZLIB
  1748. if (m_useZlib)
  1749. {
  1750. if (!m_pTransferSocket->InitZlib(m_zlibLevel))
  1751. {
  1752. ShowStatus(_T("Failed to initialize zlib"), 1);
  1753. ResetOperation(FZ_REPLY_ERROR);
  1754. return;
  1755. }
  1756. }
  1757. #endif
  1758. m_pTransferSocket->m_nInternalMessageID = m_pOwner->m_nInternalMessageID;
  1759. #ifndef MPEXT_NO_GSS
  1760. if (m_pGssLayer && m_pGssLayer->AuthSuccessful())
  1761. m_pTransferSocket->UseGSS(m_pGssLayer);
  1762. #endif
  1763. m_pTransferSocket->SetFamily(GetFamily());
  1764. if (!m_pTransferSocket->Create(
  1765. #ifndef MPEXT_NO_SSL
  1766. m_pSslLayer && m_bProtP
  1767. #endif
  1768. ) ||
  1769. !m_pTransferSocket->AsyncSelect())
  1770. {
  1771. ShowStatus(_T("Failed to create socket"), 1);
  1772. ResetOperation(FZ_REPLY_ERROR);
  1773. return;
  1774. }
  1775. if (pData->bPasv)
  1776. switch (GetFamily())
  1777. {
  1778. case AF_INET:
  1779. cmd = _T("PASV");
  1780. break;
  1781. case AF_INET6:
  1782. cmd = _T("EPSV");
  1783. break;
  1784. default:
  1785. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Protocol %d not supported"), GetFamily());
  1786. ResetOperation(FZ_REPLY_ERROR);
  1787. return;
  1788. }
  1789. else
  1790. {
  1791. m_pTransferSocket->m_bListening=TRUE;
  1792. if (m_pProxyLayer)
  1793. {
  1794. SOCKADDR_IN addr;
  1795. int len=sizeof(addr);
  1796. if (!m_pProxyLayer->GetPeerName((SOCKADDR *)&addr,&len))
  1797. {
  1798. ResetOperation(FZ_REPLY_ERROR);
  1799. return;
  1800. }
  1801. else if (!m_pTransferSocket->Listen(addr.sin_addr.S_un.S_addr))
  1802. {
  1803. ResetOperation(FZ_REPLY_ERROR);
  1804. return;
  1805. }
  1806. }
  1807. else
  1808. {
  1809. //Set up an active file transfer
  1810. CString tempHostname;
  1811. UINT nPort;
  1812. if (// create listen socket (let MFC choose the port) & start the socket listening
  1813. !m_pTransferSocket->Listen() ||
  1814. !m_pTransferSocket->GetSockName(tempHostname, nPort))
  1815. {
  1816. ShowStatus(_T("Failed to create listen socket"), 1);
  1817. ResetOperation(FZ_REPLY_ERROR);
  1818. return;
  1819. }
  1820. CString host;
  1821. bool bError = false;
  1822. if (GetFamily() == AF_INET)
  1823. {
  1824. host = COptions::GetOption(OPTION_TRANSFERIP);
  1825. if (host != "")
  1826. {
  1827. DWORD ip = inet_addr(T2CA(host));
  1828. if (ip != INADDR_NONE)
  1829. host.Format(_T("%d,%d,%d,%d"), ip%256, (ip>>8)%256, (ip>>16)%256, ip>>24);
  1830. else
  1831. {
  1832. hostent *fullname = gethostbyname(T2CA(host));
  1833. if (!fullname)
  1834. host = "";
  1835. else
  1836. {
  1837. DWORD ip = ((LPIN_ADDR)fullname->h_addr)->s_addr;
  1838. if (ip != INADDR_NONE)
  1839. host.Format(_T("%d,%d,%d,%d"), ip%256, (ip>>8)%256, (ip>>16)%256, ip>>24);
  1840. else
  1841. host = "";
  1842. }
  1843. }
  1844. }
  1845. if (host == "")
  1846. {
  1847. UINT temp;
  1848. if (!GetSockName(host, temp))
  1849. {
  1850. ShowStatus(_T("Failed to get socket address "), 1);
  1851. bError = true;
  1852. }
  1853. host.Replace('.', ',');
  1854. }
  1855. if (!bError)
  1856. {
  1857. host.Format(host+",%d,%d", nPort/256, nPort%256);
  1858. cmd = _T("PORT ") + host; // send PORT cmd to server
  1859. }
  1860. }
  1861. else if (GetFamily() == AF_INET6)
  1862. {
  1863. host = COptions::GetOption(OPTION_TRANSFERIP6);
  1864. if (host != "")
  1865. {
  1866. USES_CONVERSION;
  1867. addrinfo hints, *res;
  1868. memset(&hints, 0, sizeof(addrinfo));
  1869. hints.ai_family = AF_INET6;
  1870. hints.ai_socktype = SOCK_STREAM;
  1871. if (!p_getaddrinfo(T2CA(host), "1024", &hints, &res))
  1872. {
  1873. host = Inet6AddrToString(((SOCKADDR_IN6 *)res->ai_addr)->sin6_addr);
  1874. p_freeaddrinfo(res);
  1875. }
  1876. else
  1877. host = _T("");
  1878. }
  1879. if (host == "")
  1880. {
  1881. UINT temp;
  1882. if(!GetSockName(host, temp))
  1883. bError = true;
  1884. }
  1885. if (!bError)
  1886. {
  1887. // assamble EPRT command
  1888. cmd.Format(_T("EPRT |2|") + host + "|%d|", nPort);
  1889. }
  1890. }
  1891. else
  1892. {
  1893. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Protocol %d not supported"), GetFamily());
  1894. bError = true;
  1895. }
  1896. if (bError)
  1897. {
  1898. ResetOperation(FZ_REPLY_ERROR);
  1899. return;
  1900. }
  1901. }
  1902. }
  1903. }
  1904. else if (m_Operation.nOpState==LIST_TYPE)
  1905. cmd=_T("TYPE A");
  1906. else if (m_Operation.nOpState==LIST_LIST)
  1907. {
  1908. if (!m_pTransferSocket)
  1909. {
  1910. LogMessage(__FILE__, __LINE__, this,FZ_LOG_APIERROR, _T("Error: m_pTransferSocket==NULL") );
  1911. ResetOperation(FZ_REPLY_ERROR);
  1912. return;
  1913. }
  1914. m_pTransferSocket->SetActive();
  1915. cmd = _T("LIST");
  1916. #ifdef MPEXT
  1917. if (TRUE && !(m_CurrentServer.nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_VMS | FZ_SERVERTYPE_SUB_FTP_BS2000)))
  1918. #else
  1919. if (m_pOwner->GetOption(FZAPI_OPTION_SHOWHIDDEN) && !(m_CurrentServer.nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_VMS | FZ_SERVERTYPE_SUB_FTP_BS2000)))
  1920. #endif
  1921. cmd += _T(" -a");
  1922. if (!Send(cmd))
  1923. return;
  1924. pData->ListStartTime=CTime::GetCurrentTime();
  1925. if (pData->bPasv)
  1926. {
  1927. // if PASV create the socket & initiate outbound data channel connection
  1928. if (!m_pTransferSocket->Connect(pData->host,pData->port))
  1929. {
  1930. if (GetLastError()!=WSAEWOULDBLOCK)
  1931. {
  1932. ResetOperation(FZ_REPLY_ERROR);
  1933. return;
  1934. }
  1935. }
  1936. }
  1937. return;
  1938. }
  1939. if (cmd != _T(""))
  1940. Send(cmd);
  1941. }
  1942. void CFtpControlSocket::TransferEnd(int nMode)
  1943. {
  1944. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("TransferEnd(%d) OpMode=%d OpState=%d"), nMode, m_Operation.nOpMode, m_Operation.nOpState);
  1945. if (!m_Operation.nOpMode)
  1946. {
  1947. LogMessage(__FILE__, __LINE__, this,FZ_LOG_INFO, _T("Ignoring old TransferEnd message"));
  1948. return;
  1949. }
  1950. m_LastRecvTime=CTime::GetCurrentTime();
  1951. if (m_Operation.nOpMode&CSMODE_TRANSFER)
  1952. FileTransfer(0,TRUE,nMode&(CSMODE_TRANSFERERROR|CSMODE_TRANSFERTIMEOUT));
  1953. else if (m_Operation.nOpMode&CSMODE_LIST)
  1954. List(TRUE,nMode&(CSMODE_TRANSFERERROR|CSMODE_TRANSFERTIMEOUT));
  1955. }
  1956. void CFtpControlSocket::OnClose(int nErrorCode)
  1957. {
  1958. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("OnClose(%d) OpMode=%d OpState=%d"), nErrorCode, m_Operation.nOpMode, m_Operation.nOpState);
  1959. ShowStatus(IDS_STATUSMSG_DISCONNECTED, 1);
  1960. if (m_pTransferSocket)
  1961. {
  1962. m_pTransferSocket->OnClose(0);
  1963. m_pTransferSocket->Close();
  1964. delete m_pTransferSocket;
  1965. m_pTransferSocket=0;
  1966. DoClose();
  1967. ShowStatus(IDS_ERRORMSG_TIMEOUT,1);
  1968. return;
  1969. }
  1970. #ifndef MPEXT_NO_SSL
  1971. if (m_bDidRejectCertificate)
  1972. DoClose(FZ_REPLY_CANCEL);
  1973. else
  1974. #endif
  1975. DoClose();
  1976. }
  1977. void CFtpControlSocket::FileTransfer(t_transferfile *transferfile/*=0*/,BOOL bFinish/*=FALSE*/,int nError/*=0*/)
  1978. {
  1979. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("FileTransfer(%d, %s, %d) OpMode=%d OpState=%d"), transferfile,bFinish?_T("TRUE"):_T("FALSE"), nError, m_Operation.nOpMode, m_Operation.nOpState);
  1980. USES_CONVERSION;
  1981. #define FILETRANSFER_INIT -1
  1982. #define FILETRANSFER_PWD 0
  1983. #define FILETRANSFER_CWD 1
  1984. #define FILETRANSFER_MKD 2
  1985. #define FILETRANSFER_CWD2 3
  1986. #define FILETRANSFER_PWD2 4
  1987. #define FILETRANSFER_LIST_MODE 5
  1988. #define FILETRANSFER_LIST_OPTS 6
  1989. #define FILETRANSFER_LIST_PORTPASV 7
  1990. #define FILETRANSFER_LIST_TYPE 8
  1991. #define FILETRANSFER_LIST_LIST 9
  1992. #define FILETRANSFER_LIST_WAITFINISH 10
  1993. #define FILETRANSFER_NOLIST_SIZE 11
  1994. #define FILETRANSFER_NOLIST_MDTM 12
  1995. #define FILETRANSFER_TYPE 13
  1996. #define FILETRANSFER_REST 14
  1997. #define FILETRANSFER_MODE 15
  1998. #define FILETRANSFER_OPTS 16
  1999. #define FILETRANSFER_PORTPASV 17
  2000. #define FILETRANSFER_RETRSTOR 18
  2001. #define FILETRANSFER_WAITFINISH 19
  2002. #define FILETRANSFER_WAIT 20
  2003. //Partial flowchart of FileTransfer
  2004. //
  2005. // +----+
  2006. // /------|Init|--------\
  2007. // | +----+ |
  2008. // | | |
  2009. // | /---+ |
  2010. // | | | |
  2011. // | | +---+ |
  2012. // | | |PWD| |
  2013. // | | +---+ |
  2014. // | | | |
  2015. // | \---+ |
  2016. // | | |
  2017. // | +---+ |
  2018. // | |CWD|--\ |
  2019. // | +---+ | |
  2020. // | | | |
  2021. // | | | |
  2022. // | | +---+ |
  2023. // | | |MKD| |
  2024. // | | +---+ |
  2025. // | | | |
  2026. // | | | |
  2027. // | | +----+ |
  2028. // | | |CWD2| |
  2029. // | | +----+ |
  2030. // | | | |
  2031. // | +----/ |
  2032. // | | |
  2033. // | +---+ |
  2034. // +-------|PWD| |
  2035. // | +---+ |
  2036. // | | |
  2037. // | +----------/
  2038. // | |
  2039. // | +---------+
  2040. // | |LIST_TYPE|
  2041. // | +---------+
  2042. // | |
  2043. // | |
  2044. // | +-------------+
  2045. // | |LIST_PORTPASV|
  2046. // | +-------------+
  2047. // | |
  2048. // | |
  2049. // | +---------+
  2050. // | |LIST_LIST|-----\ //List failes, maybe folder is list protected
  2051. // | +---------+ | //Use SIZE and MDTM to get file information
  2052. // | | +----+
  2053. // | | |SIZE|
  2054. // | | +----+
  2055. // | | |
  2056. // | | +----+
  2057. // | | |MDTM|
  2058. // | | +----+
  2059. // | | |
  2060. // | | |
  2061. // | +---------------+ |
  2062. // | |LIST_WAITFINISH| |
  2063. // | +---------------+ |
  2064. // | | |
  2065. // | | |
  2066. // | +----------/
  2067. // | |
  2068. // \---------+
  2069. // |
  2070. // +----+
  2071. // |TYPE|
  2072. // +----+
  2073. // |
  2074. // |
  2075. // +--------+
  2076. // |PORTPASV|--\
  2077. // +--------+ |
  2078. // | |
  2079. // | |
  2080. // | +----+
  2081. // | |REST|
  2082. // | +----+
  2083. // | |
  2084. // +------/
  2085. // |
  2086. // +--------+
  2087. // |RETRSTOR|
  2088. // +--------+
  2089. // |
  2090. // |
  2091. // +----------+
  2092. // |WAITFINISH|
  2093. // +----------+
  2094. ASSERT(!m_Operation.nOpMode || m_Operation.nOpMode&CSMODE_TRANSFER);
  2095. if (!m_pOwner->IsConnected())
  2096. {
  2097. m_Operation.nOpMode=CSMODE_TRANSFER|(transferfile->get?CSMODE_DOWNLOAD:CSMODE_UPLOAD);
  2098. ResetOperation(FZ_REPLY_ERROR|FZ_REPLY_DISCONNECTED);
  2099. return;
  2100. }
  2101. CFileTransferData *pData=static_cast<CFileTransferData *>(m_Operation.pData);
  2102. //Process finish and error messages
  2103. if (bFinish || nError)
  2104. {
  2105. ASSERT(m_Operation.nOpMode&CSMODE_TRANSFER);
  2106. // APPE failed, ignore this reply
  2107. if (m_Operation.nOpMode == FILETRANSFER_WAIT && bFinish)
  2108. return;
  2109. if (!(m_Operation.nOpMode&CSMODE_TRANSFER))
  2110. return;
  2111. if (nError)
  2112. {
  2113. if (m_Operation.nOpState == FILETRANSFER_LIST_LIST && nError & CSMODE_TRANSFERERROR)
  2114. { //Don't abort operation, use fallback to SIZE and MDTM (when actual LIST reply comes in)
  2115. if (m_pTransferSocket)
  2116. m_pTransferSocket=0;
  2117. delete m_pDirectoryListing;
  2118. m_pDirectoryListing=0;
  2119. }
  2120. else if (nError&CSMODE_TRANSFERTIMEOUT)
  2121. DoClose();
  2122. else
  2123. ResetOperation(FZ_REPLY_ERROR);
  2124. return;
  2125. }
  2126. if (m_Operation.nOpState <= FILETRANSFER_LIST_PORTPASV)
  2127. {
  2128. ResetOperation(FZ_REPLY_ERROR);
  2129. return;
  2130. }
  2131. else if (m_Operation.nOpState<=FILETRANSFER_LIST_WAITFINISH)
  2132. {
  2133. if (!m_pTransferSocket || m_pTransferSocket->m_bListening)
  2134. {
  2135. delete m_pDirectoryListing;
  2136. m_pDirectoryListing=0;
  2137. ResetOperation(FZ_REPLY_ERROR);
  2138. return;
  2139. }
  2140. int num=0;
  2141. pData->pDirectoryListing=new t_directory;
  2142. if (COptions::GetOptionVal(OPTION_DEBUGSHOWLISTING))
  2143. m_pTransferSocket->m_pListResult->SendToMessageLog(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID);
  2144. pData->pDirectoryListing->direntry=m_pTransferSocket->m_pListResult->getList(num, pData->ListStartTime);
  2145. pData->pDirectoryListing->num=num;
  2146. if (m_pTransferSocket->m_pListResult->m_server.nServerType&FZ_SERVERTYPE_SUB_FTP_VMS && m_CurrentServer.nServerType&FZ_SERVERTYPE_FTP)
  2147. m_CurrentServer.nServerType |= FZ_SERVERTYPE_SUB_FTP_VMS;
  2148. pData->pDirectoryListing->server = m_CurrentServer;
  2149. pData->pDirectoryListing->path.SetServer(m_CurrentServer);
  2150. pData->pDirectoryListing->path = pData->transferfile.remotepath;
  2151. if (pData->rawpwd!="")
  2152. {
  2153. if (!pData->pDirectoryListing->path.SetPath(pData->rawpwd))
  2154. {
  2155. delete m_pDirectoryListing;
  2156. m_pDirectoryListing=0;
  2157. delete m_pTransferSocket;
  2158. m_pTransferSocket=0;
  2159. ResetOperation(FZ_REPLY_ERROR);
  2160. return;
  2161. }
  2162. }
  2163. else
  2164. pData->pDirectoryListing->path=pData->transferfile.remotepath;
  2165. if (m_Operation.nOpState!=FILETRANSFER_LIST_WAITFINISH)
  2166. return;
  2167. }
  2168. else if (m_Operation.nOpState <= FILETRANSFER_PORTPASV)
  2169. {
  2170. ResetOperation(FZ_REPLY_ERROR);
  2171. return;
  2172. }
  2173. else if (m_Operation.nOpState<=FILETRANSFER_WAITFINISH)
  2174. {
  2175. if (m_pTransferSocket->m_bListening)
  2176. {
  2177. ResetOperation(FZ_REPLY_ERROR);
  2178. return;
  2179. }
  2180. pData->nGotTransferEndReply |= 2;
  2181. if (m_Operation.nOpState!=FILETRANSFER_WAITFINISH)
  2182. return;
  2183. else
  2184. {
  2185. delete m_pTransferSocket;
  2186. m_pTransferSocket=0;
  2187. }
  2188. }
  2189. }
  2190. //////////////////
  2191. //Initialization//
  2192. //////////////////
  2193. int nReplyError = 0;
  2194. if (m_Operation.nOpState == FILETRANSFER_INIT)
  2195. {
  2196. ASSERT(transferfile);
  2197. ASSERT(!m_Operation.nOpMode);
  2198. ASSERT(!m_Operation.pData);
  2199. CString str;
  2200. str.Format(transferfile->get?IDS_STATUSMSG_DOWNLOADSTART:IDS_STATUSMSG_UPLOADSTART,
  2201. transferfile->get ? transferfile->remotepath.FormatFilename(transferfile->remotefile) : transferfile->localfile);
  2202. ShowStatus(str,0);
  2203. m_Operation.nOpMode=CSMODE_TRANSFER|(transferfile->get?CSMODE_DOWNLOAD:CSMODE_UPLOAD);
  2204. m_Operation.pData=new CFileTransferData;
  2205. pData=static_cast<CFileTransferData *>(m_Operation.pData);
  2206. if (COptions::GetOptionVal(OPTION_PROXYTYPE)!=PROXYTYPE_NOPROXY && !m_CurrentServer.fwbypass)
  2207. pData->bPasv = TRUE;
  2208. else if (m_CurrentServer.nPasv == 1)
  2209. pData->bPasv = TRUE;
  2210. else if (m_CurrentServer.nPasv == 2)
  2211. pData->bPasv = FALSE;
  2212. else
  2213. pData->bPasv = COptions::GetOptionVal(OPTION_PASV);
  2214. //Replace invalid characters in the local filename
  2215. int pos=transferfile->localfile.ReverseFind('\\');
  2216. for (int i=(pos+1);i<transferfile->localfile.GetLength();i++)
  2217. if (transferfile->localfile[i]==':')
  2218. transferfile->localfile.SetAt(i, '_');
  2219. pData->transferfile=*transferfile;
  2220. pData->transferdata.transfersize=pData->transferfile.size;
  2221. pData->transferdata.transferleft=pData->transferfile.size;
  2222. pData->transferdata.bResume = FALSE;
  2223. pData->transferdata.bType = (pData->transferfile.nType == 1) ? TRUE : FALSE;
  2224. CServerPath path;
  2225. VERIFY(m_pOwner->GetCurrentPath(path));
  2226. if (path == pData->transferfile.remotepath)
  2227. {
  2228. if (m_pDirectoryListing)
  2229. {
  2230. m_Operation.nOpState=FILETRANSFER_TYPE;
  2231. CString remotefile=pData->transferfile.remotefile;
  2232. int i;
  2233. for (i=0; i<m_pDirectoryListing->num; i++)
  2234. {
  2235. if (m_pDirectoryListing->direntry[i].name==remotefile &&
  2236. ( m_pDirectoryListing->direntry[i].bUnsure || m_pDirectoryListing->direntry[i].size==-1 ))
  2237. {
  2238. delete m_pDirectoryListing;
  2239. m_pDirectoryListing=0;
  2240. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_LIST_MODE : (NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE);
  2241. break;
  2242. }
  2243. }
  2244. if (m_pDirectoryListing && i==m_pDirectoryListing->num)
  2245. {
  2246. nReplyError = CheckOverwriteFile();
  2247. if (!nReplyError)
  2248. {
  2249. if (pData->transferfile.get)
  2250. {
  2251. CString path=pData->transferfile.localfile;
  2252. if (path.ReverseFind('\\')!=-1)
  2253. {
  2254. path=path.Left(path.ReverseFind('\\')+1);
  2255. CString path2;
  2256. while (path!="")
  2257. {
  2258. path2+=path.Left(path.Find( _T("\\") )+1);
  2259. path=path.Mid(path.Find( _T("\\") )+1);
  2260. CreateDirectory(path2, 0);
  2261. }
  2262. }
  2263. }
  2264. }
  2265. }
  2266. }
  2267. else
  2268. {
  2269. #ifndef MPEXT_NO_CACHE
  2270. CDirectoryCache cache;
  2271. t_server server;
  2272. m_pOwner->GetCurrentServer(server);
  2273. t_directory dir;
  2274. BOOL res = cache.Lookup(pData->transferfile.remotepath,server,dir);
  2275. if (res)
  2276. {
  2277. CString remotefile=pData->transferfile.remotefile;
  2278. int i;
  2279. for (i=0; i<dir.num; i++)
  2280. {
  2281. if (dir.direntry[i].name==remotefile &&
  2282. ( dir.direntry[i].bUnsure || dir.direntry[i].size==-1 ))
  2283. {
  2284. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_LIST_MODE : (NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE);
  2285. break;
  2286. }
  2287. }
  2288. if (i == dir.num)
  2289. {
  2290. SetDirectoryListing(&dir);
  2291. m_Operation.nOpState=FILETRANSFER_TYPE;
  2292. nReplyError = CheckOverwriteFile();
  2293. if (!nReplyError)
  2294. {
  2295. if (pData->transferfile.get)
  2296. {
  2297. CString path=pData->transferfile.localfile;
  2298. if (path.ReverseFind('\\')!=-1)
  2299. {
  2300. path=path.Left(path.ReverseFind('\\')+1);
  2301. CString path2;
  2302. while (path!="")
  2303. {
  2304. path2+=path.Left(path.Find( _T("\\") )+1);
  2305. path=path.Mid(path.Find(_T( "\\") )+1);
  2306. CreateDirectory(path2, 0);
  2307. }
  2308. }
  2309. }
  2310. }
  2311. }
  2312. }
  2313. else
  2314. #endif
  2315. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_LIST_MODE : (NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE);
  2316. }
  2317. }
  2318. else
  2319. {
  2320. if (path.IsEmpty())
  2321. m_Operation.nOpState = FILETRANSFER_PWD;
  2322. else
  2323. m_Operation.nOpState = FILETRANSFER_CWD;
  2324. }
  2325. }
  2326. else
  2327. {
  2328. ///////////
  2329. //Replies//
  2330. ///////////
  2331. int code = GetReplyCode();
  2332. switch(m_Operation.nOpState)
  2333. {
  2334. case FILETRANSFER_PWD:
  2335. if (code != 2 && code != 3)
  2336. {
  2337. nReplyError = FZ_REPLY_ERROR;
  2338. break;
  2339. }
  2340. pData->rawpwd = GetReply();
  2341. if ((m_mayBeMvsFilesystem || m_mayBeBS2000Filesystem) && m_CurrentServer.nServerType & FZ_SERVERTYPE_FTP &&
  2342. pData->rawpwd[0] != '/')
  2343. {
  2344. m_mayBeMvsFilesystem = false;
  2345. m_mayBeBS2000Filesystem = false;
  2346. if (m_mayBeBS2000Filesystem)
  2347. m_CurrentServer.nServerType |= FZ_SERVERTYPE_SUB_FTP_BS2000;
  2348. else
  2349. m_CurrentServer.nServerType |= FZ_SERVERTYPE_SUB_FTP_MVS;
  2350. pData->transferfile.remotepath.SetServer(m_CurrentServer);
  2351. }
  2352. if (!ParsePwdReply(pData->rawpwd))
  2353. return;
  2354. if (m_pOwner->GetCurrentPath() == pData->transferfile.remotepath)
  2355. {
  2356. #ifndef MPEXT_NO_CACHE
  2357. CDirectoryCache cache;
  2358. t_server server;
  2359. m_pOwner->GetCurrentServer(server);
  2360. t_directory dir;
  2361. BOOL res = cache.Lookup(pData->transferfile.remotepath,server,dir);
  2362. if (res)
  2363. {
  2364. CString remotefile = pData->transferfile.remotefile;
  2365. int i;
  2366. for (i = 0; i < dir.num; i++)
  2367. {
  2368. if (dir.direntry[i].name == remotefile &&
  2369. (dir.direntry[i].bUnsure || dir.direntry[i].size == -1))
  2370. {
  2371. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_LIST_MODE : (NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE);
  2372. break;
  2373. }
  2374. }
  2375. if (i == dir.num)
  2376. {
  2377. SetDirectoryListing(&dir);
  2378. m_Operation.nOpState=FILETRANSFER_TYPE;
  2379. nReplyError = CheckOverwriteFile();
  2380. if (!nReplyError)
  2381. {
  2382. if (pData->transferfile.get)
  2383. {
  2384. CString path=pData->transferfile.localfile;
  2385. if (path.ReverseFind('\\')!=-1)
  2386. {
  2387. path=path.Left(path.ReverseFind('\\')+1);
  2388. CString path2;
  2389. while (path!="")
  2390. {
  2391. path2+=path.Left(path.Find( _T("\\") )+1);
  2392. path=path.Mid(path.Find(_T( "\\") )+1);
  2393. int res = CreateDirectory(path2, 0);
  2394. }
  2395. }
  2396. }
  2397. }
  2398. }
  2399. }
  2400. else
  2401. #endif
  2402. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_LIST_MODE : (NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE);
  2403. }
  2404. else
  2405. m_Operation.nOpState = LIST_CWD;
  2406. break;
  2407. case FILETRANSFER_CWD:
  2408. if (code != 2 && code != 3)
  2409. if (pData->transferfile.get)
  2410. {
  2411. pData->bUseAbsolutePaths = TRUE;
  2412. m_Operation.nOpState = FILETRANSFER_NOLIST_SIZE;
  2413. }
  2414. else
  2415. m_Operation.nOpState = FILETRANSFER_MKD;
  2416. else
  2417. m_Operation.nOpState = FILETRANSFER_PWD2;
  2418. break;
  2419. case FILETRANSFER_MKD:
  2420. switch(pData->nMKDOpState)
  2421. {
  2422. case MKD_FINDPARENT:
  2423. {
  2424. if (code == 2 || code == 3)
  2425. {
  2426. m_pOwner->SetCurrentPath(pData->MKDCurrent);
  2427. #ifndef MPEXT_NO_CACHE
  2428. if (!m_pDirectoryListing)
  2429. {
  2430. CDirectoryCache cache;
  2431. t_directory dir;
  2432. BOOL res = cache.Lookup(pData->MKDCurrent, m_CurrentServer, dir, TRUE);
  2433. if (res)
  2434. SetDirectoryListing(&dir);
  2435. }
  2436. #endif
  2437. pData->nMKDOpState=MKD_CHANGETOSUBDIR;
  2438. pData->MKDCurrent.AddSubdir(pData->MKDSegments.front());
  2439. CString Segment=pData->MKDSegments.front();
  2440. pData->MKDSegments.pop_front();
  2441. if (!Send( _T("MKD ") + Segment))
  2442. return;
  2443. }
  2444. else
  2445. {
  2446. if (!pData->MKDCurrent.HasParent())
  2447. nReplyError = FZ_REPLY_ERROR | ((code == 5) ? FZ_REPLY_CRITICALERROR : 0);
  2448. else
  2449. {
  2450. pData->MKDSegments.push_front(pData->MKDCurrent.GetLastSegment());
  2451. pData->MKDCurrent=pData->MKDCurrent.GetParent();
  2452. if (!Send("CWD "+pData->MKDCurrent.GetPath()))
  2453. return;
  2454. }
  2455. }
  2456. }
  2457. break;
  2458. case MKD_MAKESUBDIRS:
  2459. {
  2460. if (code == 2 || code == 3)
  2461. { //Create dir entry in parent dir
  2462. ASSERT(!pData->MKDSegments.empty());
  2463. pData->MKDCurrent.AddSubdir(pData->MKDSegments.front());
  2464. CString Segment=pData->MKDSegments.front();
  2465. pData->MKDSegments.pop_front();
  2466. if (Send( _T("MKD ") + Segment))
  2467. pData->nMKDOpState=MKD_CHANGETOSUBDIR;
  2468. else
  2469. return;
  2470. }
  2471. else
  2472. nReplyError=FZ_REPLY_ERROR;
  2473. }
  2474. break;
  2475. case MKD_CHANGETOSUBDIR:
  2476. {
  2477. if (code == 2 || code == 3 || //Creation successful
  2478. m_RecvBuffer.front() == "550 Directory already exists") //Creation was successful, although someone else did the work for us
  2479. {
  2480. CServerPath path2 = pData->MKDCurrent;
  2481. if (path2.HasParent())
  2482. {
  2483. CString name=path2.GetLastSegment();
  2484. path2=path2.GetParent();
  2485. #ifndef MPEXT_NO_CACHE
  2486. CDirectoryCache cache;
  2487. cache.Lock();
  2488. t_directory dir;
  2489. BOOL bCached=TRUE;
  2490. BOOL res=cache.Lookup(path2,m_CurrentServer,dir);
  2491. if (!res)
  2492. bCached=FALSE;
  2493. #else
  2494. t_directory dir;
  2495. BOOL res=FALSE;
  2496. #endif
  2497. if (!res && m_pDirectoryListing)
  2498. {
  2499. if (m_pDirectoryListing->path==path2)
  2500. {
  2501. dir=*m_pDirectoryListing;
  2502. res=TRUE;
  2503. }
  2504. }
  2505. t_directory WorkingDir;
  2506. BOOL bFound=m_pOwner->GetWorkingDir(&WorkingDir);
  2507. if (!res && bFound)
  2508. if (WorkingDir.path==path2)
  2509. {
  2510. dir=WorkingDir;
  2511. res=TRUE;
  2512. }
  2513. if (!res)
  2514. {
  2515. dir.path=path2;
  2516. dir.server=m_CurrentServer;
  2517. }
  2518. int i;
  2519. for (i=0; i<dir.num; i++)
  2520. if (dir.direntry[i].name == name)
  2521. {
  2522. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Dir already exists in cache!"));
  2523. break;
  2524. }
  2525. if (i==dir.num)
  2526. {
  2527. t_directory::t_direntry *entries = new t_directory::t_direntry[dir.num+1];
  2528. for (i=0;i<dir.num;i++)
  2529. entries[i]=dir.direntry[i];
  2530. entries[i].name=name;
  2531. entries[i].lName=name;
  2532. entries[i].lName.MakeLower();
  2533. entries[i].dir=TRUE;
  2534. entries[i].date.hasdate=FALSE;
  2535. entries[i].size=-1;
  2536. entries[i].bUnsure=FALSE;
  2537. delete [] dir.direntry;
  2538. dir.direntry=entries;
  2539. dir.num++;
  2540. #ifndef MPEXT_NO_CACHE
  2541. cache.Store(dir, bCached);
  2542. #endif
  2543. BOOL updated=FALSE;
  2544. if (m_pDirectoryListing && m_pDirectoryListing->path==dir.path)
  2545. {
  2546. updated=TRUE;
  2547. SetDirectoryListing(&dir);
  2548. }
  2549. if (!updated)
  2550. if (WorkingDir.path==dir.path)
  2551. {
  2552. updated=TRUE;
  2553. m_pOwner->SetWorkingDir(&dir);
  2554. }
  2555. }
  2556. #ifndef MPEXT_NO_CACHE
  2557. cache.Unlock();
  2558. #endif
  2559. }
  2560. }
  2561. //Continue operation even if MKD failed, maybe another thread did create this directory for us
  2562. if (pData->MKDSegments.empty())
  2563. m_Operation.nOpState=FILETRANSFER_CWD2;
  2564. else
  2565. {
  2566. if (Send( _T("CWD ") + pData->MKDCurrent.GetPath()))
  2567. pData->nMKDOpState=MKD_MAKESUBDIRS;
  2568. else
  2569. return;
  2570. }
  2571. }
  2572. break;
  2573. default:
  2574. ASSERT(FALSE);
  2575. }
  2576. break;
  2577. case FILETRANSFER_CWD2:
  2578. if (code != 2 && code != 3)
  2579. if (code == 4)
  2580. nReplyError=FZ_REPLY_ERROR;
  2581. else
  2582. nReplyError=FZ_REPLY_CRITICALERROR;
  2583. else
  2584. m_Operation.nOpState=FILETRANSFER_PWD2;
  2585. break;
  2586. case FILETRANSFER_PWD2:
  2587. if (code != 2 && code != 2)
  2588. nReplyError = FZ_REPLY_ERROR;
  2589. else
  2590. {
  2591. pData->rawpwd = GetReply();
  2592. if (!ParsePwdReply(pData->rawpwd))
  2593. return;
  2594. if (m_pOwner->GetCurrentPath() != pData->transferfile.remotepath)
  2595. {
  2596. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Real path and requested remote path do not match: \"%s\" \"%s\""), m_pOwner->GetCurrentPath().GetSafePath(), pData->transferfile.remotepath.GetSafePath());
  2597. nReplyError = FZ_REPLY_CRITICALERROR;
  2598. }
  2599. else
  2600. {
  2601. #ifndef MPEXT_NO_CACHE
  2602. CDirectoryCache cache;
  2603. t_server server;
  2604. m_pOwner->GetCurrentServer(server);
  2605. t_directory dir;
  2606. BOOL res = cache.Lookup(pData->transferfile.remotepath, server,dir);
  2607. if (res)
  2608. {
  2609. CString remotefile = pData->transferfile.remotefile;
  2610. int i;
  2611. for (i = 0; i < dir.num; i++)
  2612. {
  2613. if (dir.direntry[i].name == remotefile &&
  2614. (dir.direntry[i].bUnsure || dir.direntry[i].size == -1))
  2615. {
  2616. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_LIST_MODE : (NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE);
  2617. break;
  2618. }
  2619. }
  2620. if (i == dir.num)
  2621. {
  2622. SetDirectoryListing(&dir);
  2623. m_Operation.nOpState=FILETRANSFER_TYPE;
  2624. nReplyError=CheckOverwriteFile();
  2625. if (!nReplyError)
  2626. {
  2627. if (pData->transferfile.get)
  2628. {
  2629. CString path=pData->transferfile.localfile;
  2630. if (path.ReverseFind('\\')!=-1)
  2631. {
  2632. path=path.Left(path.ReverseFind('\\')+1);
  2633. CString path2;
  2634. while (path!="")
  2635. {
  2636. path2+=path.Left(path.Find( _T("\\") )+1);
  2637. path=path.Mid(path.Find( _T("\\") )+1);
  2638. CreateDirectory(path2, 0);
  2639. }
  2640. }
  2641. }
  2642. }
  2643. }
  2644. }
  2645. else
  2646. #endif
  2647. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_LIST_MODE : (NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE);
  2648. }
  2649. }
  2650. break;
  2651. case FILETRANSFER_LIST_MODE:
  2652. #ifdef MPEXT_NO_ZLIB
  2653. ASSERT(false);
  2654. m_Operation.nOpState = FILETRANSFER_LIST_TYPE;
  2655. #else
  2656. if (code == 2 || code == 3)
  2657. m_useZlib = !m_useZlib;
  2658. m_Operation.nOpState = NeedOptsCommand() ? FILETRANSFER_LIST_OPTS : FILETRANSFER_LIST_TYPE;
  2659. #endif
  2660. break;
  2661. case FILETRANSFER_LIST_OPTS:
  2662. #ifdef MPEXT_NO_ZLIB
  2663. ASSERT(false);
  2664. #else
  2665. if (code == 2 || code == 3)
  2666. m_zlibLevel = pData->newZlibLevel;
  2667. m_Operation.nOpState = FILETRANSFER_LIST_TYPE;
  2668. #endif
  2669. break;
  2670. case FILETRANSFER_LIST_TYPE:
  2671. if (code != 2 && code != 3)
  2672. nReplyError = FZ_REPLY_ERROR;
  2673. else
  2674. m_Operation.nOpState = FILETRANSFER_LIST_PORTPASV;
  2675. break;
  2676. case FILETRANSFER_LIST_PORTPASV:
  2677. if (code!=3 && code!=2)
  2678. {
  2679. if (!pData->bTriedPortPasvOnce)
  2680. {
  2681. pData->bTriedPortPasvOnce = TRUE;
  2682. pData->bPasv = !pData->bPasv;
  2683. }
  2684. else
  2685. nReplyError=FZ_REPLY_ERROR;
  2686. break;
  2687. }
  2688. if (pData->bPasv)
  2689. {
  2690. CString reply = GetReply();
  2691. int i = reply.Find( _T("(") );
  2692. int j = j = reply.Find( _T(")") );
  2693. // extract connect port number and IP from string returned by server
  2694. if (i==-1 || j==-1 || (i+11)>=j)
  2695. {
  2696. if (!pData->bTriedPortPasvOnce)
  2697. {
  2698. pData->bTriedPortPasvOnce = TRUE;
  2699. pData->bPasv = !pData->bPasv;
  2700. }
  2701. else
  2702. nReplyError = FZ_REPLY_ERROR;
  2703. break;
  2704. }
  2705. CString temp;
  2706. temp = reply.Mid(i+1,(j-i)-1);
  2707. if (GetFamily() == AF_INET)
  2708. {
  2709. int count=0;
  2710. int pos=0;
  2711. //Convert commas to dots
  2712. temp.Replace( _T(","), _T(".") );
  2713. while(1)
  2714. {
  2715. pos=temp.Find(_T("."),pos);
  2716. if (pos!=-1)
  2717. count++;
  2718. else
  2719. break;
  2720. pos++;
  2721. }
  2722. if (count!=5)
  2723. {
  2724. if (!pData->bTriedPortPasvOnce)
  2725. {
  2726. pData->bTriedPortPasvOnce = TRUE;
  2727. pData->bPasv = !pData->bPasv;
  2728. }
  2729. else
  2730. nReplyError = FZ_REPLY_ERROR;
  2731. break;
  2732. }
  2733. i=temp.ReverseFind('.');
  2734. pData->port=atol( T2CA( temp.Right(temp.GetLength()-(i+1)) ) ); //get ls byte of server socket
  2735. temp=temp.Left(i);
  2736. i=temp.ReverseFind('.');
  2737. pData->port+=256*atol( T2CA( temp.Right(temp.GetLength()-(i+1)) ) ); // add ms byte to server socket
  2738. pData->host=temp.Left(i);
  2739. }
  2740. else if (GetFamily() == AF_INET6)
  2741. {
  2742. temp = temp.Mid(3);
  2743. pData->port = atol( T2CA(temp.Left(temp.GetLength() - 1) ) );
  2744. if (pData->port < 0 || pData->port > 65535)
  2745. {
  2746. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Port %u not valid"), pData->port);
  2747. nReplyError = FZ_REPLY_ERROR;
  2748. break;
  2749. }
  2750. unsigned int tmpPort;
  2751. if (!GetPeerName(pData->host, tmpPort))
  2752. {
  2753. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("GetPeerName failed"));
  2754. nReplyError = FZ_REPLY_ERROR;
  2755. break;
  2756. }
  2757. }
  2758. m_pTransferSocket = new CTransferSocket(this, CSMODE_LIST);
  2759. #ifndef MPEXT_NO_ZLIB
  2760. if (m_useZlib)
  2761. {
  2762. if (!m_pTransferSocket->InitZlib(m_zlibLevel))
  2763. {
  2764. ShowStatus("Failed to initialize zlib", 1);
  2765. ResetOperation(FZ_REPLY_ERROR);
  2766. return;
  2767. }
  2768. }
  2769. #endif
  2770. #ifndef MPEXT_NO_GSS
  2771. if (m_pGssLayer && m_pGssLayer->AuthSuccessful())
  2772. m_pTransferSocket->UseGSS(m_pGssLayer);
  2773. #endif
  2774. m_pTransferSocket->m_nInternalMessageID = m_pOwner->m_nInternalMessageID;
  2775. m_pTransferSocket->SetFamily(GetFamily());
  2776. if (!m_pTransferSocket->Create(
  2777. #ifndef MPEXT_NO_SSL
  2778. m_pSslLayer && m_bProtP
  2779. #endif
  2780. ))
  2781. {
  2782. nReplyError = FZ_REPLY_ERROR;
  2783. break;
  2784. }
  2785. VERIFY(m_pTransferSocket->AsyncSelect());
  2786. }
  2787. m_Operation.nOpState=FILETRANSFER_LIST_LIST;
  2788. break;
  2789. case FILETRANSFER_LIST_LIST:
  2790. if (IsMisleadingListResponse())
  2791. {
  2792. ShowStatus(IDS_STATUSMSG_DIRLISTSUCCESSFUL, 0);
  2793. t_directory listing;
  2794. listing.server = m_CurrentServer;
  2795. listing.path.SetServer(m_CurrentServer);
  2796. if (pData->rawpwd != "")
  2797. {
  2798. if (!listing.path.SetPath(pData->rawpwd))
  2799. {
  2800. delete m_pDirectoryListing;
  2801. m_pDirectoryListing = 0;
  2802. delete m_pTransferSocket;
  2803. m_pTransferSocket = 0;
  2804. ResetOperation(FZ_REPLY_ERROR);
  2805. return;
  2806. }
  2807. }
  2808. else
  2809. listing.path = pData->transferfile.remotepath;
  2810. #ifndef MPEXT_NO_CACHE
  2811. CDirectoryCache cache;
  2812. cache.Lock();
  2813. t_directory dir;
  2814. if (cache.Lookup(listing.path, listing.server, dir))
  2815. listing.Merge(dir, pData->ListStartTime);
  2816. cache.Store(listing);
  2817. cache.Unlock();
  2818. #endif
  2819. SetDirectoryListing(&listing);
  2820. m_Operation.nOpState = FILETRANSFER_TYPE;
  2821. delete m_pTransferSocket;
  2822. m_pTransferSocket = 0;
  2823. nReplyError = CheckOverwriteFile();
  2824. if (!nReplyError)
  2825. {
  2826. if (pData->transferfile.get)
  2827. {
  2828. CString path=pData->transferfile.localfile;
  2829. if (path.ReverseFind('\\')!=-1)
  2830. {
  2831. path=path.Left(path.ReverseFind('\\')+1);
  2832. CString path2;
  2833. while (path!="")
  2834. {
  2835. path2+=path.Left(path.Find( _T("\\") )+1);
  2836. path=path.Mid(path.Find( _T("\\") )+1);
  2837. CreateDirectory(path2, 0);
  2838. }
  2839. }
  2840. }
  2841. }
  2842. }
  2843. else if (code==4 || code==5) //LIST failed, try getting file information using SIZE and MDTM
  2844. {
  2845. if (m_pTransferSocket)
  2846. delete m_pTransferSocket;
  2847. m_pTransferSocket=0;
  2848. m_Operation.nOpState = FILETRANSFER_NOLIST_SIZE;
  2849. }
  2850. else if (code!=1)
  2851. nReplyError=FZ_REPLY_ERROR;
  2852. else
  2853. m_Operation.nOpState=FILETRANSFER_LIST_WAITFINISH;
  2854. break;
  2855. case FILETRANSFER_LIST_WAITFINISH:
  2856. if (!bFinish)
  2857. {
  2858. if (code!=2 && code!=3)
  2859. nReplyError=FZ_REPLY_ERROR;
  2860. else
  2861. pData->nGotTransferEndReply = 1;
  2862. }
  2863. if (pData->nGotTransferEndReply && pData->pDirectoryListing)
  2864. {
  2865. #ifndef MPEXT_NO_CACHE
  2866. CDirectoryCache cache;
  2867. t_directory dir;
  2868. cache.Lock();
  2869. if (cache.Lookup(pData->pDirectoryListing->path, pData->pDirectoryListing->server, dir, TRUE))
  2870. {
  2871. pData->pDirectoryListing->Merge(dir, pData->ListStartTime);
  2872. }
  2873. cache.Store(*pData->pDirectoryListing);
  2874. cache.Unlock();
  2875. #endif
  2876. SetDirectoryListing(pData->pDirectoryListing);
  2877. delete m_pTransferSocket;
  2878. m_pTransferSocket=0;
  2879. m_Operation.nOpState=FILETRANSFER_TYPE;
  2880. nReplyError = CheckOverwriteFile();
  2881. if (!nReplyError)
  2882. {
  2883. if (pData->transferfile.get)
  2884. {
  2885. CString path=pData->transferfile.localfile;
  2886. if (path.ReverseFind('\\')!=-1)
  2887. {
  2888. path=path.Left(path.ReverseFind('\\')+1);
  2889. CString path2;
  2890. while (path!="")
  2891. {
  2892. path2+=path.Left(path.Find( _T("\\") )+1);
  2893. path=path.Mid(path.Find( _T("\\") )+1);
  2894. CreateDirectory(path2, 0);
  2895. }
  2896. }
  2897. }
  2898. }
  2899. pData->nGotTransferEndReply=0;
  2900. }
  2901. break;
  2902. case FILETRANSFER_NOLIST_SIZE:
  2903. if (code==2)
  2904. {
  2905. CString line = GetReply();
  2906. if ( line.GetLength()>4 && line.Left(4) == _T("213 ") )
  2907. {
  2908. __int64 size=_ttoi64(line.Mid(4));
  2909. ASSERT(!pData->pFileSize);
  2910. pData->pFileSize=new _int64;
  2911. *pData->pFileSize=size;
  2912. }
  2913. }
  2914. m_Operation.nOpState=FILETRANSFER_NOLIST_MDTM;
  2915. break;
  2916. case FILETRANSFER_NOLIST_MDTM:
  2917. if (code==2)
  2918. {
  2919. CString line = GetReply();
  2920. if ( line.GetLength()>4 && line.Left(4) == _T("213 ") )
  2921. {
  2922. int y=0, M=0, d=0, h=0, m=0;
  2923. line=line.Mid(4);
  2924. y=_ttoi(line.Left(4));
  2925. if (y && line.GetLength()>4)
  2926. {
  2927. line=line.Mid(4);
  2928. M=_ttoi(line.Left(2));
  2929. if (M && line.GetLength()>2)
  2930. {
  2931. line=line.Mid(2);
  2932. d=_ttoi(line.Left(2));
  2933. if (d && line.GetLength()>2)
  2934. {
  2935. line=line.Mid(2);
  2936. h=_ttoi(line.Left(2));
  2937. if (h && line.GetLength()>2)
  2938. {
  2939. line=line.Mid(2);
  2940. m=_ttoi(line.Left(2));
  2941. if (m && line.GetLength()>2)
  2942. {
  2943. line=line.Mid(2);
  2944. }
  2945. }
  2946. }
  2947. if (M>0 && M<=12 && d>0 && d<=31 && h>=0 && h<24 && m>=0 && m<60)
  2948. {
  2949. if (pData->pFileTime)
  2950. delete pData->pFileTime;
  2951. TRY
  2952. {
  2953. pData->pFileTime = new CTime(y, M, d, h, m, 0);
  2954. }
  2955. CATCH_ALL(e)
  2956. {
  2957. TCHAR buffer[1024];
  2958. CString str =_T("Exception creating CTime object: ");
  2959. if (e->GetErrorMessage(buffer, 1024, NULL))
  2960. str += buffer;
  2961. else
  2962. str += _T("Unknown exception");
  2963. LogMessageRaw(__FILE__, __LINE__, this, FZ_LOG_WARNING, str);
  2964. pData->pFileTime = 0;
  2965. }
  2966. END_CATCH_ALL;
  2967. }
  2968. }
  2969. }
  2970. }
  2971. }
  2972. m_Operation.nOpState=FILETRANSFER_TYPE;
  2973. nReplyError=CheckOverwriteFile();
  2974. break;
  2975. case FILETRANSFER_TYPE:
  2976. if (code!=2 && code!=3)
  2977. nReplyError = FZ_REPLY_ERROR;
  2978. m_Operation.nOpState = NeedModeCommand() ? FILETRANSFER_MODE : (NeedOptsCommand() ? FILETRANSFER_OPTS : FILETRANSFER_PORTPASV);
  2979. break;
  2980. case FILETRANSFER_WAIT:
  2981. if (!pData->nWaitNextOpState)
  2982. nReplyError=FZ_REPLY_ERROR;
  2983. else
  2984. m_Operation.nOpState=pData->nWaitNextOpState;
  2985. break;
  2986. case FILETRANSFER_MODE:
  2987. #ifdef MPEXT_NO_ZLIB
  2988. ASSERT(false);
  2989. m_Operation.nOpState = FILETRANSFER_PORTPASV;
  2990. #else
  2991. if (code == 2 || code == 3)
  2992. m_useZlib = !m_useZlib;
  2993. m_Operation.nOpState = NeedOptsCommand() ? FILETRANSFER_OPTS : FILETRANSFER_PORTPASV;
  2994. #endif
  2995. break;
  2996. case FILETRANSFER_OPTS:
  2997. #ifdef MPEXT_NO_ZLIB
  2998. ASSERT(false);
  2999. #else
  3000. if (code == 2 || code == 3)
  3001. m_zlibLevel = pData->newZlibLevel;
  3002. #endif
  3003. m_Operation.nOpState = FILETRANSFER_PORTPASV;
  3004. break;
  3005. case FILETRANSFER_PORTPASV:
  3006. if (code == 3 || code == 2)
  3007. {
  3008. if (pData->bPasv)
  3009. {
  3010. CString reply = GetReply();
  3011. int i = reply.Find( _T("(") );
  3012. int j = j = reply.Find( _T(")") );
  3013. // extract connect port number and IP from string returned by server
  3014. if(i==-1 || j==-1 || (GetFamily() == AF_INET && (i+11) >= j))
  3015. {
  3016. if (!pData->bTriedPortPasvOnce)
  3017. {
  3018. pData->bTriedPortPasvOnce = TRUE;
  3019. pData->bPasv = !pData->bPasv;
  3020. }
  3021. else
  3022. nReplyError = FZ_REPLY_ERROR;
  3023. break;
  3024. }
  3025. CString temp;
  3026. temp = reply.Mid(i+1,(j-i)-1);
  3027. if (GetFamily() == AF_INET)
  3028. {
  3029. int count=0;
  3030. int pos=0;
  3031. //Convert commas to dots
  3032. temp.Replace( _T(","), _T(".") );
  3033. while(1)
  3034. {
  3035. pos=temp.Find( _T("."), pos);
  3036. if (pos!=-1)
  3037. count++;
  3038. else
  3039. break;
  3040. pos++;
  3041. }
  3042. if (count!=5)
  3043. {
  3044. if (!pData->bTriedPortPasvOnce)
  3045. {
  3046. pData->bTriedPortPasvOnce = TRUE;
  3047. pData->bPasv = !pData->bPasv;
  3048. }
  3049. else
  3050. nReplyError = FZ_REPLY_ERROR;
  3051. break;
  3052. }
  3053. i=temp.ReverseFind('.');
  3054. pData->port=atol( T2CA( temp.Right(temp.GetLength()-(i+1)) ) ); //get ls byte of server socket
  3055. temp=temp.Left(i);
  3056. i=temp.ReverseFind('.');
  3057. pData->port+=256*atol( T2CA( temp.Right(temp.GetLength()-(i+1)) ) ); // add ms byte to server socket
  3058. pData->host=temp.Left(i);
  3059. }
  3060. else if (GetFamily() == AF_INET6)
  3061. {
  3062. temp = temp.Mid(3);
  3063. pData->port = atol( T2CA(temp.Left(temp.GetLength() - 1) ) );
  3064. if (pData->port < 0 || pData->port > 65535)
  3065. {
  3066. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Port %u not valid"), pData->port);
  3067. nReplyError = FZ_REPLY_ERROR;
  3068. break;
  3069. }
  3070. unsigned int tmpPort;
  3071. if (!GetPeerName(pData->host, tmpPort))
  3072. {
  3073. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("GetPeerName failed"));
  3074. nReplyError = FZ_REPLY_ERROR;
  3075. break;
  3076. }
  3077. }
  3078. else
  3079. {
  3080. nReplyError = FZ_REPLY_ERROR;
  3081. break;
  3082. }
  3083. m_pTransferSocket = new CTransferSocket(this, m_Operation.nOpMode);
  3084. #ifndef MPEXT_NO_ZLIB
  3085. if (m_useZlib)
  3086. {
  3087. if (!m_pTransferSocket->InitZlib(m_zlibLevel))
  3088. {
  3089. ShowStatus("Failed to initialize zlib", 1);
  3090. ResetOperation(FZ_REPLY_ERROR);
  3091. return;
  3092. }
  3093. }
  3094. #endif
  3095. #ifndef MPEXT_NO_GSS
  3096. if (m_pGssLayer && m_pGssLayer->AuthSuccessful())
  3097. m_pTransferSocket->UseGSS(m_pGssLayer);
  3098. #endif
  3099. m_pTransferSocket->m_nInternalMessageID = m_pOwner->m_nInternalMessageID;
  3100. m_pTransferSocket->SetFamily(GetFamily());
  3101. if (!m_pTransferSocket->Create(
  3102. #ifndef MPEXT_NO_SSL
  3103. m_pSslLayer && m_bProtP
  3104. #endif
  3105. ))
  3106. {
  3107. nReplyError = FZ_REPLY_ERROR;
  3108. break;
  3109. }
  3110. VERIFY(m_pTransferSocket->AsyncSelect());
  3111. }
  3112. if (pData->transferdata.bResume && pData->transferfile.get)
  3113. m_Operation.nOpState = FILETRANSFER_REST;
  3114. else
  3115. m_Operation.nOpState = FILETRANSFER_RETRSTOR;
  3116. BOOL res = FALSE;
  3117. if (!m_pDataFile)
  3118. m_pDataFile = new CFile;
  3119. if (pData->transferfile.get)
  3120. {
  3121. if (pData->transferdata.bResume)
  3122. res = m_pDataFile->Open(pData->transferfile.localfile,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate|CFile::shareDenyWrite);
  3123. else
  3124. res = m_pDataFile->Open(pData->transferfile.localfile,CFile::modeWrite|CFile::modeCreate|CFile::shareDenyWrite);
  3125. }
  3126. else
  3127. res = m_pDataFile->Open(pData->transferfile.localfile,CFile::modeRead|CFile::shareDenyWrite);
  3128. if (!res)
  3129. {
  3130. //Error opening the file
  3131. CString str;
  3132. str.Format(IDS_ERRORMSG_FILEOPENFAILED,pData->transferfile.localfile);
  3133. ShowStatus(str,1);
  3134. nReplyError = FZ_REPLY_ERROR;
  3135. break;
  3136. }
  3137. if (!m_pTransferSocket)
  3138. {
  3139. nReplyError=FZ_REPLY_ERROR;
  3140. break;
  3141. }
  3142. m_pTransferSocket->m_pFile = m_pDataFile;
  3143. if (!pData->transferfile.get)
  3144. {
  3145. pData->transferdata.transfersize=GetLength64(*m_pDataFile);
  3146. pData->transferdata.transferleft=pData->transferdata.transfersize;
  3147. if (pData->transferdata.bResume)
  3148. {
  3149. CString remotefile=pData->transferfile.remotefile;
  3150. if (m_pDirectoryListing)
  3151. for (int i = 0; i < m_pDirectoryListing->num; i++)
  3152. {
  3153. if (m_pDirectoryListing->direntry[i].name == remotefile)
  3154. {
  3155. pData->transferdata.transferleft -= m_pDirectoryListing->direntry[i].size;
  3156. break;
  3157. }
  3158. }
  3159. _int64 size = pData->transferdata.transfersize-pData->transferdata.transferleft;
  3160. LONG low = static_cast<LONG>(size&0xFFFFFFFF);
  3161. LONG high = static_cast<LONG>(size>>32);
  3162. if (SetFilePointer((HANDLE)m_pDataFile->m_hFile, low, &high, FILE_BEGIN)==0xFFFFFFFF && GetLastError()!=NO_ERROR)
  3163. {
  3164. ShowStatus(IDS_ERRORMSG_SETFILEPOINTER, 1);
  3165. nReplyError = FZ_REPLY_ERROR;
  3166. }
  3167. }
  3168. }
  3169. else
  3170. {
  3171. pData->transferdata.transfersize=-1;
  3172. CString remotefile=pData->transferfile.remotefile;
  3173. if (m_pDirectoryListing)
  3174. for (int i=0; i<m_pDirectoryListing->num; i++)
  3175. {
  3176. if (m_pDirectoryListing->direntry[i].name==remotefile)
  3177. {
  3178. if (pData->pFileTime)
  3179. {
  3180. delete pData->pFileTime;
  3181. pData->pFileTime = 0;
  3182. }
  3183. t_directory::t_direntry::t_date *pDate=&m_pDirectoryListing->direntry[i].date;
  3184. if (pDate->hasdate)
  3185. {
  3186. TRY
  3187. {
  3188. if (pDate->hastime)
  3189. pData->pFileTime = new CTime(pDate->year, pDate->month, pDate->day, pDate->hour, pDate->minute, 0);
  3190. else
  3191. pData->pFileTime = new CTime(pDate->year, pDate->month, pDate->day, 0, 0, 0);
  3192. TIME_ZONE_INFORMATION tzInfo = {0};
  3193. BOOL res = GetTimeZoneInformation(&tzInfo);
  3194. CTimeSpan span(0, 0, tzInfo.Bias + ((res == TIME_ZONE_ID_DAYLIGHT) ? tzInfo.DaylightBias : tzInfo.StandardBias), 0);
  3195. *pData->pFileTime += span;
  3196. }
  3197. CATCH_ALL(e)
  3198. {
  3199. TCHAR buffer[1024];
  3200. CString str =_T("Exception creating CTime object: ");
  3201. if (e->GetErrorMessage(buffer, 1024, NULL))
  3202. str += buffer;
  3203. else
  3204. str += _T("Unknown exception");
  3205. LogMessageRaw(__FILE__, __LINE__, this, FZ_LOG_WARNING, str);
  3206. pData->pFileTime = 0;
  3207. }
  3208. END_CATCH_ALL;
  3209. }
  3210. pData->transferdata.transfersize=m_pDirectoryListing->direntry[i].size;
  3211. }
  3212. }
  3213. else if (pData->pFileSize)
  3214. pData->transferdata.transfersize=*pData->pFileSize;
  3215. pData->transferdata.transferleft=pData->transferdata.transfersize;
  3216. }
  3217. }
  3218. else
  3219. if (!pData->bTriedPortPasvOnce)
  3220. {
  3221. pData->bTriedPortPasvOnce = TRUE;
  3222. pData->bPasv = !pData->bPasv;
  3223. }
  3224. else
  3225. nReplyError = FZ_REPLY_ERROR;
  3226. break;
  3227. case FILETRANSFER_REST:
  3228. { //Resume
  3229. if (code==3 || code==2)
  3230. {
  3231. LONG high = 0;
  3232. pData->transferdata.transferleft = pData->transferdata.transfersize - GetLength64(*m_pDataFile);
  3233. if (SetFilePointer((HANDLE)m_pDataFile->m_hFile, 0, &high, FILE_END)==0xFFFFFFFF && GetLastError()!=NO_ERROR)
  3234. {
  3235. ShowStatus(IDS_ERRORMSG_SETFILEPOINTER, 1);
  3236. nReplyError = FZ_REPLY_ERROR;
  3237. }
  3238. else
  3239. m_Operation.nOpState = FILETRANSFER_RETRSTOR;
  3240. }
  3241. else
  3242. {
  3243. if (code==5 && GetReply()[1]=='0')
  3244. {
  3245. if (pData->transferdata.transfersize!=-1 && pData->transferfile.get)
  3246. {
  3247. ASSERT(m_pDataFile);
  3248. if (GetLength64(*m_pDataFile) == pData->transferdata.transfersize)
  3249. {
  3250. ShowStatus(IDS_ERRORMSG_CANTRESUME_FINISH, 0);
  3251. ResetOperation(FZ_REPLY_OK);
  3252. return;
  3253. }
  3254. }
  3255. ShowStatus(IDS_ERRORMSG_CANTRESUME, 1);
  3256. pData->transferdata.transferleft=pData->transferdata.transfersize;
  3257. pData->transferdata.bResume=FALSE;
  3258. m_Operation.nOpState=FILETRANSFER_RETRSTOR;
  3259. }
  3260. else
  3261. nReplyError=FZ_REPLY_ERROR;
  3262. }
  3263. }
  3264. break;
  3265. case FILETRANSFER_RETRSTOR:
  3266. // A '1xy opening data connection' reply is expected if RETR/STOR/APPE
  3267. // is successful.
  3268. // On failure, it's a 4xy or 5xy reply.
  3269. // However, some servers send a 2xy transfer complete reply without opening a data
  3270. // connection if there's no data to send.
  3271. if (code==2)
  3272. {
  3273. //Transfer successful, however server did not open data connection
  3274. ResetOperation(FZ_REPLY_OK);
  3275. return;
  3276. }
  3277. else if (code!=1)
  3278. {
  3279. if (!pData->transferfile.get && pData->transferdata.bResume && pData->askOnResumeFail)
  3280. {
  3281. pData->askOnResumeFail = false;
  3282. delete m_pTransferSocket;
  3283. m_pTransferSocket = 0;
  3284. delete m_pDataFile;
  3285. m_pDataFile = 0;
  3286. pData->nGotTransferEndReply = 0;
  3287. nReplyError = CheckOverwriteFile();
  3288. }
  3289. else
  3290. {
  3291. nReplyError = FZ_REPLY_ERROR;
  3292. if (code == 5)
  3293. nReplyError |= FZ_REPLY_CRITICALERROR;
  3294. }
  3295. }
  3296. else
  3297. {
  3298. m_Operation.nOpState=FILETRANSFER_WAITFINISH;
  3299. //Look if we can find any information about the resume offset
  3300. if (!pData->transferfile.get && pData->transferdata.bResume)
  3301. {
  3302. _int64 nOffset = -1;
  3303. CString reply = GetReply();
  3304. reply.MakeLower();
  3305. int pos = reply.Find(_T("restarting at offset "));
  3306. if (pos != -1)
  3307. pos += _tcslen(_T("restarting at offset "));
  3308. reply = reply.Mid(pos);
  3309. int i;
  3310. for (i=0; i<reply.GetLength(); i++)
  3311. {
  3312. if (reply[i] < '0' || reply[i] > '9')
  3313. break;
  3314. }
  3315. if (i == reply.GetLength())
  3316. nOffset = _ttoi64(reply);
  3317. if (nOffset != -1 && m_pDataFile)
  3318. {
  3319. LONG low = 0;
  3320. LONG high = 0;
  3321. if (nOffset >= GetLength64(*m_pDataFile))
  3322. if (SetFilePointer((HANDLE)m_pDataFile->m_hFile, 0, &high, FILE_END)==0xFFFFFFFF && GetLastError()!=NO_ERROR)
  3323. {
  3324. ShowStatus(IDS_ERRORMSG_SETFILEPOINTER, 1);
  3325. nReplyError = FZ_REPLY_ERROR;
  3326. }
  3327. else
  3328. {
  3329. low=static_cast<LONG>(nOffset&0xFFFFFFFF);
  3330. high=static_cast<LONG>(nOffset>>32);
  3331. if (SetFilePointer((HANDLE)m_pDataFile->m_hFile, low, &high, FILE_BEGIN)==0xFFFFFFFF && GetLastError()!=NO_ERROR)
  3332. {
  3333. ShowStatus(IDS_ERRORMSG_SETFILEPOINTER, 1);
  3334. nReplyError = FZ_REPLY_ERROR;
  3335. }
  3336. }
  3337. }
  3338. if (!nReplyError)
  3339. m_pTransferSocket->SetActive();
  3340. }
  3341. else if (pData->bPasv)
  3342. m_pTransferSocket->SetActive();
  3343. }
  3344. break;
  3345. case FILETRANSFER_WAITFINISH:
  3346. if (!bFinish)
  3347. {
  3348. if (code == 1)
  3349. {
  3350. /* Some non-rfc959 compatible servers send more than one code 1yz reply, especially if using APPE.
  3351. * Just ignore the additional ones.
  3352. */
  3353. LogMessage(FZ_LOG_WARNING, _T("Server sent more than one code 1yz reply, ignoring additional reply"));
  3354. break;
  3355. }
  3356. else if (code!=2 && code!=3)
  3357. nReplyError = FZ_REPLY_ERROR;
  3358. else
  3359. {
  3360. pData->nGotTransferEndReply |= 1;
  3361. }
  3362. }
  3363. if (pData->nGotTransferEndReply==3)
  3364. {
  3365. if (COptions::GetOptionVal(OPTION_PRESERVEDOWNLOADFILETIME) && m_pDataFile)
  3366. {
  3367. if (pData->pFileTime)
  3368. {
  3369. SYSTEMTIME stime;
  3370. FILETIME ftime;
  3371. if (pData->pFileTime->GetAsSystemTime(stime))
  3372. if (SystemTimeToFileTime(&stime, &ftime))
  3373. SetFileTime((HANDLE)m_pDataFile->m_hFile, &ftime, &ftime, &ftime);
  3374. }
  3375. }
  3376. //Transfer successful
  3377. ResetOperation(FZ_REPLY_OK);
  3378. return;
  3379. }
  3380. break;
  3381. }
  3382. if (nReplyError)
  3383. { //Error transferring the file
  3384. ResetOperation(nReplyError);
  3385. return;
  3386. }
  3387. }
  3388. /////////////////
  3389. //Send commands//
  3390. /////////////////
  3391. BOOL bError=FALSE;
  3392. switch(m_Operation.nOpState)
  3393. {
  3394. case FILETRANSFER_PWD:
  3395. if (!Send("PWD"))
  3396. bError = TRUE;
  3397. break;
  3398. case FILETRANSFER_CWD:
  3399. if (!Send("CWD "+pData->transferfile.remotepath.GetPath()))
  3400. bError=TRUE;
  3401. break;
  3402. case FILETRANSFER_MKD:
  3403. if (pData->nMKDOpState==MKD_INIT)
  3404. {
  3405. if (!pData->transferfile.remotepath.HasParent())
  3406. {
  3407. LogMessage(__FILE__, __LINE__, this,FZ_LOG_WARNING, _T("Can't create root dir"));
  3408. ResetOperation(FZ_REPLY_CRITICALERROR);
  3409. return;
  3410. }
  3411. if (!Send("CWD "+pData->transferfile.remotepath.GetParent().GetPath()))
  3412. bError=TRUE;
  3413. pData->MKDCurrent=pData->transferfile.remotepath.GetParent();
  3414. pData->MKDSegments.push_front(pData->transferfile.remotepath.GetLastSegment());
  3415. pData->nMKDOpState=MKD_FINDPARENT;
  3416. }
  3417. break;
  3418. case FILETRANSFER_CWD2:
  3419. if (!Send("CWD "+pData->transferfile.remotepath.GetPath()))
  3420. bError=TRUE;
  3421. break;
  3422. case FILETRANSFER_PWD2:
  3423. if (!Send("PWD"))
  3424. bError=TRUE;
  3425. break;
  3426. case FILETRANSFER_LIST_MODE:
  3427. #ifdef MPEXT_NO_ZLIB
  3428. ASSERT(false);
  3429. #else
  3430. if (m_useZlib)
  3431. {
  3432. if (!Send("MODE S"))
  3433. bError = TRUE;
  3434. }
  3435. else
  3436. if (!Send("MODE Z"))
  3437. bError = TRUE;
  3438. #endif
  3439. break;
  3440. case FILETRANSFER_LIST_OPTS:
  3441. #ifdef MPEXT_NO_ZLIB
  3442. ASSERT(false);
  3443. #else
  3444. {
  3445. pData->newZlibLevel = COptions::GetOptionVal(OPTION_MODEZ_LEVEL);
  3446. CString str;
  3447. str.Format(_T("OPTS MODE Z LEVEL %d"), pData->newZlibLevel);
  3448. if (!Send(str))
  3449. bError = TRUE;
  3450. }
  3451. #endif
  3452. break;
  3453. case FILETRANSFER_LIST_PORTPASV:
  3454. delete m_pDirectoryListing;
  3455. m_pDirectoryListing=0;
  3456. if (pData->bPasv)
  3457. {
  3458. if (!Send((GetFamily() == AF_INET) ? "PASV" : "EPSV"))
  3459. bError=TRUE;
  3460. }
  3461. else
  3462. {
  3463. if (m_pTransferSocket)
  3464. {
  3465. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("m_pTransferSocket != 0"));
  3466. delete m_pTransferSocket;
  3467. }
  3468. m_pTransferSocket = new CTransferSocket(this, CSMODE_LIST);
  3469. #ifndef MPEXT_NO_ZLIB
  3470. if (m_useZlib)
  3471. {
  3472. if (!m_pTransferSocket->InitZlib(m_zlibLevel))
  3473. {
  3474. ShowStatus("Failed to initialize zlib", 1);
  3475. ResetOperation(FZ_REPLY_ERROR);
  3476. return;
  3477. }
  3478. }
  3479. #endif
  3480. #ifndef MPEXT_NO_GSS
  3481. if (m_pGssLayer && m_pGssLayer->AuthSuccessful())
  3482. m_pTransferSocket->UseGSS(m_pGssLayer);
  3483. #endif
  3484. m_pTransferSocket->m_nInternalMessageID = m_pOwner->m_nInternalMessageID;
  3485. m_pTransferSocket->m_bListening = TRUE;
  3486. m_pTransferSocket->SetFamily(GetFamily());
  3487. if(!m_pTransferSocket->Create(
  3488. #ifndef MPEXT_NO_SSL
  3489. m_pSslLayer && m_bProtP
  3490. #endif
  3491. ) || !m_pTransferSocket->AsyncSelect())
  3492. bError=TRUE;
  3493. else if (m_pProxyLayer)
  3494. {
  3495. SOCKADDR_IN addr;
  3496. int len=sizeof(addr);
  3497. if (!m_pProxyLayer->GetPeerName((SOCKADDR *)&addr,&len))
  3498. {
  3499. ShowStatus(IDS_ERRORMSG_CANTGETLIST,1);
  3500. bError=TRUE;
  3501. }
  3502. else if (!m_pTransferSocket->Listen(addr.sin_addr.S_un.S_addr))
  3503. {
  3504. ShowStatus(IDS_ERRORMSG_CANTGETLIST,1);
  3505. bError=TRUE;
  3506. }
  3507. //Don't send PORT command yet, params are unknown.
  3508. //will be sent in TransfersocketListenFinished
  3509. }
  3510. else
  3511. {
  3512. //Set up an active file transfer
  3513. CString temp;
  3514. UINT nPort;
  3515. if (//create listen socket (let Windows choose the port) & start listening
  3516. !m_pTransferSocket->Listen() ||
  3517. !m_pTransferSocket->GetSockName(temp, nPort))
  3518. {
  3519. bError = TRUE;
  3520. break;
  3521. }
  3522. CString host;
  3523. if (GetFamily() == AF_INET)
  3524. {
  3525. host = COptions::GetOption(OPTION_TRANSFERIP);
  3526. if (host != "")
  3527. {
  3528. DWORD ip = inet_addr(T2CA(host));
  3529. if (ip != INADDR_NONE)
  3530. host.Format(_T("%d,%d,%d,%d"), ip%256, (ip>>8)%256, (ip>>16)%256, ip>>24);
  3531. else
  3532. {
  3533. hostent *fullname = gethostbyname(T2CA(host));
  3534. if (!fullname)
  3535. host = "";
  3536. else
  3537. {
  3538. DWORD ip = ((LPIN_ADDR)fullname->h_addr)->s_addr;
  3539. if (ip != INADDR_NONE)
  3540. host.Format(_T("%d,%d,%d,%d"), ip%256, (ip>>8)%256, (ip>>16)%256, ip>>24);
  3541. else
  3542. host = "";
  3543. }
  3544. }
  3545. }
  3546. if (host == "")
  3547. {
  3548. UINT temp;
  3549. if (!GetSockName(host, temp))
  3550. {
  3551. bError = true;
  3552. break;
  3553. }
  3554. host.Replace('.', ',');
  3555. }
  3556. if (!bError)
  3557. {
  3558. host.Format(host+",%d,%d", nPort/256, nPort%256);
  3559. if (!Send(_T("PORT ") + host)) // send PORT cmd to server
  3560. bError = TRUE;
  3561. }
  3562. }
  3563. else if (GetFamily() == AF_INET6)
  3564. {
  3565. host = COptions::GetOption(OPTION_TRANSFERIP6);
  3566. if (host != "")
  3567. {
  3568. USES_CONVERSION;
  3569. addrinfo hints, *res;
  3570. memset(&hints, 0, sizeof(addrinfo));
  3571. hints.ai_family = AF_INET6;
  3572. hints.ai_socktype = SOCK_STREAM;
  3573. if (!p_getaddrinfo(T2CA(host), "1024", &hints, &res))
  3574. {
  3575. host = Inet6AddrToString(((SOCKADDR_IN6 *)res->ai_addr)->sin6_addr);
  3576. p_freeaddrinfo(res);
  3577. }
  3578. else
  3579. host = _T("");
  3580. }
  3581. if (host == "")
  3582. {
  3583. UINT temp;
  3584. if(!GetSockName(host, temp))
  3585. bError = true;
  3586. }
  3587. if (!bError)
  3588. {
  3589. // assamble EPRT command
  3590. CString cmd;
  3591. cmd.Format(_T("EPRT |2|") + host + "|%d|", nPort);
  3592. if (!Send(cmd))
  3593. bError = TRUE;
  3594. }
  3595. }
  3596. else
  3597. {
  3598. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Protocol %d not supported"), GetFamily());
  3599. bError = true;
  3600. }
  3601. }
  3602. }
  3603. break;
  3604. case FILETRANSFER_LIST_TYPE:
  3605. if (!Send("TYPE A"))
  3606. bError=TRUE;
  3607. break;
  3608. case FILETRANSFER_LIST_LIST:
  3609. {
  3610. if (!m_pTransferSocket)
  3611. {
  3612. LogMessage(__FILE__, __LINE__, this,FZ_LOG_APIERROR, _T("Error: m_pTransferSocket==NULL") );
  3613. ResetOperation(FZ_REPLY_ERROR);
  3614. return;
  3615. }
  3616. m_pTransferSocket->SetActive();
  3617. pData->ListStartTime=CTime::GetCurrentTime();
  3618. CString cmd="LIST";
  3619. #ifdef MPEXT
  3620. if ((TRUE || pData->transferfile.remotefile.Left(1)==".") && !(m_CurrentServer.nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_VMS | FZ_SERVERTYPE_SUB_FTP_BS2000)))
  3621. #else
  3622. if ((m_pOwner->GetOption(FZAPI_OPTION_SHOWHIDDEN) || pData->transferfile.remotefile.Left(1)==".") && !(m_CurrentServer.nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_VMS | FZ_SERVERTYPE_SUB_FTP_BS2000)))
  3623. #endif
  3624. cmd += " -a";
  3625. if(!Send(cmd))
  3626. bError=TRUE;
  3627. else if(pData->bPasv)
  3628. {
  3629. // if PASV create the socket & initiate outbound data channel connection
  3630. if (!m_pTransferSocket->Connect(pData->host,pData->port))
  3631. {
  3632. if (GetLastError()!=WSAEWOULDBLOCK)
  3633. {
  3634. bError=TRUE;
  3635. ShowStatus(IDS_ERRORMSG_CANTGETLIST,1);
  3636. }
  3637. }
  3638. }
  3639. }
  3640. break;
  3641. case FILETRANSFER_NOLIST_SIZE:
  3642. {
  3643. CString command = _T("SIZE ");
  3644. command += pData->transferfile.remotepath.FormatFilename(pData->transferfile.remotefile, !pData->bUseAbsolutePaths);
  3645. if (!Send(command))
  3646. bError=TRUE;
  3647. }
  3648. break;
  3649. case FILETRANSFER_NOLIST_MDTM:
  3650. {
  3651. CString command = _T("MDTM ");
  3652. command += pData->transferfile.remotepath.FormatFilename(pData->transferfile.remotefile, !pData->bUseAbsolutePaths);
  3653. if (!Send(command))
  3654. bError=TRUE;
  3655. }
  3656. break;
  3657. case FILETRANSFER_TYPE:
  3658. if (pData->transferfile.nType==1)
  3659. {
  3660. if (!Send("TYPE A"))
  3661. bError=TRUE;
  3662. }
  3663. else
  3664. if (!Send("TYPE I"))
  3665. bError=TRUE;
  3666. break;
  3667. case FILETRANSFER_MODE:
  3668. #ifdef MPEXT_NO_ZLIB
  3669. ASSERT(false);
  3670. #else
  3671. if (m_useZlib)
  3672. {
  3673. if (!Send("MODE S"))
  3674. bError = TRUE;
  3675. }
  3676. else
  3677. if (!Send("MODE Z"))
  3678. bError = TRUE;
  3679. #endif
  3680. break;
  3681. case FILETRANSFER_OPTS:
  3682. #ifdef MPEXT_NO_ZLIB
  3683. ASSERT(false);
  3684. #else
  3685. {
  3686. pData->newZlibLevel = COptions::GetOptionVal(OPTION_MODEZ_LEVEL);
  3687. CString str;
  3688. str.Format(_T("OPTS MODE Z LEVEL %d"), pData->newZlibLevel);
  3689. if (!Send(str))
  3690. bError = TRUE;
  3691. }
  3692. #endif
  3693. break;
  3694. case FILETRANSFER_PORTPASV:
  3695. if (pData->bPasv)
  3696. {
  3697. if (!Send((GetFamily() == AF_INET) ? "PASV" : "EPSV"))
  3698. bError=TRUE;
  3699. }
  3700. else
  3701. {
  3702. if (m_pTransferSocket)
  3703. {
  3704. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("m_pTransferSocket != 0"));
  3705. delete m_pTransferSocket;
  3706. }
  3707. m_pTransferSocket=new CTransferSocket(this, m_Operation.nOpMode);
  3708. #ifndef MPEXT_NO_ZLIB
  3709. if (m_useZlib)
  3710. {
  3711. if (!m_pTransferSocket->InitZlib(m_zlibLevel))
  3712. {
  3713. ShowStatus("Failed to initialize zlib", 1);
  3714. ResetOperation(FZ_REPLY_ERROR);
  3715. return;
  3716. }
  3717. }
  3718. #endif
  3719. #ifndef MPEXT_NO_GSS
  3720. if (m_pGssLayer && m_pGssLayer->AuthSuccessful())
  3721. m_pTransferSocket->UseGSS(m_pGssLayer);
  3722. #endif
  3723. m_pTransferSocket->m_nInternalMessageID=m_pOwner->m_nInternalMessageID;
  3724. m_pTransferSocket->m_bListening = TRUE;
  3725. m_pTransferSocket->SetFamily(GetFamily());
  3726. if(!m_pTransferSocket->Create(
  3727. #ifndef MPEXT_NO_SSL
  3728. m_pSslLayer && m_bProtP
  3729. #endif
  3730. ) || !m_pTransferSocket->AsyncSelect())
  3731. bError = TRUE;
  3732. else if (m_pProxyLayer)
  3733. {
  3734. SOCKADDR_IN addr;
  3735. int len=sizeof(addr);
  3736. if (!m_pProxyLayer->GetPeerName((SOCKADDR *)&addr,&len))
  3737. {
  3738. ShowStatus(IDS_ERRORMSG_CANTGETLIST,1);
  3739. bError=TRUE;
  3740. }
  3741. else if (!m_pTransferSocket->Listen(addr.sin_addr.S_un.S_addr))
  3742. {
  3743. ShowStatus(IDS_ERRORMSG_CANTGETLIST,1);
  3744. bError=TRUE;
  3745. }
  3746. //Don't send PORT command yet, params are unknown.
  3747. //will be sent in TransfersocketListenFinished
  3748. }
  3749. else
  3750. {
  3751. //Set up an active file transfer
  3752. CString temp;
  3753. UINT nPort;
  3754. if (//create listen socket (let Windows choose the port) & start listening
  3755. !m_pTransferSocket->Listen() ||
  3756. !m_pTransferSocket->GetSockName(temp, nPort))
  3757. {
  3758. bError = TRUE;
  3759. break;
  3760. }
  3761. CString host;
  3762. if (GetFamily() == AF_INET)
  3763. {
  3764. host = COptions::GetOption(OPTION_TRANSFERIP);
  3765. if (host != "")
  3766. {
  3767. DWORD ip = inet_addr(T2CA(host));
  3768. if (ip != INADDR_NONE)
  3769. host.Format(_T("%d,%d,%d,%d"), ip%256, (ip>>8)%256, (ip>>16)%256, ip>>24);
  3770. else
  3771. {
  3772. hostent *fullname = gethostbyname(T2CA(host));
  3773. if (!fullname)
  3774. host = "";
  3775. else
  3776. {
  3777. DWORD ip = ((LPIN_ADDR)fullname->h_addr)->s_addr;
  3778. if (ip != INADDR_NONE)
  3779. host.Format(_T("%d,%d,%d,%d"), ip%256, (ip>>8)%256, (ip>>16)%256, ip>>24);
  3780. else
  3781. host = "";
  3782. }
  3783. }
  3784. }
  3785. if (host == "")
  3786. {
  3787. UINT temp;
  3788. if (!GetSockName(host, temp))
  3789. bError = true;
  3790. host.Replace('.', ',');
  3791. }
  3792. if (!bError)
  3793. {
  3794. host.Format(host+",%d,%d", nPort/256, nPort%256);
  3795. if (!Send(_T("PORT ") + host)) // send PORT cmd to server
  3796. bError = TRUE;
  3797. }
  3798. }
  3799. else if (GetFamily() == AF_INET6)
  3800. {
  3801. host = COptions::GetOption(OPTION_TRANSFERIP6);
  3802. if (host != "")
  3803. {
  3804. USES_CONVERSION;
  3805. addrinfo hints, *res;
  3806. memset(&hints, 0, sizeof(addrinfo));
  3807. hints.ai_family = AF_INET6;
  3808. hints.ai_socktype = SOCK_STREAM;
  3809. if (!p_getaddrinfo(T2CA(host), "1024", &hints, &res))
  3810. {
  3811. host = Inet6AddrToString(((SOCKADDR_IN6 *)res->ai_addr)->sin6_addr);
  3812. p_freeaddrinfo(res);
  3813. }
  3814. else
  3815. host = _T("");
  3816. }
  3817. if (host == "")
  3818. {
  3819. UINT temp;
  3820. if(!GetSockName(host, temp))
  3821. bError = true;
  3822. }
  3823. if (!bError)
  3824. {
  3825. // assamble EPRT command
  3826. CString cmd;
  3827. cmd.Format(_T("EPRT |2|") + host + "|%d|", nPort);
  3828. if (!Send(cmd))
  3829. bError = TRUE;
  3830. }
  3831. }
  3832. else
  3833. {
  3834. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Protocol %d not supported"), GetFamily());
  3835. bError = true;
  3836. }
  3837. if (bError)
  3838. {
  3839. ResetOperation(FZ_REPLY_ERROR);
  3840. return;
  3841. }
  3842. }
  3843. }
  3844. break;
  3845. case FILETRANSFER_REST:
  3846. ASSERT(m_pDataFile);
  3847. {
  3848. CString command;
  3849. command.Format(_T("REST %I64d"), GetLength64(*m_pDataFile));
  3850. if (!Send(command))
  3851. bError=TRUE;
  3852. }
  3853. break;
  3854. case FILETRANSFER_RETRSTOR:
  3855. pData->transferdata.nTransferStart=pData->transferdata.transfersize-pData->transferdata.transferleft;
  3856. m_pTransferSocket->m_transferdata=pData->transferdata;
  3857. // send RETR/STOR command to server
  3858. if (!m_pTransferSocket)
  3859. {
  3860. LogMessage(__FILE__, __LINE__, this,FZ_LOG_APIERROR, _T("Error: m_pTransferSocket==NULL") );
  3861. ResetOperation(FZ_REPLY_ERROR);
  3862. return;
  3863. }
  3864. if ((pData->transferfile.get || !pData->transferdata.bResume) && !pData->bPasv)
  3865. m_pTransferSocket->SetActive();
  3866. CString filename;
  3867. filename = pData->transferfile.remotepath.FormatFilename(pData->transferfile.remotefile, !pData->bUseAbsolutePaths);
  3868. if(!Send((pData->transferfile.get?"RETR ":(pData->transferdata.bResume)?"APPE ":"STOR ")+ filename))
  3869. bError = TRUE;
  3870. else
  3871. {
  3872. if (pData->bPasv)
  3873. {// if PASV create the socket & initiate outbound data channel connection
  3874. if (!m_pTransferSocket->Connect(pData->host,pData->port))
  3875. {
  3876. if (GetLastError()!=WSAEWOULDBLOCK)
  3877. bError=TRUE;
  3878. }
  3879. }
  3880. }
  3881. break;
  3882. }
  3883. if (bError)
  3884. { //Error transferring the file
  3885. ResetOperation(FZ_REPLY_ERROR);
  3886. return;
  3887. }
  3888. }
  3889. void CFtpControlSocket::Cancel(BOOL bQuit/*=FALSE*/)
  3890. {
  3891. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("Cancel(%s) OpMode=%d OpState=%d"), bQuit?_T("TRUE"):_T("FALSE"), m_Operation.nOpMode, m_Operation.nOpState);
  3892. const int nOpMode = m_Operation.nOpMode;
  3893. if (nOpMode==CSMODE_CONNECT)
  3894. DoClose(FZ_REPLY_CANCEL);
  3895. else if (nOpMode & CSMODE_LIST)
  3896. {
  3897. if (m_Operation.nOpState == LIST_WAITFINISH)
  3898. m_skipReply = true;
  3899. ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_CANCEL);
  3900. }
  3901. else if (nOpMode & CSMODE_TRANSFER)
  3902. {
  3903. if (m_Operation.nOpState == FILETRANSFER_WAITFINISH || m_Operation.nOpState == FILETRANSFER_LIST_WAITFINISH)
  3904. m_skipReply = true;
  3905. ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_CANCEL | FZ_REPLY_ABORTED);
  3906. }
  3907. else if (nOpMode != CSMODE_NONE)
  3908. ResetOperation(FZ_REPLY_ERROR | FZ_REPLY_CANCEL);
  3909. if (nOpMode != CSMODE_NONE && !bQuit)
  3910. ShowStatus(IDS_ERRORMSG_INTERRUPTED, 1);
  3911. if (m_awaitsReply)
  3912. m_skipReply = true;
  3913. }
  3914. void CFtpControlSocket::TransfersocketListenFinished(unsigned int ip, unsigned short port)
  3915. {
  3916. if (m_Operation.nOpMode&CSMODE_TRANSFER || m_Operation.nOpMode&CSMODE_LIST)
  3917. {
  3918. CString host;
  3919. host.Format(_T("%d,%d,%d,%d,%d,%d"),ip%256,(ip>>8)%256,(ip>>16)%256,(ip>>24)%256,port%256,port>>8);
  3920. Send("PORT "+host);
  3921. }
  3922. }
  3923. void CFtpControlSocket::ResumeTransfer()
  3924. {
  3925. if (m_pTransferSocket && (m_Operation.nOpMode&CSMODE_TRANSFER || m_Operation.nOpMode&CSMODE_LIST))
  3926. {
  3927. m_pTransferSocket->OnSend(0);
  3928. m_pTransferSocket->OnReceive(0);
  3929. }
  3930. }
  3931. BOOL CFtpControlSocket::Create()
  3932. {
  3933. if (!COptions::GetOptionVal(OPTION_LIMITPORTRANGE))
  3934. return CAsyncSocketEx::Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, COptions::GetOptionVal(OPTION_ENABLE_IPV6) ? AF_UNSPEC : AF_INET);
  3935. else
  3936. {
  3937. int min=COptions::GetOptionVal(OPTION_PORTRANGELOW);
  3938. int max=COptions::GetOptionVal(OPTION_PORTRANGEHIGH);
  3939. if (min>=max)
  3940. {
  3941. ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,1);
  3942. return FALSE;
  3943. }
  3944. int startport = static_cast<int>(min+((double)rand()*(max-min))/(RAND_MAX+1));
  3945. int port = startport;
  3946. while (!CAsyncSocketEx::Create(port, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, 0, COptions::GetOptionVal(OPTION_ENABLE_IPV6) ? AF_UNSPEC : AF_INET))
  3947. {
  3948. port++;
  3949. if (port>max)
  3950. port=min;
  3951. if (port==startport)
  3952. {
  3953. ShowStatus(IDS_ERRORMSG_CANTCREATEDUETOPORTRANGE,1);
  3954. return FALSE;
  3955. }
  3956. if (!InitConnect())
  3957. return FALSE;
  3958. }
  3959. }
  3960. return TRUE;
  3961. }
  3962. void CFtpControlSocket::ResetOperation(int nSuccessful /*=FALSE*/)
  3963. {
  3964. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("ResetOperation(%d) OpMode=%d OpState=%d"), nSuccessful, m_Operation.nOpMode, m_Operation.nOpState);
  3965. if (nSuccessful & FZ_REPLY_CRITICALERROR)
  3966. nSuccessful |= FZ_REPLY_ERROR;
  3967. if (m_pTransferSocket)
  3968. delete m_pTransferSocket;
  3969. m_pTransferSocket=0;
  3970. #ifndef MPEXT_NO_IDENT
  3971. //There may be an active ident socket, close it
  3972. if (m_pIdentControl)
  3973. {
  3974. delete m_pIdentControl;
  3975. m_pIdentControl=0;
  3976. }
  3977. #endif
  3978. if (m_pDataFile)
  3979. delete m_pDataFile;
  3980. m_pDataFile=0;
  3981. if (m_Operation.nOpMode)
  3982. {
  3983. //Unset busy attribute so that new commands can be executed
  3984. m_pOwner->SetBusy(FALSE);
  3985. if (m_Operation.nOpMode&CSMODE_CONNECT && nSuccessful&FZ_REPLY_ERROR)
  3986. {
  3987. nSuccessful|=FZ_REPLY_DISCONNECTED;
  3988. ShowStatus(IDS_ERRORMSG_CANTCONNECT, 1);
  3989. }
  3990. if (m_Operation.nOpMode & (CSMODE_LIST|CSMODE_TRANSFER) && nSuccessful==FZ_REPLY_OK)
  3991. m_LastSendTime=CTime::GetCurrentTime();
  3992. //Update remote file entry
  3993. if (m_Operation.pData &&
  3994. m_Operation.nOpMode&CSMODE_TRANSFER &&
  3995. (!((CFileTransferData*)m_Operation.pData)->transferfile.get) &&
  3996. m_pDirectoryListing &&
  3997. m_Operation.nOpState>=FILETRANSFER_RETRSTOR)
  3998. {
  3999. CString filename=((CFileTransferData*)m_Operation.pData)->transferfile.remotefile;
  4000. CServerPath path=((CFileTransferData*)m_Operation.pData)->transferfile.remotepath;
  4001. #ifndef MPEXT_NO_CACHE
  4002. CDirectoryCache cache;
  4003. cache.Lock();
  4004. t_directory dir;
  4005. BOOL bCached=TRUE;
  4006. BOOL res=cache.Lookup(path, m_CurrentServer, dir);
  4007. if (!res)
  4008. bCached=FALSE;
  4009. #else
  4010. t_directory dir;
  4011. BOOL res=FALSE;
  4012. #endif
  4013. if (!res && m_pDirectoryListing)
  4014. {
  4015. if (m_pDirectoryListing->path==path)
  4016. {
  4017. dir=*m_pDirectoryListing;
  4018. res=TRUE;
  4019. }
  4020. }
  4021. t_directory WorkingDir;
  4022. BOOL bFound=m_pOwner->GetWorkingDir(&WorkingDir);
  4023. if (!res && bFound)
  4024. if (WorkingDir.path==path)
  4025. {
  4026. dir=WorkingDir;
  4027. res=TRUE;
  4028. }
  4029. if (res)
  4030. {
  4031. int i;
  4032. for (i=0; i<dir.num; i++)
  4033. if (dir.direntry[i].name==filename)
  4034. {
  4035. dir.direntry[i].bUnsure = TRUE;
  4036. if (nSuccessful & FZ_REPLY_ERROR)
  4037. {
  4038. dir.direntry[i].bUnsure = TRUE;
  4039. if (!((CFileTransferData *)m_Operation.pData)->transferfile.get)
  4040. dir.direntry[i].size = -1;
  4041. }
  4042. else if (nSuccessful & FZ_REPLY_ERROR)
  4043. {
  4044. if (!GetLength64(((CFileTransferData *)m_Operation.pData)->transferfile.localfile, dir.direntry[i].size))
  4045. dir.direntry[i].size = -1;
  4046. }
  4047. dir.direntry[i].date.hasdate = false;
  4048. break;
  4049. }
  4050. if (i==dir.num)
  4051. {
  4052. t_directory::t_direntry *entries=new t_directory::t_direntry[dir.num+1];
  4053. int i;
  4054. for (i=0; i<dir.num; i++)
  4055. entries[i]=dir.direntry[i];
  4056. entries[i].name=filename;
  4057. entries[i].lName=filename;
  4058. entries[i].lName.MakeLower();
  4059. entries[i].dir=FALSE;
  4060. entries[i].date.hasdate=FALSE;
  4061. entries[i].size=-1;
  4062. if (nSuccessful&FZ_REPLY_OK)
  4063. entries[i].bUnsure=FALSE;
  4064. else
  4065. entries[i].bUnsure=TRUE;
  4066. delete [] dir.direntry;
  4067. dir.direntry=entries;
  4068. dir.num++;
  4069. }
  4070. #ifndef MPEXT_NO_CACHE
  4071. cache.Store(dir, bCached);
  4072. #endif
  4073. BOOL updated=FALSE;
  4074. if (m_pDirectoryListing && m_pDirectoryListing->path==dir.path)
  4075. {
  4076. updated=TRUE;
  4077. SetDirectoryListing(&dir, bFound && WorkingDir.path == dir.path);
  4078. }
  4079. if (!updated)
  4080. if (bFound && WorkingDir.path==dir.path)
  4081. {
  4082. updated = TRUE;
  4083. m_pOwner->SetWorkingDir(&dir);
  4084. }
  4085. }
  4086. #ifndef MPEXT_NO_CACHE
  4087. cache.Unlock();
  4088. #endif
  4089. }
  4090. if (m_Operation.pData && nSuccessful&FZ_REPLY_ERROR)
  4091. {
  4092. if (m_Operation.nOpMode&CSMODE_TRANSFER)
  4093. if (nSuccessful&FZ_REPLY_ABORTED)
  4094. //Transfer aborted by user
  4095. ShowStatus((m_Operation.nOpMode&CSMODE_DOWNLOAD)?IDS_ERRORMSG_DOWNLOADABORTED:IDS_ERRORMSG_UPLOADABORTED,1);
  4096. else
  4097. ShowStatus(((CFileTransferData*)m_Operation.pData)->transferfile.get?IDS_ERRORMSG_DOWNLOADFAILED:IDS_ERRORMSG_UPLOADFAILED,1);
  4098. else if (m_Operation.nOpMode&CSMODE_LIST)
  4099. ShowStatus(IDS_ERRORMSG_CANTGETLIST,1);
  4100. }
  4101. else if (m_Operation.pData && m_Operation.nOpMode&CSMODE_TRANSFER && nSuccessful==FZ_REPLY_OK)
  4102. ShowStatus(((CFileTransferData*)m_Operation.pData)->transferfile.get?IDS_STATUSMSG_DOWNLOADSUCCESSFUL:IDS_STATUSMSG_UPLOADSUCCESSFUL,0);
  4103. }
  4104. else
  4105. {
  4106. //No operation in progress
  4107. nSuccessful&=FZ_REPLY_DISCONNECTED|FZ_REPLY_CANCEL;
  4108. if (!nSuccessful)
  4109. ASSERT(FALSE);
  4110. }
  4111. if (nSuccessful&FZ_REPLY_DISCONNECTED)
  4112. m_pOwner->SetWorkingDir(0); //Disconnected, reset working dir
  4113. if (m_Operation.nOpMode)
  4114. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_REPLY, m_pOwner->m_LastCommand.id), nSuccessful);
  4115. else
  4116. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_REPLY, 0), nSuccessful);
  4117. m_Operation.nOpMode=0;
  4118. m_Operation.nOpState=-1;
  4119. if (m_Operation.pData)
  4120. delete m_Operation.pData;
  4121. m_Operation.pData=0;
  4122. }
  4123. void CFtpControlSocket::Delete(CString filename, const CServerPath &path)
  4124. {
  4125. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("Delete(\"%s\", \"%s\") OpMode=%d OpState=%d"), filename, path.GetPath(), m_Operation.nOpMode, m_Operation.nOpState);
  4126. class CDeleteData : public CFtpControlSocket::t_operation::COpData
  4127. {
  4128. public:
  4129. CDeleteData() {}
  4130. virtual ~CDeleteData() {}
  4131. CString m_FileName;
  4132. CServerPath path;
  4133. };
  4134. if (filename!="")
  4135. {
  4136. ASSERT(!path.IsEmpty());
  4137. ASSERT(m_Operation.nOpMode==CSMODE_NONE);
  4138. ASSERT(m_Operation.nOpState==-1);
  4139. ASSERT(!m_Operation.pData);
  4140. m_Operation.nOpMode=CSMODE_DELETE;
  4141. if (!Send("DELE " + path.FormatFilename(filename)))
  4142. return;
  4143. CDeleteData *data=new CDeleteData;
  4144. data->m_FileName=filename;
  4145. data->path=path;
  4146. m_Operation.pData=data;
  4147. }
  4148. else
  4149. {
  4150. ASSERT(path.IsEmpty());
  4151. ASSERT(m_Operation.nOpMode==CSMODE_DELETE);
  4152. ASSERT(m_Operation.nOpState==-1);
  4153. ASSERT(m_Operation.pData);
  4154. int res=GetReplyCode();
  4155. if (res==2 || res==3)
  4156. { //Remove file from cached dirs
  4157. CDeleteData *pData=(CDeleteData *)m_Operation.pData;
  4158. #ifndef MPEXT_NO_CACHE
  4159. CDirectoryCache cache;
  4160. cache.Lock();
  4161. BOOL bCached=TRUE;
  4162. t_directory dir;
  4163. BOOL res=cache.Lookup(pData->path,m_CurrentServer,dir);
  4164. if (!res)
  4165. bCached=FALSE;
  4166. #else
  4167. t_directory dir;
  4168. BOOL res=FALSE;
  4169. #endif
  4170. if (!res && m_pDirectoryListing)
  4171. {
  4172. if (m_pDirectoryListing->path==pData->path)
  4173. {
  4174. dir=*m_pDirectoryListing;
  4175. res=TRUE;
  4176. }
  4177. }
  4178. t_directory WorkingDir;
  4179. BOOL bFound=m_pOwner->GetWorkingDir(&WorkingDir);
  4180. if (!res && bFound)
  4181. if (WorkingDir.path==pData->path)
  4182. {
  4183. dir=WorkingDir;
  4184. res=TRUE;
  4185. }
  4186. if (res)
  4187. {
  4188. BOOL found=FALSE;
  4189. for (int i=0;i<dir.num;i++)
  4190. {
  4191. if (dir.direntry[i].name==pData->m_FileName)
  4192. {
  4193. ASSERT(!dir.direntry[i].dir || dir.direntry[i].bLink);
  4194. found=TRUE;
  4195. break;
  4196. }
  4197. }
  4198. if (found)
  4199. {
  4200. t_directory::t_direntry *direntry=new t_directory::t_direntry[dir.num-1];
  4201. int j=0;
  4202. int i;
  4203. for (i=0; i<dir.num; i++)
  4204. {
  4205. if (dir.direntry[i].name==pData->m_FileName)
  4206. continue;
  4207. direntry[j]=dir.direntry[i];
  4208. j++;
  4209. }
  4210. delete [] dir.direntry;
  4211. dir.direntry=direntry;
  4212. dir.num--;
  4213. #ifndef MPEXT_NO_CACHE
  4214. cache.Store(dir, bCached);
  4215. #endif
  4216. BOOL updated=FALSE;
  4217. if (m_pDirectoryListing)
  4218. if (m_pDirectoryListing->path==dir.path)
  4219. {
  4220. updated=TRUE;
  4221. SetDirectoryListing(&dir);
  4222. }
  4223. if (!updated)
  4224. if (WorkingDir.path==dir.path)
  4225. {
  4226. updated=TRUE;
  4227. m_pOwner->SetWorkingDir(&dir);
  4228. }
  4229. }
  4230. }
  4231. #ifndef MPEXT_NO_CACHE
  4232. cache.Unlock();
  4233. #endif
  4234. }
  4235. ResetOperation(FZ_REPLY_OK);
  4236. }
  4237. }
  4238. void CFtpControlSocket::RemoveDir(CString dirname, const CServerPath &path)
  4239. {
  4240. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("RemoveDir(\"%s\", \"%s\") OpMode=%d OpState=%d"), dirname, path.GetPath(), m_Operation.nOpMode, m_Operation.nOpState);
  4241. class CRemoveDirData : public CFtpControlSocket::t_operation::COpData
  4242. {
  4243. public:
  4244. CRemoveDirData() {}
  4245. virtual ~CRemoveDirData() {}
  4246. CString m_DirName;
  4247. CServerPath path;
  4248. };
  4249. if (dirname != "")
  4250. {
  4251. ASSERT(!path.IsEmpty());
  4252. ASSERT(m_Operation.nOpMode == CSMODE_NONE);
  4253. ASSERT(m_Operation.nOpState == -1);
  4254. ASSERT(!m_Operation.pData);
  4255. m_Operation.nOpMode = CSMODE_RMDIR;
  4256. CServerPath newPath = path;
  4257. if (!newPath.AddSubdir(dirname))
  4258. {
  4259. ShowStatus(_T("Unable to concatenate path"), 1);
  4260. return;
  4261. }
  4262. if (!Send("RMD "+ newPath.GetPath()))
  4263. return;
  4264. CRemoveDirData *data = new CRemoveDirData;
  4265. data->m_DirName = dirname;
  4266. data->path = path;
  4267. m_Operation.pData = data;
  4268. }
  4269. else
  4270. {
  4271. ASSERT(path.IsEmpty());
  4272. ASSERT(m_Operation.nOpMode == CSMODE_RMDIR);
  4273. ASSERT(m_Operation.nOpState == -1);
  4274. ASSERT(m_Operation.pData);
  4275. int res = GetReplyCode();
  4276. if (res == 2 || res == 3)
  4277. { //Remove dir from cached dirs
  4278. CRemoveDirData *pData= (CRemoveDirData *)m_Operation.pData;
  4279. #ifndef MPEXT_NO_CACHE
  4280. CDirectoryCache cache;
  4281. cache.Lock();
  4282. BOOL bCached = TRUE;
  4283. t_directory dir;
  4284. BOOL res = cache.Lookup(pData->path, m_CurrentServer, dir);
  4285. if (!res)
  4286. bCached = FALSE;
  4287. #else
  4288. t_directory dir;
  4289. BOOL res = FALSE;
  4290. #endif
  4291. if (!res && m_pDirectoryListing)
  4292. {
  4293. if (m_pDirectoryListing->path == pData->path)
  4294. {
  4295. dir = *m_pDirectoryListing;
  4296. res = TRUE;
  4297. }
  4298. }
  4299. t_directory WorkingDir;
  4300. BOOL bFound = m_pOwner->GetWorkingDir(&WorkingDir);
  4301. if (!res && bFound)
  4302. if (WorkingDir.path == pData->path)
  4303. {
  4304. dir = WorkingDir;
  4305. res = TRUE;
  4306. }
  4307. if (res)
  4308. {
  4309. BOOL found = FALSE;
  4310. for (int i = 0; i < dir.num; i++)
  4311. {
  4312. if (dir.direntry[i].name == pData->m_DirName)
  4313. {
  4314. ASSERT(dir.direntry[i].dir);
  4315. found = TRUE;
  4316. break;
  4317. }
  4318. }
  4319. if (found)
  4320. {
  4321. t_directory::t_direntry *direntry = new t_directory::t_direntry[dir.num-1];
  4322. int j = 0;
  4323. int i;
  4324. for (i = 0; i < dir.num; i++)
  4325. {
  4326. if (dir.direntry[i].name == pData->m_DirName)
  4327. continue;
  4328. direntry[j] = dir.direntry[i];
  4329. j++;
  4330. }
  4331. delete [] dir.direntry;
  4332. dir.direntry = direntry;
  4333. dir.num--;
  4334. #ifndef MPEXT_NO_CACHE
  4335. cache.Store(dir, bCached);
  4336. #endif
  4337. BOOL updated = FALSE;
  4338. if (m_pDirectoryListing)
  4339. if (m_pDirectoryListing->path == dir.path)
  4340. {
  4341. updated = TRUE;
  4342. SetDirectoryListing(&dir);
  4343. }
  4344. if (!updated)
  4345. if (WorkingDir.path == dir.path)
  4346. {
  4347. updated = TRUE;
  4348. m_pOwner->SetWorkingDir(&dir);
  4349. }
  4350. }
  4351. }
  4352. #ifndef MPEXT_NO_CACHE
  4353. if (cache.Lookup(pData->path, pData->m_DirName, m_CurrentServer, dir))
  4354. cache.Purge(dir.path, dir.server);
  4355. cache.Unlock();
  4356. #endif
  4357. ResetOperation(FZ_REPLY_OK);
  4358. }
  4359. else
  4360. ResetOperation(FZ_REPLY_ERROR);
  4361. }
  4362. }
  4363. int CFtpControlSocket::CheckOverwriteFile()
  4364. {
  4365. if (!m_Operation.pData)
  4366. return FZ_REPLY_ERROR;
  4367. CFileTransferData *pData = reinterpret_cast<CFileTransferData *>(m_Operation.pData);
  4368. int nReplyError = 0;
  4369. CFileStatus64 status;
  4370. BOOL res = GetStatus64(pData->transferfile.localfile, status);
  4371. if (!res)
  4372. if (!pData->transferfile.get)
  4373. nReplyError = FZ_REPLY_CRITICALERROR; //File has to exist when uploading
  4374. else
  4375. m_Operation.nOpState = FILETRANSFER_TYPE;
  4376. else
  4377. {
  4378. if (status.m_attribute & 0x10)
  4379. nReplyError = FZ_REPLY_CRITICALERROR; //Can't transfer to/from dirs
  4380. else
  4381. {
  4382. _int64 localsize;
  4383. if (!GetLength64(pData->transferfile.localfile, localsize))
  4384. if (!pData->transferfile.get)
  4385. nReplyError = FZ_REPLY_CRITICALERROR;
  4386. else
  4387. m_Operation.nOpState = FILETRANSFER_TYPE;
  4388. CTime *localtime = NULL;
  4389. TRY
  4390. {
  4391. if (status.m_has_mtime && status.m_mtime != -1)
  4392. localtime = new CTime(status.m_mtime);
  4393. }
  4394. CATCH_ALL(e)
  4395. {
  4396. TCHAR buffer[1024];
  4397. CString str =_T("Exception creating CTime object: ");
  4398. if (e->GetErrorMessage(buffer, 1024, NULL))
  4399. str += buffer;
  4400. else
  4401. str += _T("Unknown exception");
  4402. LogMessageRaw(__FILE__, __LINE__, this, FZ_LOG_WARNING, str);
  4403. localtime = NULL;
  4404. }
  4405. END_CATCH_ALL;
  4406. BOOL bRemoteFileExists = FALSE;
  4407. __int64 remotesize = -1;
  4408. CTime *remotetime = 0;
  4409. if (m_pDirectoryListing)
  4410. {
  4411. for (int i=0; i<m_pDirectoryListing->num; i++)
  4412. {
  4413. CString remotefile = pData->transferfile.remotefile;
  4414. if (m_pDirectoryListing->direntry[i].name == remotefile)
  4415. {
  4416. remotesize = m_pDirectoryListing->direntry[i].size;
  4417. if (m_pDirectoryListing->direntry[i].date.hasdate)
  4418. {
  4419. TRY
  4420. {
  4421. remotetime = new CTime(m_pDirectoryListing->direntry[i].date.year,
  4422. m_pDirectoryListing->direntry[i].date.month,
  4423. m_pDirectoryListing->direntry[i].date.day,
  4424. m_pDirectoryListing->direntry[i].date.hastime?m_pDirectoryListing->direntry[i].date.hour:0,
  4425. m_pDirectoryListing->direntry[i].date.hastime?m_pDirectoryListing->direntry[i].date.minute:0,
  4426. 0,-1);
  4427. }
  4428. CATCH_ALL(e)
  4429. {
  4430. TCHAR buffer[1024];
  4431. CString str =_T("Exception creating CTime object: ");
  4432. if (e->GetErrorMessage(buffer, 1024, NULL))
  4433. str += buffer;
  4434. else
  4435. str += _T("Unknown exception");
  4436. LogMessageRaw(__FILE__, __LINE__, this, FZ_LOG_WARNING, str);
  4437. remotetime = NULL;
  4438. }
  4439. END_CATCH_ALL;
  4440. }
  4441. bRemoteFileExists = TRUE;
  4442. break;
  4443. }
  4444. }
  4445. }
  4446. if (remotesize == -1 && pData->pFileSize)
  4447. {
  4448. remotesize = *pData->pFileSize;
  4449. bRemoteFileExists = TRUE;
  4450. }
  4451. if (!remotetime && pData->pFileTime)
  4452. {
  4453. remotetime = new CTime;
  4454. *remotetime = *pData->pFileTime;
  4455. bRemoteFileExists = TRUE;
  4456. }
  4457. if (bRemoteFileExists || pData->transferfile.get )
  4458. {
  4459. COverwriteRequestData *pOverwriteData = new COverwriteRequestData;
  4460. t_transferfile *pTransferFile = new t_transferfile;
  4461. *pTransferFile = pData->transferfile;
  4462. pOverwriteData->pTransferFile = pTransferFile;
  4463. if (pData->transferfile.get)
  4464. {
  4465. int pos = pData->transferfile.localfile.ReverseFind('\\');
  4466. ASSERT(pos!=-1);
  4467. pOverwriteData->FileName1 = pData->transferfile.localfile.Mid(pos+1);
  4468. pOverwriteData->FileName2 = pData->transferfile.remotefile;
  4469. pOverwriteData->path1 = pData->transferfile.localfile.Left(pos+1);
  4470. pOverwriteData->path2 = pData->transferfile.remotepath.GetPath();
  4471. pOverwriteData->size1 = localsize;
  4472. pOverwriteData->size2 = remotesize;
  4473. pOverwriteData->time1 = localtime;
  4474. pOverwriteData->time2 = remotetime;
  4475. }
  4476. else
  4477. {
  4478. int pos = pData->transferfile.localfile.ReverseFind('\\');
  4479. ASSERT(pos!=-1);
  4480. pOverwriteData->FileName1 = pData->transferfile.remotefile;
  4481. pOverwriteData->FileName2 = pData->transferfile.localfile.Mid(pos+1);
  4482. pOverwriteData->path1 = pData->transferfile.remotepath.GetPath();
  4483. pOverwriteData->path2 = pData->transferfile.localfile.Left(pos+1);
  4484. pOverwriteData->size1 = remotesize;
  4485. pOverwriteData->size2 = localsize;
  4486. pOverwriteData->time1 = remotetime;
  4487. pOverwriteData->time2 = localtime;
  4488. }
  4489. pOverwriteData->nRequestID = m_pOwner->GetNextAsyncRequestID();
  4490. if (!PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_ASYNCREQUEST, FZ_ASYNCREQUEST_OVERWRITE), (LPARAM)pOverwriteData))
  4491. {
  4492. delete pOverwriteData;
  4493. nReplyError = FZ_REPLY_ERROR;
  4494. }
  4495. else
  4496. {
  4497. m_bCheckForTimeout = FALSE;
  4498. m_Operation.nOpState = FILETRANSFER_WAIT;
  4499. }
  4500. }
  4501. else
  4502. {
  4503. m_Operation.nOpState = FILETRANSFER_TYPE;
  4504. delete localtime;
  4505. delete remotetime;
  4506. }
  4507. }
  4508. }
  4509. return nReplyError;
  4510. }
  4511. void CFtpControlSocket::SetFileExistsAction(int nAction, COverwriteRequestData *pData)
  4512. {
  4513. if (!pData)
  4514. return;
  4515. if (!(m_Operation.nOpMode & CSMODE_TRANSFER))
  4516. return;
  4517. if (m_Operation.nOpState != FILETRANSFER_WAIT)
  4518. return;
  4519. CFileTransferData* pTransferData = reinterpret_cast<CFileTransferData*>(m_Operation.pData);
  4520. if (!pTransferData)
  4521. return;
  4522. pTransferData->transferdata.bResume = false;
  4523. m_bCheckForTimeout = TRUE;
  4524. int nReplyError = 0;
  4525. switch (nAction)
  4526. {
  4527. case FILEEXISTS_SKIP:
  4528. nReplyError = FZ_REPLY_OK;
  4529. break;
  4530. case FILEEXISTS_OVERWRITE:
  4531. pTransferData->nWaitNextOpState = FILETRANSFER_TYPE;
  4532. break;
  4533. case FILEEXISTS_OVERWRITEIFNEWER:
  4534. if ( !pData->time1 || !pData->time2 )
  4535. pTransferData->nWaitNextOpState = FILETRANSFER_TYPE;
  4536. else if (*pData->time2 <= *pData->time1)
  4537. nReplyError = FZ_REPLY_OK;
  4538. else
  4539. pTransferData->nWaitNextOpState = FILETRANSFER_TYPE;
  4540. break;
  4541. case FILEEXISTS_RENAME:
  4542. if (pTransferData->transferfile.get)
  4543. {
  4544. CFileStatus64 status;
  4545. if (GetStatus64(pData->FileName1, status))
  4546. {
  4547. ShowStatus(IDS_ERRORMSG_NAMEINUSE, 1);
  4548. nReplyError= FZ_REPLY_CRITICALERROR;
  4549. }
  4550. else
  4551. {
  4552. pTransferData->transferfile.localfile = pData->path1+pData->FileName1;
  4553. //Replace invalid characters in the local filename
  4554. int pos = pTransferData->transferfile.localfile.ReverseFind('\\');
  4555. for (int i = (pos+1); i < pTransferData->transferfile.localfile.GetLength(); i++)
  4556. if (pTransferData->transferfile.localfile[i] == ':')
  4557. pTransferData->transferfile.localfile.SetAt(i, '_');
  4558. pTransferData->nWaitNextOpState= FILETRANSFER_TYPE;
  4559. }
  4560. }
  4561. else
  4562. {
  4563. ASSERT(m_pDirectoryListing);
  4564. int i;
  4565. for (i = 0; i < m_pDirectoryListing->num; i++)
  4566. {
  4567. if (m_pDirectoryListing->direntry[i].name == pData->FileName1)
  4568. {
  4569. ShowStatus(IDS_ERRORMSG_NAMEINUSE, 1);
  4570. nReplyError = FZ_REPLY_CRITICALERROR;
  4571. break;
  4572. }
  4573. }
  4574. if (i==m_pDirectoryListing->num)
  4575. {
  4576. pTransferData->transferfile.remotefile = pData->FileName1;
  4577. pTransferData->nWaitNextOpState = FILETRANSFER_TYPE;
  4578. }
  4579. }
  4580. break;
  4581. case FILEEXISTS_RESUME_ASKONFAIL:
  4582. pTransferData->askOnResumeFail = true;
  4583. case FILEEXISTS_RESUME:
  4584. if (pData->size1 >= 0)
  4585. {
  4586. pTransferData->transferdata.bResume = TRUE;
  4587. }
  4588. pTransferData->nWaitNextOpState = FILETRANSFER_TYPE;
  4589. break;
  4590. }
  4591. if (nReplyError == FZ_REPLY_OK)
  4592. ResetOperation(FZ_REPLY_OK);
  4593. else if (nReplyError)
  4594. ResetOperation(FZ_REPLY_ERROR | nReplyError); //Error transferring the file
  4595. else
  4596. FileTransfer();
  4597. }
  4598. void CFtpControlSocket::SendKeepAliveCommand()
  4599. {
  4600. m_bKeepAliveActive=TRUE;
  4601. //Choose a random command from the list
  4602. char commands[4][7]={"PWD","REST 0","TYPE A","TYPE I"};
  4603. int choice=(rand()*4)/(RAND_MAX+1);
  4604. Send(commands[choice]);
  4605. }
  4606. void CFtpControlSocket::MakeDir(const CServerPath &path)
  4607. {
  4608. //Directory creation works like this:
  4609. //Find existing parent and create subdirs one by one
  4610. LogMessage(__FILE__, __LINE__, this, FZ_LOG_DEBUG, _T("MakeDir(\"%s\") OpMode=%d OpState=%d"), path.GetPath(), m_Operation.nOpMode, m_Operation.nOpState);
  4611. if (m_Operation.nOpState == MKD_INIT)
  4612. {
  4613. ASSERT(!path.IsEmpty());
  4614. ASSERT(m_Operation.nOpMode==CSMODE_NONE);
  4615. ASSERT(!m_Operation.pData);
  4616. m_Operation.nOpMode = CSMODE_MKDIR;
  4617. if (!Send("CWD "+path.GetParent().GetPath()))
  4618. return;
  4619. CMakeDirData *data = new CMakeDirData;
  4620. data->path = path;
  4621. data->Current = path.GetParent();
  4622. data->Segments.push_front(path.GetLastSegment());
  4623. m_Operation.pData = data;
  4624. m_Operation.nOpState = MKD_FINDPARENT;
  4625. }
  4626. else if (m_Operation.nOpState==MKD_FINDPARENT)
  4627. {
  4628. ASSERT(m_Operation.nOpMode==CSMODE_MKDIR);
  4629. ASSERT(path.IsEmpty());
  4630. ASSERT(m_Operation.pData);
  4631. CMakeDirData *pData=(CMakeDirData *)m_Operation.pData;
  4632. int res=GetReplyCode();
  4633. if (res==2 || res==3)
  4634. {
  4635. m_pOwner->SetCurrentPath(pData->Current);
  4636. m_Operation.nOpState=MKD_MAKESUBDIRS;
  4637. pData->Current.AddSubdir(pData->Segments.front());
  4638. CString Segment=pData->Segments.front();
  4639. pData->Segments.pop_front();
  4640. if (Send( _T("MKD ") + Segment))
  4641. m_Operation.nOpState = MKD_CHANGETOSUBDIR;
  4642. else
  4643. return;
  4644. }
  4645. else
  4646. {
  4647. if (!pData->Current.HasParent())
  4648. ResetOperation(FZ_REPLY_ERROR);
  4649. else
  4650. {
  4651. pData->Segments.push_front(pData->Current.GetLastSegment());
  4652. pData->Current=pData->Current.GetParent();
  4653. if (!Send("CWD "+pData->Current.GetPath()))
  4654. return;
  4655. }
  4656. }
  4657. }
  4658. else if (m_Operation.nOpState==MKD_MAKESUBDIRS)
  4659. {
  4660. int res=GetReplyCode();
  4661. if (res==2 || res==3)
  4662. { //Create dir entry in parent dir
  4663. CMakeDirData *pData=(CMakeDirData *)m_Operation.pData;
  4664. ASSERT(!pData->Segments.empty());
  4665. m_pOwner->SetCurrentPath(pData->Current);
  4666. pData->Current.AddSubdir(pData->Segments.front());
  4667. CString Segment=pData->Segments.front();
  4668. pData->Segments.pop_front();
  4669. if (Send( _T("MKD ") + Segment))
  4670. m_Operation.nOpState=MKD_CHANGETOSUBDIR;
  4671. else
  4672. return;
  4673. }
  4674. else
  4675. ResetOperation(FZ_REPLY_ERROR);
  4676. }
  4677. else if (m_Operation.nOpState==MKD_CHANGETOSUBDIR)
  4678. {
  4679. CMakeDirData *pData=(CMakeDirData *)m_Operation.pData;
  4680. int res=GetReplyCode();
  4681. if (res==2 || res==3 || //Creation successful
  4682. GetReply() == _T("550 Directory already exists"))//Creation was successful, although someone else did the work for us
  4683. { //Create dir entry in parent dir
  4684. CServerPath path2=pData->Current;
  4685. if (path2.HasParent())
  4686. {
  4687. CString name=path2.GetLastSegment();
  4688. path2=path2.GetParent();
  4689. #ifndef MPEXT_NO_CACHE
  4690. CDirectoryCache cache;
  4691. t_directory dir;
  4692. cache.Lock();
  4693. BOOL bCached = TRUE;
  4694. BOOL res = cache.Lookup(path2, m_CurrentServer,dir);
  4695. if (!res)
  4696. bCached = FALSE;
  4697. #else
  4698. t_directory dir;
  4699. BOOL res = FALSE;
  4700. #endif
  4701. if (!res && m_pDirectoryListing)
  4702. {
  4703. if (m_pDirectoryListing->path == path2)
  4704. {
  4705. dir = *m_pDirectoryListing;
  4706. res = TRUE;
  4707. }
  4708. }
  4709. t_directory WorkingDir;
  4710. BOOL bFound = m_pOwner->GetWorkingDir(&WorkingDir);
  4711. if (!res && bFound)
  4712. if (WorkingDir.path == path2)
  4713. {
  4714. dir = WorkingDir;
  4715. res = TRUE;
  4716. }
  4717. if (!res)
  4718. {
  4719. dir.path = path2;
  4720. dir.server = m_CurrentServer;
  4721. }
  4722. int i;
  4723. for (i=0; i<dir.num; i++)
  4724. if (dir.direntry[i].name==name)
  4725. {
  4726. LogMessage(__FILE__, __LINE__, this,FZ_LOG_WARNING, _T("Dir already exists in cache!"));
  4727. break;
  4728. }
  4729. if (i==dir.num)
  4730. {
  4731. t_directory::t_direntry *entries=new t_directory::t_direntry[dir.num+1];
  4732. for (i=0;i<dir.num;i++)
  4733. entries[i]=dir.direntry[i];
  4734. entries[i].name=name;
  4735. entries[i].lName=name;
  4736. entries[i].lName.MakeLower();
  4737. entries[i].dir=TRUE;
  4738. entries[i].date.hasdate=FALSE;
  4739. entries[i].size=-1;
  4740. entries[i].bUnsure=FALSE;
  4741. delete [] dir.direntry;
  4742. dir.direntry=entries;
  4743. dir.num++;
  4744. #ifndef MPEXT_NO_CACHE
  4745. cache.Store(dir, bCached);
  4746. #endif
  4747. BOOL updated = FALSE;
  4748. if (m_pDirectoryListing && m_pDirectoryListing->path == dir.path)
  4749. {
  4750. updated = TRUE;
  4751. SetDirectoryListing(&dir, bFound && WorkingDir.path == dir.path);
  4752. }
  4753. if (!updated)
  4754. if (bFound && WorkingDir.path == dir.path)
  4755. {
  4756. updated = TRUE;
  4757. m_pOwner->SetWorkingDir(&dir);
  4758. }
  4759. }
  4760. #ifndef MPEXT_NO_CACHE
  4761. cache.Unlock();
  4762. #endif
  4763. }
  4764. }
  4765. //Continue operation even if MKD failed, maybe another thread did create this directory for us
  4766. if (pData->Segments.empty())
  4767. ResetOperation(FZ_REPLY_OK);
  4768. else
  4769. {
  4770. if (Send( _T("CWD ") + pData->Current.GetPath()))
  4771. m_Operation.nOpState=MKD_MAKESUBDIRS;
  4772. else
  4773. return;
  4774. }
  4775. }
  4776. else
  4777. ASSERT(FALSE);
  4778. }
  4779. void CFtpControlSocket::Rename(CString oldName, CString newName, const CServerPath &path, const CServerPath &newPath)
  4780. {
  4781. LogMessage(__FILE__, __LINE__, this,FZ_LOG_DEBUG, _T("Rename(\"%s\", \"%s\", \"%s\") OpMode=%d OpState=%d"), oldName, newName, path.GetPath(),
  4782. m_Operation.nOpMode, m_Operation.nOpState);
  4783. class CRenameData : public CFtpControlSocket::t_operation::COpData
  4784. {
  4785. public:
  4786. CRenameData() {}
  4787. virtual ~CRenameData() {}
  4788. CString oldName, newName;
  4789. CServerPath path;
  4790. CServerPath newPath;
  4791. };
  4792. if (oldName != "")
  4793. {
  4794. ASSERT(newName != "");
  4795. ASSERT(!path.IsEmpty());
  4796. ASSERT(m_Operation.nOpMode == CSMODE_NONE);
  4797. ASSERT(m_Operation.nOpState == -1);
  4798. ASSERT(!m_Operation.pData);
  4799. m_Operation.nOpMode = CSMODE_RENAME;
  4800. if (!Send("RNFR " + path.FormatFilename(oldName)))
  4801. return;
  4802. CRenameData *data = new CRenameData;
  4803. data->oldName = oldName;
  4804. data->newName = newName;
  4805. data->path = path;
  4806. data->newPath = newPath;
  4807. m_Operation.pData = data;
  4808. }
  4809. else
  4810. {
  4811. ASSERT(oldName == "");
  4812. ASSERT(path.IsEmpty());
  4813. ASSERT(m_Operation.nOpMode == CSMODE_RENAME);
  4814. ASSERT(m_Operation.pData);
  4815. CRenameData *pData = reinterpret_cast<CRenameData *>(m_Operation.pData);
  4816. if (m_Operation.nOpState == -1)
  4817. {
  4818. int res = GetReplyCode();
  4819. if (res == 2 || res == 3)
  4820. {
  4821. m_Operation.nOpState++;
  4822. if (pData->newPath.IsEmpty())
  4823. {
  4824. if (!Send("RNTO " + pData->path.FormatFilename(((CRenameData *)m_Operation.pData)->newName)))
  4825. return;
  4826. }
  4827. else
  4828. if (!Send("RNTO " + pData->newPath.FormatFilename(((CRenameData *)m_Operation.pData)->newName)))
  4829. return;
  4830. }
  4831. else
  4832. ResetOperation(FZ_REPLY_ERROR);
  4833. }
  4834. else
  4835. {
  4836. int res = GetReplyCode();
  4837. if (res == 2 || res == 3)
  4838. { //Rename entry in cached directory
  4839. CRenameData *pData = reinterpret_cast<CRenameData *>(m_Operation.pData);
  4840. #ifndef MPEXT_NO_CACHE
  4841. CDirectoryCache cache;
  4842. cache.Lock();
  4843. //Rename all references to the directory in the cache
  4844. if (pData->newPath.IsEmpty())
  4845. cache.Rename(pData->path, pData->oldName, pData->newName, m_CurrentServer);
  4846. BOOL bCached = TRUE;
  4847. t_directory dir;
  4848. BOOL res = cache.Lookup(pData->path, m_CurrentServer, dir);
  4849. if (!res)
  4850. bCached = FALSE;
  4851. #else
  4852. t_directory dir;
  4853. BOOL res = FALSE;
  4854. #endif
  4855. if (!res && m_pDirectoryListing)
  4856. {
  4857. if (m_pDirectoryListing->path == pData->path)
  4858. {
  4859. dir = *m_pDirectoryListing;
  4860. res = TRUE;
  4861. }
  4862. }
  4863. t_directory WorkingDir;
  4864. BOOL bFound = m_pOwner->GetWorkingDir(&WorkingDir);
  4865. if (!res && bFound)
  4866. if (WorkingDir.path == pData->path)
  4867. {
  4868. dir = WorkingDir;
  4869. res = TRUE;
  4870. }
  4871. if (res)
  4872. {
  4873. for (int i=0; i<dir.num; i++)
  4874. if (dir.direntry[i].name == pData->oldName)
  4875. {
  4876. if (pData->newPath.IsEmpty())
  4877. {
  4878. dir.direntry[i].name = pData->newName;
  4879. dir.direntry[i].lName = pData->newName;
  4880. dir.direntry[i].lName.MakeLower();
  4881. #ifndef MPEXT_NO_CACHE
  4882. CDirectoryCache cache;
  4883. cache.Store(dir, bCached);
  4884. #endif
  4885. BOOL updated = FALSE;
  4886. if (m_pDirectoryListing && m_pDirectoryListing->path == dir.path)
  4887. {
  4888. updated = TRUE;
  4889. SetDirectoryListing(&dir, WorkingDir.path == dir.path);
  4890. }
  4891. if (!updated)
  4892. if (WorkingDir.path == dir.path)
  4893. {
  4894. updated = TRUE;
  4895. m_pOwner->SetWorkingDir(&dir);
  4896. }
  4897. }
  4898. else
  4899. {
  4900. t_directory::t_direntry oldentry = dir.direntry[i];
  4901. for (int j = i+1; j < dir.num; j++)
  4902. {
  4903. dir.direntry[j-1] = dir.direntry[j];
  4904. }
  4905. dir.num--;
  4906. #ifndef MPEXT_NO_CACHE
  4907. cache.Store(dir, bCached);
  4908. // If directory, delete old directory from cache
  4909. t_directory olddir;
  4910. res = cache.Lookup(pData->path, pData->oldName, m_CurrentServer, olddir);
  4911. if (res)
  4912. {
  4913. cache.Purge(olddir.path, m_CurrentServer);
  4914. t_directory newdir;
  4915. if (cache.Lookup(dir.path, m_CurrentServer, newdir))
  4916. dir = newdir;
  4917. }
  4918. #endif
  4919. BOOL updated = FALSE;
  4920. if (m_pDirectoryListing && m_pDirectoryListing->path == dir.path)
  4921. {
  4922. updated = TRUE;
  4923. SetDirectoryListing(&dir, WorkingDir.path == dir.path);
  4924. }
  4925. if (!updated)
  4926. if (WorkingDir.path == dir.path)
  4927. {
  4928. updated = TRUE;
  4929. m_pOwner->SetWorkingDir(&dir);
  4930. }
  4931. #ifndef MPEXT_NO_CACHE
  4932. BOOL bCached = TRUE;
  4933. BOOL res = cache.Lookup(pData->newPath, m_CurrentServer, dir);
  4934. if (!res)
  4935. bCached = FALSE;
  4936. #else
  4937. BOOL res = FALSE;
  4938. #endif
  4939. if (!res && m_pDirectoryListing)
  4940. {
  4941. if (m_pDirectoryListing->path == pData->newPath)
  4942. {
  4943. dir = *m_pDirectoryListing;
  4944. res = TRUE;
  4945. }
  4946. }
  4947. t_directory WorkingDir;
  4948. BOOL bFound = m_pOwner->GetWorkingDir(&WorkingDir);
  4949. if (!res && bFound)
  4950. if (WorkingDir.path == pData->newPath)
  4951. {
  4952. dir = WorkingDir;
  4953. res = TRUE;
  4954. }
  4955. if (res)
  4956. {
  4957. t_directory::t_direntry *direntry = new t_directory::t_direntry[dir.num + 1];
  4958. for (int i = 0; i < dir.num; i++)
  4959. direntry[i] = dir.direntry[i];
  4960. direntry[dir.num] = oldentry;
  4961. direntry[dir.num].name = pData->newName;
  4962. direntry[dir.num].lName = pData->newName;
  4963. direntry[dir.num].lName.MakeLower();
  4964. dir.num++;
  4965. delete [] dir.direntry;
  4966. dir.direntry = direntry;
  4967. #ifndef MPEXT_NO_CACHE
  4968. cache.Store(dir, bCached);
  4969. #endif
  4970. BOOL updated = FALSE;
  4971. if (m_pDirectoryListing && m_pDirectoryListing->path == dir.path)
  4972. {
  4973. updated = TRUE;
  4974. SetDirectoryListing(&dir, bFound && WorkingDir.path == dir.path);
  4975. }
  4976. if (!updated)
  4977. if (bFound && WorkingDir.path == dir.path)
  4978. {
  4979. updated = TRUE;
  4980. m_pOwner->SetWorkingDir(&dir);
  4981. }
  4982. }
  4983. }
  4984. break;
  4985. }
  4986. }
  4987. #ifndef MPEXT_NO_CACHE
  4988. cache.Unlock();
  4989. #endif
  4990. ResetOperation(FZ_REPLY_OK);
  4991. }
  4992. else
  4993. ResetOperation(FZ_REPLY_ERROR);
  4994. }
  4995. }
  4996. }
  4997. #ifndef MPEXT_NO_SSL
  4998. void CFtpControlSocket::SetVerifyCertResult(int nResult, t_SslCertData *pData)
  4999. {
  5000. ASSERT(pData);
  5001. if (!m_pSslLayer)
  5002. return;
  5003. if (!m_Operation.nOpMode == CSMODE_CONNECT)
  5004. return;
  5005. m_bCheckForTimeout = TRUE;
  5006. m_pSslLayer->SetNotifyReply(pData->priv_data, SSL_VERIFY_CERT, nResult);
  5007. m_LastRecvTime = CTime::GetCurrentTime();
  5008. }
  5009. #endif
  5010. void CFtpControlSocket::OnTimer()
  5011. {
  5012. CheckForTimeout();
  5013. ResumeTransfer();
  5014. if (COptions::GetOptionVal(OPTION_KEEPALIVE))
  5015. {
  5016. if (!m_pOwner->IsBusy() && m_pOwner->IsConnected() && !m_bKeepAliveActive)
  5017. {
  5018. //Getting intervals for the Keep Alive feature
  5019. int low=COptions::GetOptionVal(OPTION_INTERVALLOW);
  5020. int diff=COptions::GetOptionVal(OPTION_INTERVALHIGH)-low;
  5021. //Choose a new delay
  5022. static int delay=low+(rand()*diff)/RAND_MAX;
  5023. CTimeSpan span=CTime::GetCurrentTime()-m_LastSendTime;
  5024. if (span.GetTotalSeconds()>=delay)
  5025. SendKeepAliveCommand();
  5026. }
  5027. }
  5028. }
  5029. BOOL CFtpControlSocket::IsReady()
  5030. {
  5031. return !m_bKeepAliveActive;
  5032. }
  5033. void CFtpControlSocket::Chmod(CString filename, const CServerPath &path, int nValue)
  5034. {
  5035. m_Operation.nOpMode=CSMODE_CHMOD;
  5036. CString str;
  5037. str.Format( _T("SITE CHMOD %03d %s"), nValue, path.FormatFilename(filename));
  5038. Send(str);
  5039. }
  5040. void CFtpControlSocket::SetAsyncRequestResult(int nAction, CAsyncRequestData *pData)
  5041. {
  5042. switch (pData->nRequestType)
  5043. {
  5044. case FZ_ASYNCREQUEST_OVERWRITE:
  5045. SetFileExistsAction(nAction, (COverwriteRequestData *)pData);
  5046. break;
  5047. #ifndef MPEXT_NO_SSL
  5048. case FZ_ASYNCREQUEST_VERIFYCERT:
  5049. SetVerifyCertResult(nAction, ((CVerifyCertRequestData *)pData)->pCertData );
  5050. break;
  5051. #endif
  5052. #ifndef MPEXT_NO_GSS
  5053. case FZ_ASYNCREQUEST_GSS_AUTHFAILED:
  5054. if (m_Operation.nOpMode!=CSMODE_CONNECT || m_Operation.nOpState!=CONNECT_GSS_FAILED)
  5055. break;
  5056. if (!m_RecvBuffer.empty() && m_RecvBuffer.front() != "")
  5057. {
  5058. DoClose();
  5059. break;
  5060. }
  5061. if (!nAction)
  5062. {
  5063. DoClose(FZ_REPLY_CRITICALERROR|FZ_REPLY_CANCEL);
  5064. ShowStatus(IDS_ERRORMSG_INTERRUPTED,1);
  5065. break;
  5066. }
  5067. m_Operation.nOpState=-1;
  5068. LogOnToServer(TRUE);
  5069. break;
  5070. case FZ_ASYNCREQUEST_GSS_NEEDPASS:
  5071. if (m_Operation.nOpMode!=CSMODE_CONNECT ||
  5072. m_Operation.nOpState != CONNECT_GSS_NEEDPASS)
  5073. break;
  5074. if (!m_RecvBuffer.empty() && m_RecvBuffer.front() != "")
  5075. {
  5076. DoClose();
  5077. break;
  5078. }
  5079. if (!nAction)
  5080. {
  5081. DoClose(FZ_REPLY_CRITICALERROR|FZ_REPLY_CANCEL);
  5082. ShowStatus(IDS_ERRORMSG_INTERRUPTED,1);
  5083. break;
  5084. }
  5085. else
  5086. {
  5087. m_CurrentServer.pass=((CGssNeedPassRequestData *)pData)->pass;
  5088. m_Operation.nOpState=((CGssNeedPassRequestData *)pData)->nOldOpState;
  5089. LogOnToServer(TRUE);
  5090. }
  5091. break;
  5092. case FZ_ASYNCREQUEST_GSS_NEEDUSER:
  5093. if (m_Operation.nOpMode != CSMODE_CONNECT ||
  5094. m_Operation.nOpState != CONNECT_GSS_NEEDUSER)
  5095. break;
  5096. if (!m_RecvBuffer.empty() && m_RecvBuffer.front() != "")
  5097. {
  5098. DoClose();
  5099. break;
  5100. }
  5101. if (!nAction)
  5102. {
  5103. DoClose(FZ_REPLY_CRITICALERROR | FZ_REPLY_CANCEL);
  5104. ShowStatus(IDS_ERRORMSG_INTERRUPTED, 1);
  5105. break;
  5106. }
  5107. else
  5108. {
  5109. m_CurrentServer.user = ((CGssNeedUserRequestData *)pData)->user;
  5110. m_Operation.nOpState=((CGssNeedUserRequestData *)pData)->nOldOpState;
  5111. LogOnToServer(TRUE);
  5112. }
  5113. break;
  5114. #endif
  5115. default:
  5116. LogMessage(__FILE__, __LINE__, this,FZ_LOG_WARNING, _T("Unknown request reply %d"), pData->nRequestType);
  5117. break;
  5118. }
  5119. }
  5120. int CFtpControlSocket::OnLayerCallback(std::list<t_callbackMsg>& callbacks)
  5121. {
  5122. for (std::list<t_callbackMsg>::iterator iter = callbacks.begin(); iter != callbacks.end(); iter++)
  5123. {
  5124. if (iter->nType == LAYERCALLBACK_STATECHANGE)
  5125. {
  5126. #ifndef MPEXT_NO_SSL
  5127. if (iter->pLayer == m_pSslLayer)
  5128. {
  5129. LogMessage(__FILE__, __LINE__, this, FZ_LOG_INFO, _T("m_pSslLayer changed state from %d to %d"), iter->nParam2, iter->nParam1);
  5130. delete [] iter->str;
  5131. continue;
  5132. }
  5133. #endif
  5134. }
  5135. else if (iter->nType == LAYERCALLBACK_LAYERSPECIFIC)
  5136. {
  5137. #ifndef MPEXT_NO_SSL
  5138. if (iter->pLayer == m_pSslLayer)
  5139. {
  5140. USES_CONVERSION;
  5141. switch (iter->nParam1)
  5142. {
  5143. case SSL_INFO:
  5144. switch (iter->nParam2)
  5145. {
  5146. case SSL_INFO_ESTABLISHED:
  5147. ShowStatus(IDS_STATUSMSG_SSLESTABLISHED, 0);
  5148. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SECURESERVER, 1), 0);
  5149. if (m_Operation.nOpState == CONNECT_SSL_WAITDONE)
  5150. {
  5151. LogOnToServer();
  5152. }
  5153. break;
  5154. }
  5155. break;
  5156. case SSL_FAILURE:
  5157. switch (iter->nParam2)
  5158. {
  5159. case SSL_FAILURE_UNKNOWN:
  5160. ShowStatus(IDS_ERRORMSG_UNKNOWNSSLERROR, 1);
  5161. break;
  5162. case SSL_FAILURE_ESTABLISH:
  5163. ShowStatus(IDS_ERRORMSG_CANTESTABLISHSSLCONNECTION, 1);
  5164. break;
  5165. case SSL_FAILURE_LOADDLLS:
  5166. ShowStatus(IDS_ERRORMSG_CANTLOADSSLDLLS, 1);
  5167. break;
  5168. case SSL_FAILURE_INITSSL:
  5169. ShowStatus(IDS_ERRORMSG_CANTINITSSL, 1);
  5170. break;
  5171. case SSL_FAILURE_VERIFYCERT:
  5172. ShowStatus(IDS_ERRORMSG_SSLCERTIFICATEERROR, 1);
  5173. break;
  5174. case SSL_FAILURE_CERTREJECTED:
  5175. ShowStatus(IDS_ERRORMSG_CERTREJECTED, 1);
  5176. m_bDidRejectCertificate = TRUE;
  5177. break;
  5178. }
  5179. TriggerEvent(FD_CLOSE);
  5180. break;
  5181. case SSL_VERBOSE_INFO:
  5182. LogMessageRaw(FZ_LOG_INFO, A2CT(iter->str));
  5183. break;
  5184. case SSL_VERBOSE_WARNING:
  5185. LogMessageRaw(FZ_LOG_WARNING, A2CT(iter->str));
  5186. break;
  5187. case SSL_VERIFY_CERT:
  5188. t_SslCertData *pData = new t_SslCertData;
  5189. if (m_pSslLayer->GetPeerCertificateData(*pData))
  5190. {
  5191. CVerifyCertRequestData *pRequestData = new CVerifyCertRequestData;
  5192. pRequestData->nRequestID=m_pOwner->GetNextAsyncRequestID();
  5193. pRequestData->pCertData = pData;
  5194. m_bCheckForTimeout = FALSE;
  5195. if (!PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_ASYNCREQUEST, FZ_ASYNCREQUEST_VERIFYCERT), (LPARAM)pRequestData))
  5196. {
  5197. delete pRequestData->pCertData;
  5198. delete pRequestData;
  5199. ResetOperation(FZ_REPLY_ERROR);
  5200. }
  5201. delete [] iter->str;
  5202. continue;
  5203. }
  5204. else
  5205. {
  5206. delete pData;
  5207. delete [] iter->str;
  5208. ResetOperation(FZ_REPLY_ERROR);
  5209. delete [] iter->str;
  5210. continue;
  5211. }
  5212. break;
  5213. }
  5214. delete [] iter->str;
  5215. continue;
  5216. }
  5217. #ifndef MPEXT_NO_GSS
  5218. else
  5219. #endif
  5220. #endif
  5221. #ifndef MPEXT_NO_GSS
  5222. if (iter->pLayer == m_pGssLayer)
  5223. {
  5224. if (iter->nParam1 == GSS_AUTHCOMPLETE ||
  5225. iter->nParam1 == GSS_AUTHFAILED)
  5226. {
  5227. LogOnToServer(TRUE);
  5228. delete [] iter->str;
  5229. continue;
  5230. }
  5231. }
  5232. #endif
  5233. }
  5234. std::list<t_callbackMsg> tmp;
  5235. tmp.push_back(*iter);
  5236. CControlSocket::OnLayerCallback(tmp);
  5237. }
  5238. return 0;
  5239. }
  5240. BOOL CFtpControlSocket::ParsePwdReply(CString& rawpwd)
  5241. {
  5242. CListData *pData = static_cast<CListData *>(m_Operation.pData);
  5243. ASSERT(pData);
  5244. int pos1 = rawpwd.Find('"');
  5245. int pos2 = rawpwd.ReverseFind('"');
  5246. if (pos1 == -1 || pos2 == -1 || pos1 >= pos2)
  5247. {
  5248. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("No quoted path found, try using first token as path"));
  5249. pos1 = rawpwd.Find(' ');
  5250. if (pos1 != -1)
  5251. {
  5252. pos2 = rawpwd.Find(' ', pos1 + 1);
  5253. if (pos2 == -1)
  5254. pos2 = rawpwd.GetLength();
  5255. }
  5256. if (pos1 == -1)
  5257. {
  5258. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Can't parse path!"));
  5259. ResetOperation(FZ_REPLY_ERROR);
  5260. return FALSE;
  5261. }
  5262. }
  5263. rawpwd = rawpwd.Mid(pos1 + 1, pos2 - pos1 - 1);
  5264. CServerPath realPath = m_pOwner->GetCurrentPath();
  5265. realPath.SetServer(m_CurrentServer);
  5266. if (!realPath.SetPath(rawpwd))
  5267. {
  5268. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Can't parse path!"));
  5269. ResetOperation(FZ_REPLY_ERROR);
  5270. return FALSE;
  5271. }
  5272. m_pOwner->SetCurrentPath(realPath);
  5273. return TRUE;
  5274. }
  5275. void CFtpControlSocket::DiscardLine(CStringA line)
  5276. {
  5277. if (m_Operation.nOpMode == CSMODE_CONNECT && m_Operation.nOpState == CONNECT_FEAT)
  5278. {
  5279. line.MakeUpper();
  5280. #ifndef MPEXT_NO_ZLIB
  5281. if (line == _T(" MODE Z") || line.Left(8) == _T(" MODE Z "))
  5282. m_zlibSupported = true;
  5283. else
  5284. #endif
  5285. if (line == _T(" UTF8") && m_CurrentServer.nUTF8 != 2)
  5286. m_bAnnouncesUTF8 = true;
  5287. else if (line == _T(" CLNT") || line.Left(6) == _T(" CLNT "))
  5288. m_hasClntCmd = true;
  5289. }
  5290. }
  5291. bool CFtpControlSocket::NeedModeCommand()
  5292. {
  5293. #ifdef MPEXT_NO_ZLIB
  5294. return false;
  5295. #else
  5296. bool useZlib;
  5297. if (m_Operation.nOpMode == CSMODE_LIST || (m_Operation.nOpMode == CSMODE_TRANSFER && m_Operation.nOpMode <= FILETRANSFER_TYPE))
  5298. useZlib = COptions::GetOptionVal(OPTION_MODEZ_USE) != 0;
  5299. else
  5300. useZlib = COptions::GetOptionVal(OPTION_MODEZ_USE) > 1;
  5301. if (!m_useZlib && !m_zlibSupported)
  5302. return false;
  5303. return m_useZlib != useZlib;
  5304. #endif
  5305. }
  5306. bool CFtpControlSocket::NeedOptsCommand()
  5307. {
  5308. #ifndef MPEXT_NO_ZLIB
  5309. if (!m_useZlib)
  5310. #endif
  5311. return false;
  5312. #ifndef MPEXT_NO_ZLIB
  5313. return m_zlibLevel != COptions::GetOptionVal(OPTION_MODEZ_LEVEL);
  5314. #endif
  5315. }
  5316. CString CFtpControlSocket::GetReply()
  5317. {
  5318. if (m_RecvBuffer.empty())
  5319. return "";
  5320. USES_CONVERSION;
  5321. if (m_bUTF8)
  5322. {
  5323. // convert from UTF-8 to ANSI
  5324. LPCSTR utf8 = (LPCSTR)m_RecvBuffer.front();
  5325. if (!utf8_valid((const unsigned char*)utf8, strlen(utf8)))
  5326. {
  5327. if (m_CurrentServer.nUTF8 != 1)
  5328. {
  5329. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Server does not send proper UTF-8, falling back to local charset"));
  5330. m_bUTF8 = false;
  5331. }
  5332. return A2CT(m_RecvBuffer.front());
  5333. }
  5334. // convert from UTF-8 to ANSI
  5335. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
  5336. if (!len)
  5337. {
  5338. m_RecvBuffer.pop_front();
  5339. if (m_RecvBuffer.empty())
  5340. m_RecvBuffer.push_back("");
  5341. return "";
  5342. }
  5343. else
  5344. {
  5345. LPWSTR p1 = new WCHAR[len + 1];
  5346. MultiByteToWideChar(CP_UTF8, 0, utf8, -1 , (LPWSTR)p1, len + 1);
  5347. CString reply = W2CT(p1);
  5348. delete [] p1;
  5349. return reply;
  5350. }
  5351. }
  5352. else
  5353. return A2CT(m_RecvBuffer.front());
  5354. }
  5355. void CFtpControlSocket::OnSend(int nErrorCode)
  5356. {
  5357. if (!m_sendBufferLen || !m_sendBuffer || m_awaitsReply)
  5358. return;
  5359. int res = CAsyncSocketEx::Send(m_sendBuffer, m_sendBufferLen);
  5360. if (res == -1)
  5361. {
  5362. if (GetLastError() != WSAEWOULDBLOCK)
  5363. {
  5364. ShowStatus(IDS_ERRORMSG_CANTSENDCOMMAND, 1);
  5365. DoClose();
  5366. }
  5367. return;
  5368. }
  5369. if (!res)
  5370. {
  5371. ShowStatus(IDS_ERRORMSG_CANTSENDCOMMAND, 1);
  5372. DoClose();
  5373. }
  5374. m_awaitsReply = true;
  5375. m_LastSendTime = CTime::GetCurrentTime();
  5376. PostMessage(m_pOwner->m_hOwnerWnd, m_pOwner->m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_SOCKETSTATUS, FZ_SOCKETSTATUS_SEND), 0);
  5377. if (res == m_sendBufferLen)
  5378. {
  5379. delete [] m_sendBuffer;
  5380. m_sendBuffer = 0;
  5381. m_sendBufferLen = 0;
  5382. }
  5383. else
  5384. {
  5385. char* tmp = new char[m_sendBufferLen - res];
  5386. memcpy(tmp, m_sendBuffer + res, m_sendBufferLen - res);
  5387. delete [] m_sendBuffer;
  5388. m_sendBuffer = tmp;
  5389. m_sendBufferLen -= res;
  5390. }
  5391. }
  5392. bool CFtpControlSocket::IsMisleadingListResponse()
  5393. {
  5394. // Some servers are broken. Instead of an empty listing, some MVS servers
  5395. // for example they return something "550 no members found"
  5396. // Other servers return "550 No files found."
  5397. CString retmsg = GetReply();
  5398. if (!retmsg.CompareNoCase(_T("550 No members found.")))
  5399. return true;
  5400. if (!retmsg.CompareNoCase(_T("550 No data sets found.")))
  5401. return true;
  5402. if (!retmsg.CompareNoCase(_T("550 No files found.")))
  5403. return true;
  5404. return false;
  5405. }