1
0

FtpControlSocket.cpp 184 KB

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