SessionData.cpp 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "SessionData.h"
  5. #include "Common.h"
  6. #include "Exceptions.h"
  7. #include "FileBuffer.h"
  8. #include "CoreMain.h"
  9. #include "TextsCore.h"
  10. #include "PuttyIntf.h"
  11. #include "RemoteFiles.h"
  12. #include "SFTPFileSystem.h"
  13. #include "S3FileSystem.h"
  14. #include <Soap.EncdDecd.hpp>
  15. #include <StrUtils.hpp>
  16. #include <XMLDoc.hpp>
  17. #include <StrUtils.hpp>
  18. #include <algorithm>
  19. //---------------------------------------------------------------------------
  20. #pragma package(smart_init)
  21. //---------------------------------------------------------------------------
  22. #define SET_SESSION_PROPERTY_FROM(PROPERTY, FROM) \
  23. if (F##PROPERTY != FROM) { F##PROPERTY = FROM; Modify(); }
  24. //---------------------------------------------------------------------------
  25. #define SET_SESSION_PROPERTY(PROPERTY) \
  26. SET_SESSION_PROPERTY_FROM(PROPERTY, value)
  27. //---------------------------------------------------------------------------
  28. const wchar_t * PingTypeNames = L"Off;Null;Dummy";
  29. const wchar_t * ProxyMethodNames = L"None;SOCKS4;SOCKS5;HTTP;Telnet;Cmd";
  30. const wchar_t * DefaultName = L"Default Settings";
  31. const UnicodeString CipherNames[CIPHER_COUNT] = {L"WARN", L"3des", L"blowfish", L"aes", L"des", L"arcfour", L"chacha20"};
  32. const UnicodeString KexNames[KEX_COUNT] = {L"WARN", L"dh-group1-sha1", L"dh-group14-sha1", L"dh-gex-sha1", L"rsa", L"ecdh"};
  33. const UnicodeString HostKeyNames[HOSTKEY_COUNT] = {L"WARN", L"rsa", L"dsa", L"ecdsa", L"ed25519"};
  34. const UnicodeString GssLibNames[GSSLIB_COUNT] = {L"gssapi32", L"sspi", L"custom"};
  35. const wchar_t SshProtList[][10] = {L"1", L"1>2", L"2>1", L"2"};
  36. // Update also order in Ssh2CipherList()
  37. const TCipher DefaultCipherList[CIPHER_COUNT] =
  38. { cipAES, cipChaCha20, cipBlowfish, cip3DES, cipWarn, cipArcfour, cipDES };
  39. // Update also order in SshKexList()
  40. const TKex DefaultKexList[KEX_COUNT] =
  41. { kexECDH, kexDHGEx, kexDHGroup14, kexRSA, kexWarn, kexDHGroup1 };
  42. const THostKey DefaultHostKeyList[HOSTKEY_COUNT] =
  43. { hkED25519, hkECDSA, hkRSA, hkDSA, hkWarn };
  44. const TGssLib DefaultGssLibList[GSSLIB_COUNT] =
  45. { gssGssApi32, gssSspi, gssCustom };
  46. const wchar_t FSProtocolNames[FSPROTOCOL_COUNT][16] = { L"SCP", L"SFTP (SCP)", L"SFTP", L"", L"", L"FTP", L"WebDAV", L"S3" };
  47. const int SshPortNumber = 22;
  48. const int FtpPortNumber = 21;
  49. const int FtpsImplicitPortNumber = 990;
  50. const int HTTPPortNumber = 80;
  51. const int HTTPSPortNumber = 443;
  52. const int TelnetPortNumber = 23;
  53. const int DefaultSendBuf = 262144;
  54. const int ProxyPortNumber = 80;
  55. const UnicodeString AnonymousUserName(L"anonymous");
  56. const UnicodeString AnonymousPassword(L"[email protected]");
  57. const UnicodeString PuttySshProtocol(L"ssh");
  58. const UnicodeString PuttyTelnetProtocol(L"telnet");
  59. const UnicodeString SftpProtocol(L"sftp");
  60. const UnicodeString ScpProtocol(L"scp");
  61. const UnicodeString FtpProtocol(L"ftp");
  62. const UnicodeString FtpsProtocol(L"ftps");
  63. const UnicodeString FtpesProtocol(L"ftpes");
  64. const UnicodeString WebDAVProtocol(L"dav");
  65. const UnicodeString WebDAVSProtocol(L"davs");
  66. const UnicodeString S3Protocol(L"s3");
  67. const UnicodeString SshProtocol(L"ssh");
  68. const UnicodeString WinSCPProtocolPrefix(L"winscp-");
  69. const wchar_t UrlParamSeparator = L';';
  70. const wchar_t UrlParamValueSeparator = L'=';
  71. const UnicodeString UrlHostKeyParamName(L"fingerprint");
  72. const UnicodeString UrlSaveParamName(L"save");
  73. const UnicodeString UrlRawSettingsParamNamePrefix(L"x-");
  74. const UnicodeString PassphraseOption(L"passphrase");
  75. const UnicodeString RawSettingsOption(L"rawsettings");
  76. const UnicodeString S3HostName(S3LibDefaultHostName());
  77. //---------------------------------------------------------------------
  78. TDateTime __fastcall SecToDateTime(int Sec)
  79. {
  80. return TDateTime(double(Sec) / SecsPerDay);
  81. }
  82. //--- TSessionData ----------------------------------------------------
  83. __fastcall TSessionData::TSessionData(UnicodeString aName):
  84. TNamedObject(aName)
  85. {
  86. Default();
  87. FModified = true;
  88. }
  89. //---------------------------------------------------------------------
  90. _fastcall TSessionData::~TSessionData()
  91. {
  92. }
  93. //---------------------------------------------------------------------
  94. int __fastcall TSessionData::Compare(TNamedObject * Other)
  95. {
  96. int Result;
  97. // To avoid using CompareLogicalText on hex names of sessions in workspace.
  98. // The session 000A would be sorted before 0001.
  99. if (IsWorkspace && DebugNotNull(dynamic_cast<TSessionData *>(Other))->IsWorkspace)
  100. {
  101. Result = CompareText(Name, Other->Name);
  102. }
  103. else
  104. {
  105. Result = TNamedObject::Compare(Other);
  106. }
  107. return Result;
  108. }
  109. //---------------------------------------------------------------------
  110. TSessionData * __fastcall TSessionData::Clone()
  111. {
  112. std::unique_ptr<TSessionData> Data(new TSessionData(L""));
  113. Data->Assign(this);
  114. return Data.release();
  115. }
  116. //---------------------------------------------------------------------
  117. void __fastcall TSessionData::DefaultSettings()
  118. {
  119. HostName = L"";
  120. PortNumber = SshPortNumber;
  121. UserName = L"";
  122. Password = L"";
  123. NewPassword = L"";
  124. ChangePassword = false;
  125. PingInterval = 30;
  126. PingType = ptOff;
  127. Timeout = 15;
  128. TryAgent = true;
  129. AgentFwd = false;
  130. AuthTIS = false;
  131. AuthKI = true;
  132. AuthKIPassword = true;
  133. AuthGSSAPI = true;
  134. AuthGSSAPIKEX = false;
  135. GSSAPIFwdTGT = false;
  136. LogicalHostName = L"";
  137. ChangeUsername = false;
  138. Compression = false;
  139. SshProt = ssh2only;
  140. Ssh2DES = false;
  141. SshNoUserAuth = false;
  142. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  143. {
  144. Cipher[Index] = DefaultCipherList[Index];
  145. }
  146. for (int Index = 0; Index < KEX_COUNT; Index++)
  147. {
  148. Kex[Index] = DefaultKexList[Index];
  149. }
  150. for (int Index = 0; Index < HOSTKEY_COUNT; Index++)
  151. {
  152. HostKeys[Index] = DefaultHostKeyList[Index];
  153. }
  154. for (int Index = 0; Index < GSSLIB_COUNT; Index++)
  155. {
  156. GssLib[Index] = DefaultGssLibList[Index];
  157. }
  158. GssLibCustom = L"";
  159. PublicKeyFile = L"";
  160. Passphrase = L"";
  161. FPuttyProtocol = L"";
  162. TcpNoDelay = false;
  163. SendBuf = DefaultSendBuf;
  164. SourceAddress = L"";
  165. ProtocolFeatures = L"";
  166. SshSimple = true;
  167. HostKey = L"";
  168. FingerprintScan = false;
  169. FOverrideCachedHostKey = true;
  170. Note = L"";
  171. WinTitle = L"";
  172. InternalEditorEncoding = -1;
  173. EncryptKey = UnicodeString();
  174. WebDavLiberalEscaping = false;
  175. ProxyMethod = ::pmNone;
  176. ProxyHost = L"proxy";
  177. ProxyPort = ProxyPortNumber;
  178. ProxyUsername = L"";
  179. ProxyPassword = L"";
  180. ProxyTelnetCommand = L"connect %host %port\\n";
  181. ProxyLocalCommand = L"";
  182. ProxyDNS = asAuto;
  183. ProxyLocalhost = false;
  184. for (unsigned int Index = 0; Index < LENOF(FBugs); Index++)
  185. {
  186. Bug[(TSshBug)Index] = asAuto;
  187. }
  188. Special = false;
  189. FSProtocol = fsSFTP;
  190. AddressFamily = afAuto;
  191. RekeyData = L"1G";
  192. RekeyTime = MinsPerHour;
  193. // FS common
  194. LocalDirectory = L"";
  195. RemoteDirectory = L"";
  196. SynchronizeBrowsing = false;
  197. UpdateDirectories = true;
  198. CacheDirectories = true;
  199. CacheDirectoryChanges = true;
  200. PreserveDirectoryChanges = true;
  201. LockInHome = false;
  202. ResolveSymlinks = true;
  203. FollowDirectorySymlinks = false;
  204. DSTMode = dstmUnix;
  205. DeleteToRecycleBin = false;
  206. OverwrittenToRecycleBin = false;
  207. RecycleBinPath = L"";
  208. Color = 0;
  209. PostLoginCommands = L"";
  210. // SCP
  211. LookupUserGroups = asAuto;
  212. EOLType = eolLF;
  213. TrimVMSVersions = false;
  214. Shell = L""; //default shell
  215. ReturnVar = L"";
  216. ExitCode1IsError = false;
  217. ClearAliases = true;
  218. UnsetNationalVars = true;
  219. ListingCommand = L"ls -la";
  220. IgnoreLsWarnings = true;
  221. Scp1Compatibility = false;
  222. TimeDifference = 0;
  223. TimeDifferenceAuto = true;
  224. SCPLsFullTime = asAuto;
  225. NotUtf = asAuto;
  226. S3DefaultRegion = L"";
  227. S3SessionToken = L"";
  228. S3UrlStyle = s3usVirtualHost;
  229. S3MaxKeys = asAuto;
  230. // SFTP
  231. SftpServer = L"";
  232. SFTPDownloadQueue = 32;
  233. SFTPUploadQueue = 32;
  234. SFTPListingQueue = 2;
  235. SFTPMaxVersion = ::SFTPMaxVersion;
  236. SFTPMaxPacketSize = 0;
  237. SFTPRealPath = asAuto;
  238. for (unsigned int Index = 0; Index < LENOF(FSFTPBugs); Index++)
  239. {
  240. SFTPBug[(TSftpBug)Index] = asAuto;
  241. }
  242. Tunnel = false;
  243. TunnelHostName = L"";
  244. TunnelPortNumber = SshPortNumber;
  245. TunnelUserName = L"";
  246. TunnelPassword = L"";
  247. TunnelPublicKeyFile = L"";
  248. TunnelLocalPortNumber = 0;
  249. TunnelPortFwd = L"";
  250. TunnelHostKey = L"";
  251. // FTP
  252. FtpPasvMode = true;
  253. FtpForcePasvIp = asAuto;
  254. FtpUseMlsd = asAuto;
  255. FtpAccount = L"";
  256. FtpPingInterval = 30;
  257. FtpPingType = ptDummyCommand;
  258. FtpTransferActiveImmediately = asAuto;
  259. Ftps = ftpsNone;
  260. MinTlsVersion = tls10;
  261. MaxTlsVersion = tls13;
  262. FtpListAll = asAuto;
  263. FtpHost = asAuto;
  264. FtpWorkFromCwd = asAuto;
  265. SslSessionReuse = true;
  266. TlsCertificateFile = L"";
  267. FtpProxyLogonType = 0; // none
  268. PuttySettings = UnicodeString();
  269. CustomParam1 = L"";
  270. CustomParam2 = L"";
  271. }
  272. //---------------------------------------------------------------------
  273. void __fastcall TSessionData::Default()
  274. {
  275. DefaultSettings();
  276. IsWorkspace = false;
  277. Link = L"";
  278. NameOverride = L"";
  279. Selected = false;
  280. FModified = false;
  281. FSource = ::ssNone;
  282. FSaveOnly = false;
  283. // add also to TSessionLog::AddStartupInfo()
  284. }
  285. //---------------------------------------------------------------------
  286. void __fastcall TSessionData::NonPersistant()
  287. {
  288. UpdateDirectories = false;
  289. PreserveDirectoryChanges = false;
  290. }
  291. //---------------------------------------------------------------------
  292. #define PROPERTY(P) PROPERTY_HANDLER(P, )
  293. #define BASE_PROPERTIES \
  294. PROPERTY(HostName); \
  295. PROPERTY(PortNumber); \
  296. PROPERTY(UserName); \
  297. PROPERTY_HANDLER(Password, F); \
  298. PROPERTY(PublicKeyFile); \
  299. PROPERTY_HANDLER(Passphrase, F); \
  300. PROPERTY(FSProtocol); \
  301. PROPERTY(Ftps); \
  302. PROPERTY(LocalDirectory); \
  303. PROPERTY(RemoteDirectory); \
  304. PROPERTY(Color); \
  305. PROPERTY(SynchronizeBrowsing); \
  306. PROPERTY(Note);
  307. //---------------------------------------------------------------------
  308. #define ADVANCED_PROPERTIES \
  309. PROPERTY_HANDLER(NewPassword, F); \
  310. PROPERTY(ChangePassword); \
  311. PROPERTY(PingInterval); \
  312. PROPERTY(PingType); \
  313. PROPERTY(Timeout); \
  314. PROPERTY(TryAgent); \
  315. PROPERTY(AgentFwd); \
  316. PROPERTY(AuthTIS); \
  317. PROPERTY(LogicalHostName); \
  318. PROPERTY(ChangeUsername); \
  319. PROPERTY(Compression); \
  320. PROPERTY(SshProt); \
  321. PROPERTY(Ssh2DES); \
  322. PROPERTY(SshNoUserAuth); \
  323. PROPERTY(CipherList); \
  324. PROPERTY(KexList); \
  325. PROPERTY(HostKeyList); \
  326. PROPERTY(GssLibList); \
  327. PROPERTY(GssLibCustom); \
  328. PROPERTY(AddressFamily); \
  329. PROPERTY(RekeyData); \
  330. PROPERTY(RekeyTime); \
  331. PROPERTY(HostKey); \
  332. PROPERTY(FingerprintScan); \
  333. PROPERTY(InternalEditorEncoding); \
  334. \
  335. PROPERTY(UpdateDirectories); \
  336. PROPERTY(CacheDirectories); \
  337. PROPERTY(CacheDirectoryChanges); \
  338. PROPERTY(PreserveDirectoryChanges); \
  339. \
  340. PROPERTY(ResolveSymlinks); \
  341. PROPERTY(FollowDirectorySymlinks); \
  342. PROPERTY(DSTMode); \
  343. PROPERTY(LockInHome); \
  344. PROPERTY(Special); \
  345. PROPERTY(Selected); \
  346. PROPERTY(ReturnVar); \
  347. PROPERTY(ExitCode1IsError); \
  348. PROPERTY(LookupUserGroups); \
  349. PROPERTY(EOLType); \
  350. PROPERTY(TrimVMSVersions); \
  351. PROPERTY(Shell); \
  352. PROPERTY(ClearAliases); \
  353. PROPERTY(Scp1Compatibility); \
  354. PROPERTY(UnsetNationalVars); \
  355. PROPERTY(ListingCommand); \
  356. PROPERTY(IgnoreLsWarnings); \
  357. PROPERTY(SCPLsFullTime); \
  358. \
  359. PROPERTY(TimeDifference); \
  360. PROPERTY(TimeDifferenceAuto); \
  361. PROPERTY(TcpNoDelay); \
  362. PROPERTY(SendBuf); \
  363. PROPERTY(SourceAddress); \
  364. PROPERTY(ProtocolFeatures); \
  365. PROPERTY(SshSimple); \
  366. PROPERTY(AuthKI); \
  367. PROPERTY(AuthKIPassword); \
  368. PROPERTY(AuthGSSAPI); \
  369. PROPERTY(AuthGSSAPIKEX); \
  370. PROPERTY(GSSAPIFwdTGT); \
  371. PROPERTY(DeleteToRecycleBin); \
  372. PROPERTY(OverwrittenToRecycleBin); \
  373. PROPERTY(RecycleBinPath); \
  374. PROPERTY(NotUtf); \
  375. PROPERTY(PostLoginCommands); \
  376. \
  377. PROPERTY(S3DefaultRegion); \
  378. PROPERTY(S3SessionToken); \
  379. PROPERTY(S3UrlStyle); \
  380. PROPERTY(S3MaxKeys); \
  381. \
  382. PROPERTY(ProxyMethod); \
  383. PROPERTY(ProxyHost); \
  384. PROPERTY(ProxyPort); \
  385. PROPERTY(ProxyUsername); \
  386. PROPERTY_HANDLER(ProxyPassword, F); \
  387. PROPERTY(ProxyTelnetCommand); \
  388. PROPERTY(ProxyLocalCommand); \
  389. PROPERTY(ProxyDNS); \
  390. PROPERTY(ProxyLocalhost); \
  391. \
  392. for (unsigned int Index = 0; Index < LENOF(FBugs); Index++) \
  393. { \
  394. PROPERTY(Bug[(TSshBug)Index]); \
  395. } \
  396. \
  397. PROPERTY(SftpServer); \
  398. PROPERTY(SFTPDownloadQueue); \
  399. PROPERTY(SFTPUploadQueue); \
  400. PROPERTY(SFTPListingQueue); \
  401. PROPERTY(SFTPMaxVersion); \
  402. PROPERTY(SFTPMaxPacketSize); \
  403. PROPERTY(SFTPRealPath); \
  404. \
  405. for (unsigned int Index = 0; Index < LENOF(FSFTPBugs); Index++) \
  406. { \
  407. PROPERTY(SFTPBug[(TSftpBug)Index]); \
  408. } \
  409. \
  410. PROPERTY(Tunnel); \
  411. PROPERTY(TunnelHostName); \
  412. PROPERTY(TunnelPortNumber); \
  413. PROPERTY(TunnelUserName); \
  414. PROPERTY_HANDLER(TunnelPassword, F); \
  415. PROPERTY(TunnelPublicKeyFile); \
  416. PROPERTY(TunnelLocalPortNumber); \
  417. PROPERTY(TunnelPortFwd); \
  418. PROPERTY(TunnelHostKey); \
  419. \
  420. PROPERTY(FtpPasvMode); \
  421. PROPERTY(FtpForcePasvIp); \
  422. PROPERTY(FtpUseMlsd); \
  423. PROPERTY(FtpAccount); \
  424. PROPERTY(FtpPingInterval); \
  425. PROPERTY(FtpPingType); \
  426. PROPERTY(FtpTransferActiveImmediately); \
  427. PROPERTY(FtpListAll); \
  428. PROPERTY(FtpHost); \
  429. PROPERTY(FtpWorkFromCwd); \
  430. PROPERTY(SslSessionReuse); \
  431. PROPERTY(TlsCertificateFile); \
  432. \
  433. PROPERTY(FtpProxyLogonType); \
  434. \
  435. PROPERTY(MinTlsVersion); \
  436. PROPERTY(MaxTlsVersion); \
  437. \
  438. PROPERTY(WinTitle); \
  439. \
  440. PROPERTY_HANDLER(EncryptKey, F); \
  441. \
  442. PROPERTY(WebDavLiberalEscaping); \
  443. \
  444. PROPERTY(PuttySettings); \
  445. \
  446. PROPERTY(CustomParam1); \
  447. PROPERTY(CustomParam2);
  448. #define META_PROPERTIES \
  449. PROPERTY(IsWorkspace); \
  450. PROPERTY(Link); \
  451. PROPERTY(NameOverride);
  452. //---------------------------------------------------------------------
  453. void __fastcall TSessionData::Assign(TPersistent * Source)
  454. {
  455. if (Source && Source->InheritsFrom(__classid(TSessionData)))
  456. {
  457. TSessionData * SourceData = (TSessionData *)Source;
  458. // Master password prompt shows implicitly here, when cloning the session data for a new terminal
  459. CopyData(SourceData);
  460. FSource = SourceData->FSource;
  461. }
  462. else
  463. {
  464. TNamedObject::Assign(Source);
  465. }
  466. }
  467. //---------------------------------------------------------------------
  468. void __fastcall TSessionData::DoCopyData(TSessionData * SourceData, bool NoRecrypt)
  469. {
  470. #define PROPERTY_HANDLER(P, F) \
  471. if (NoRecrypt) \
  472. { \
  473. F##P = SourceData->F##P; \
  474. } \
  475. else \
  476. { \
  477. P = SourceData->P; \
  478. }
  479. PROPERTY(Name);
  480. BASE_PROPERTIES;
  481. ADVANCED_PROPERTIES;
  482. META_PROPERTIES;
  483. #undef PROPERTY_HANDLER
  484. FOverrideCachedHostKey = SourceData->FOverrideCachedHostKey;
  485. FModified = SourceData->Modified;
  486. FSaveOnly = SourceData->FSaveOnly;
  487. }
  488. //---------------------------------------------------------------------
  489. void __fastcall TSessionData::CopyData(TSessionData * SourceData)
  490. {
  491. DoCopyData(SourceData, false);
  492. }
  493. //---------------------------------------------------------------------
  494. void __fastcall TSessionData::CopyDataNoRecrypt(TSessionData * SourceData)
  495. {
  496. DoCopyData(SourceData, true);
  497. }
  498. //---------------------------------------------------------------------
  499. void __fastcall TSessionData::CopyDirectoriesStateData(TSessionData * SourceData)
  500. {
  501. RemoteDirectory = SourceData->RemoteDirectory;
  502. LocalDirectory = SourceData->LocalDirectory;
  503. SynchronizeBrowsing = SourceData->SynchronizeBrowsing;
  504. }
  505. //---------------------------------------------------------------------
  506. bool __fastcall TSessionData::HasStateData()
  507. {
  508. return
  509. !RemoteDirectory.IsEmpty() ||
  510. !LocalDirectory.IsEmpty() ||
  511. (Color != 0);
  512. }
  513. //---------------------------------------------------------------------
  514. void __fastcall TSessionData::CopyStateData(TSessionData * SourceData)
  515. {
  516. // Keep in sync with TCustomScpExplorerForm::UpdateSessionData.
  517. CopyDirectoriesStateData(SourceData);
  518. Color = SourceData->Color;
  519. }
  520. //---------------------------------------------------------------------
  521. void __fastcall TSessionData::CopyNonCoreData(TSessionData * SourceData)
  522. {
  523. CopyStateData(SourceData);
  524. UpdateDirectories = SourceData->UpdateDirectories;
  525. Note = SourceData->Note;
  526. }
  527. //---------------------------------------------------------------------
  528. bool __fastcall TSessionData::IsSame(
  529. const TSessionData * Default, bool AdvancedOnly, TStrings * DifferentProperties, bool Decrypted)
  530. {
  531. bool Result = true;
  532. #define PROPERTY_HANDLER(P, F) \
  533. if ((Decrypted && (P != Default->P)) || \
  534. (!Decrypted && (F##P != Default->F##P))) \
  535. { \
  536. Result = false; \
  537. if (DifferentProperties != NULL) \
  538. { \
  539. DifferentProperties->Add(#P); \
  540. } \
  541. else \
  542. { \
  543. return Result; \
  544. } \
  545. }
  546. if (!AdvancedOnly)
  547. {
  548. BASE_PROPERTIES;
  549. META_PROPERTIES;
  550. }
  551. ADVANCED_PROPERTIES;
  552. #undef PROPERTY_HANDLER
  553. return Result;
  554. }
  555. //---------------------------------------------------------------------
  556. bool __fastcall TSessionData::IsSame(const TSessionData * Default, bool AdvancedOnly)
  557. {
  558. return IsSame(Default, AdvancedOnly, NULL, false);
  559. }
  560. //---------------------------------------------------------------------
  561. bool __fastcall TSessionData::IsSameDecrypted(const TSessionData * Default)
  562. {
  563. return IsSame(Default, false, NULL, true);
  564. }
  565. //---------------------------------------------------------------------
  566. TFSProtocol NormalizeFSProtocol(TFSProtocol FSProtocol)
  567. {
  568. if (FSProtocol == fsSFTPonly)
  569. {
  570. FSProtocol = fsSFTP;
  571. }
  572. return FSProtocol;
  573. }
  574. //---------------------------------------------------------------------
  575. bool __fastcall TSessionData::IsSameSite(const TSessionData * Other)
  576. {
  577. return
  578. // Particularly when handling /refresh,
  579. // fsSFTPonly sites when compared against sftp:// URLs (fsSFTP) have to match.
  580. // But similarly also falled back SCP sites.
  581. (NormalizeFSProtocol(FSProtocol) == NormalizeFSProtocol(Other->FSProtocol)) &&
  582. (HostName == Other->HostName) &&
  583. (PortNumber == Other->PortNumber) &&
  584. (UserName == Other->UserName);
  585. }
  586. //---------------------------------------------------------------------
  587. bool __fastcall TSessionData::IsInFolderOrWorkspace(UnicodeString AFolder)
  588. {
  589. return StartsText(UnixIncludeTrailingBackslash(AFolder), Name);
  590. }
  591. //---------------------------------------------------------------------
  592. void __fastcall TSessionData::DoLoad(THierarchicalStorage * Storage, bool PuttyImport, bool & RewritePassword, bool Unsafe)
  593. {
  594. // Make sure we only ever use methods supported by TOptionsStorage
  595. // (implemented by TOptionsIniFile)
  596. PortNumber = Storage->ReadInteger(L"PortNumber", PortNumber);
  597. UserName = Storage->ReadString(L"UserName", UserName);
  598. // must be loaded after UserName, because HostName may be in format user@host
  599. HostName = Storage->ReadString(L"HostName", HostName);
  600. if (!Configuration->DisablePasswordStoring)
  601. {
  602. if (Storage->ValueExists(L"PasswordPlain"))
  603. {
  604. Password = Storage->ReadString(L"PasswordPlain", Password);
  605. RewritePassword = true;
  606. }
  607. else
  608. {
  609. RawByteString APassword = Storage->ReadStringAsBinaryData(L"Password", FPassword);
  610. SET_SESSION_PROPERTY_FROM(Password, APassword);
  611. }
  612. }
  613. HostKey = Storage->ReadString(L"SshHostKey", HostKey); // probably never used
  614. Note = Storage->ReadString(L"Note", Note);
  615. // Putty uses PingIntervalSecs
  616. int PingIntervalSecs = Storage->ReadInteger(L"PingIntervalSecs", -1);
  617. if (PingIntervalSecs < 0)
  618. {
  619. PingIntervalSecs = Storage->ReadInteger(L"PingIntervalSec", PingInterval%SecsPerMin);
  620. }
  621. PingInterval =
  622. Storage->ReadInteger(L"PingInterval", PingInterval/SecsPerMin)*SecsPerMin +
  623. PingIntervalSecs;
  624. if (PingInterval == 0)
  625. {
  626. PingInterval = 30;
  627. }
  628. PingType = static_cast<TPingType>(Storage->ReadInteger(L"PingType", PingType));
  629. Timeout = Storage->ReadInteger(L"Timeout", Timeout);
  630. TryAgent = Storage->ReadBool(L"TryAgent", TryAgent);
  631. AgentFwd = Storage->ReadBool(L"AgentFwd", AgentFwd);
  632. AuthTIS = Storage->ReadBool(L"AuthTIS", AuthTIS);
  633. AuthKI = Storage->ReadBool(L"AuthKI", AuthKI);
  634. AuthKIPassword = Storage->ReadBool(L"AuthKIPassword", AuthKIPassword);
  635. // Continue to use setting keys of previous kerberos implementation (vaclav tomec),
  636. // but fallback to keys of other implementations (official putty and vintela quest putty),
  637. // to allow imports from all putty versions.
  638. // Both vaclav tomec and official putty use AuthGSSAPI
  639. AuthGSSAPI = Storage->ReadBool(L"AuthGSSAPI", Storage->ReadBool(L"AuthSSPI", AuthGSSAPI));
  640. AuthGSSAPIKEX = Storage->ReadBool(L"AuthGSSAPIKEX", AuthGSSAPIKEX);
  641. GSSAPIFwdTGT = Storage->ReadBool(L"GSSAPIFwdTGT", Storage->ReadBool(L"GssapiFwd", Storage->ReadBool(L"SSPIFwdTGT", GSSAPIFwdTGT)));
  642. // KerbPrincipal was used by Quest PuTTY
  643. // GSSAPIServerRealm was used by Vaclav Tomec
  644. LogicalHostName = Storage->ReadString(L"LogicalHostName", Storage->ReadString(L"GSSAPIServerRealm", Storage->ReadString(L"KerbPrincipal", LogicalHostName)));
  645. ChangeUsername = Storage->ReadBool(L"ChangeUsername", ChangeUsername);
  646. Compression = Storage->ReadBool(L"Compression", Compression);
  647. TSshProt ASshProt = (TSshProt)Storage->ReadInteger(L"SshProt", SshProt);
  648. // Old sessions may contain the values correponding to the fallbacks we used to allow; migrate them
  649. if (ASshProt == ssh2deprecated)
  650. {
  651. ASshProt = ssh2only;
  652. }
  653. else if (ASshProt == ssh1deprecated)
  654. {
  655. ASshProt = ssh1only;
  656. }
  657. SshProt = ASshProt;
  658. Ssh2DES = Storage->ReadBool(L"Ssh2DES", Ssh2DES);
  659. SshNoUserAuth = Storage->ReadBool(L"SshNoUserAuth", SshNoUserAuth);
  660. CipherList = Storage->ReadString(L"Cipher", CipherList);
  661. KexList = Storage->ReadString(L"KEX", KexList);
  662. HostKeyList = Storage->ReadString(L"HostKey", HostKeyList);
  663. if (!Unsafe)
  664. {
  665. GssLibList = Storage->ReadString(L"GSSLibs", GssLibList);
  666. }
  667. GssLibCustom = Storage->ReadString(L"GSSCustom", GssLibCustom);
  668. PublicKeyFile = Storage->ReadString(L"PublicKeyFile", PublicKeyFile);
  669. AddressFamily = static_cast<TAddressFamily>
  670. (Storage->ReadInteger(L"AddressFamily", AddressFamily));
  671. RekeyData = Storage->ReadString(L"RekeyBytes", RekeyData);
  672. RekeyTime = Storage->ReadInteger(L"RekeyTime", RekeyTime);
  673. FSProtocol = (TFSProtocol)Storage->ReadInteger(L"FSProtocol", FSProtocol);
  674. LocalDirectory = Storage->ReadString(L"LocalDirectory", LocalDirectory);
  675. RemoteDirectory = Storage->ReadString(L"RemoteDirectory", RemoteDirectory);
  676. SynchronizeBrowsing = Storage->ReadBool(L"SynchronizeBrowsing", SynchronizeBrowsing);
  677. UpdateDirectories = Storage->ReadBool(L"UpdateDirectories", UpdateDirectories);
  678. CacheDirectories = Storage->ReadBool(L"CacheDirectories", CacheDirectories);
  679. CacheDirectoryChanges = Storage->ReadBool(L"CacheDirectoryChanges", CacheDirectoryChanges);
  680. PreserveDirectoryChanges = Storage->ReadBool(L"PreserveDirectoryChanges", PreserveDirectoryChanges);
  681. ResolveSymlinks = Storage->ReadBool(L"ResolveSymlinks", ResolveSymlinks);
  682. FollowDirectorySymlinks = Storage->ReadBool(L"FollowDirectorySymlinks", FollowDirectorySymlinks);
  683. DSTMode = (TDSTMode)Storage->ReadInteger(L"ConsiderDST", DSTMode);
  684. LockInHome = Storage->ReadBool(L"LockInHome", LockInHome);
  685. Special = Storage->ReadBool(L"Special", Special);
  686. if (!Unsafe)
  687. {
  688. Shell = Storage->ReadString(L"Shell", Shell);
  689. }
  690. ClearAliases = Storage->ReadBool(L"ClearAliases", ClearAliases);
  691. UnsetNationalVars = Storage->ReadBool(L"UnsetNationalVars", UnsetNationalVars);
  692. if (!Unsafe)
  693. {
  694. ListingCommand = Storage->ReadString(L"ListingCommand",
  695. Storage->ReadBool(L"AliasGroupList", false) ? UnicodeString(L"ls -gla") : ListingCommand);
  696. }
  697. IgnoreLsWarnings = Storage->ReadBool(L"IgnoreLsWarnings", IgnoreLsWarnings);
  698. SCPLsFullTime = Storage->ReadEnum(L"SCPLsFullTime", SCPLsFullTime, AutoSwitchMapping);
  699. Scp1Compatibility = Storage->ReadBool(L"Scp1Compatibility", Scp1Compatibility);
  700. TimeDifference = Storage->ReadFloat(L"TimeDifference", TimeDifference);
  701. TimeDifferenceAuto = Storage->ReadBool(L"TimeDifferenceAuto", (TimeDifference == TDateTime()));
  702. if (!Unsafe)
  703. {
  704. DeleteToRecycleBin = Storage->ReadBool(L"DeleteToRecycleBin", DeleteToRecycleBin);
  705. OverwrittenToRecycleBin = Storage->ReadBool(L"OverwrittenToRecycleBin", OverwrittenToRecycleBin);
  706. RecycleBinPath = Storage->ReadString(L"RecycleBinPath", RecycleBinPath);
  707. PostLoginCommands = Storage->ReadString(L"PostLoginCommands", PostLoginCommands);
  708. ReturnVar = Storage->ReadString(L"ReturnVar", ReturnVar);
  709. }
  710. ExitCode1IsError = Storage->ReadBool(L"ExitCode1IsError", ExitCode1IsError);
  711. LookupUserGroups = Storage->ReadEnum(L"LookupUserGroups2", LookupUserGroups, AutoSwitchMapping);
  712. EOLType = (TEOLType)Storage->ReadInteger(L"EOLType", EOLType);
  713. TrimVMSVersions = Storage->ReadBool(L"TrimVMSVersions", TrimVMSVersions);
  714. NotUtf = Storage->ReadEnum(L"Utf", Storage->ReadEnum(L"SFTPUtfBug", NotUtf), AutoSwitchReversedMapping);
  715. InternalEditorEncoding = Storage->ReadInteger(L"InternalEditorEncoding", InternalEditorEncoding);
  716. S3DefaultRegion = Storage->ReadString(L"S3DefaultRegion", S3DefaultRegion);
  717. S3SessionToken = Storage->ReadString(L"S3SessionToken", S3SessionToken);
  718. S3UrlStyle = (TS3UrlStyle)Storage->ReadInteger(L"S3UrlStyle", S3UrlStyle);
  719. S3MaxKeys = Storage->ReadEnum(L"S3MaxKeys", S3MaxKeys, AutoSwitchMapping);
  720. // PuTTY defaults to TcpNoDelay, but the psftp/pscp ignores this preference, and always set this to off (what is our default too)
  721. if (!PuttyImport)
  722. {
  723. TcpNoDelay = Storage->ReadBool(L"TcpNoDelay", TcpNoDelay);
  724. }
  725. SendBuf = Storage->ReadInteger(L"SendBuf", Storage->ReadInteger("SshSendBuf", SendBuf));
  726. SourceAddress = Storage->ReadString(L"SourceAddress", SourceAddress);
  727. ProtocolFeatures = Storage->ReadString(L"ProtocolFeatures", ProtocolFeatures);
  728. SshSimple = Storage->ReadBool(L"SshSimple", SshSimple);
  729. ProxyMethod = (TProxyMethod)Storage->ReadInteger(L"ProxyMethod", ProxyMethod);
  730. ProxyHost = Storage->ReadString(L"ProxyHost", ProxyHost);
  731. ProxyPort = Storage->ReadInteger(L"ProxyPort", ProxyPort);
  732. ProxyUsername = Storage->ReadString(L"ProxyUsername", ProxyUsername);
  733. if (Storage->ValueExists(L"ProxyPassword"))
  734. {
  735. // encrypt unencrypted password
  736. ProxyPassword = Storage->ReadString(L"ProxyPassword", L"");
  737. }
  738. else
  739. {
  740. // load encrypted password
  741. RawByteString AProxyPassword = Storage->ReadStringAsBinaryData(L"ProxyPasswordEnc", FProxyPassword);
  742. SET_SESSION_PROPERTY_FROM(ProxyPassword, AProxyPassword);
  743. }
  744. if (!Unsafe)
  745. {
  746. if (ProxyMethod == pmCmd)
  747. {
  748. ProxyLocalCommand = Storage->ReadStringRaw(L"ProxyTelnetCommand", ProxyLocalCommand);
  749. }
  750. else
  751. {
  752. ProxyTelnetCommand = Storage->ReadStringRaw(L"ProxyTelnetCommand", ProxyTelnetCommand);
  753. }
  754. }
  755. ProxyDNS = TAutoSwitch((Storage->ReadInteger(L"ProxyDNS", (ProxyDNS + 2) % 3) + 1) % 3);
  756. ProxyLocalhost = Storage->ReadBool(L"ProxyLocalhost", ProxyLocalhost);
  757. #define READ_BUG(BUG) \
  758. Bug[sb##BUG] = TAutoSwitch(2 - Storage->ReadInteger(L"Bug"#BUG, \
  759. 2 - Bug[sb##BUG]));
  760. READ_BUG(Ignore1);
  761. READ_BUG(PlainPW1);
  762. READ_BUG(RSA1);
  763. READ_BUG(HMAC2);
  764. READ_BUG(DeriveKey2);
  765. READ_BUG(RSAPad2);
  766. READ_BUG(PKSessID2);
  767. READ_BUG(Rekey2);
  768. READ_BUG(MaxPkt2);
  769. READ_BUG(Ignore2);
  770. READ_BUG(OldGex2);
  771. READ_BUG(WinAdj);
  772. READ_BUG(ChanReq);
  773. #undef READ_BUG
  774. if ((Bug[sbHMAC2] == asAuto) &&
  775. Storage->ReadBool(L"BuggyMAC", false))
  776. {
  777. Bug[sbHMAC2] = asOn;
  778. }
  779. if (!Unsafe)
  780. {
  781. SftpServer = Storage->ReadString(L"SftpServer", SftpServer);
  782. }
  783. #define READ_SFTP_BUG(BUG) \
  784. SFTPBug[sb##BUG] = Storage->ReadEnum(L"SFTP" #BUG "Bug", SFTPBug[sb##BUG], AutoSwitchMapping);
  785. READ_SFTP_BUG(Symlink);
  786. READ_SFTP_BUG(SignedTS);
  787. #undef READ_SFTP_BUG
  788. SFTPMaxVersion = Storage->ReadInteger(L"SFTPMaxVersion", SFTPMaxVersion);
  789. SFTPMaxPacketSize = Storage->ReadInteger(L"SFTPMaxPacketSize", SFTPMaxPacketSize);
  790. SFTPDownloadQueue = Storage->ReadInteger(L"SFTPDownloadQueue", SFTPDownloadQueue);
  791. SFTPUploadQueue = Storage->ReadInteger(L"SFTPUploadQueue", SFTPUploadQueue);
  792. SFTPListingQueue = Storage->ReadInteger(L"SFTPListingQueue", SFTPListingQueue);
  793. SFTPRealPath = Storage->ReadEnum(L"SFTPRealPath", SFTPRealPath, AutoSwitchMapping);
  794. Color = Storage->ReadInteger(L"Color", Color);
  795. PuttyProtocol = Storage->ReadString(L"Protocol", PuttyProtocol);
  796. Tunnel = Storage->ReadBool(L"Tunnel", Tunnel);
  797. TunnelPortNumber = Storage->ReadInteger(L"TunnelPortNumber", TunnelPortNumber);
  798. TunnelUserName = Storage->ReadString(L"TunnelUserName", TunnelUserName);
  799. // must be loaded after TunnelUserName,
  800. // because TunnelHostName may be in format user@host
  801. TunnelHostName = Storage->ReadString(L"TunnelHostName", TunnelHostName);
  802. if (!Configuration->DisablePasswordStoring)
  803. {
  804. if (Storage->ValueExists(L"TunnelPasswordPlain"))
  805. {
  806. TunnelPassword = Storage->ReadString(L"TunnelPasswordPlain", TunnelPassword);
  807. RewritePassword = true;
  808. }
  809. else
  810. {
  811. RawByteString ATunnelPassword = Storage->ReadStringAsBinaryData(L"TunnelPassword", FTunnelPassword);
  812. SET_SESSION_PROPERTY_FROM(TunnelPassword, ATunnelPassword);
  813. }
  814. }
  815. TunnelPublicKeyFile = Storage->ReadString(L"TunnelPublicKeyFile", TunnelPublicKeyFile);
  816. TunnelLocalPortNumber = Storage->ReadInteger(L"TunnelLocalPortNumber", TunnelLocalPortNumber);
  817. TunnelHostKey = Storage->ReadString(L"TunnelHostKey", TunnelHostKey);
  818. // Ftp prefix
  819. FtpPasvMode = Storage->ReadBool(L"FtpPasvMode", FtpPasvMode);
  820. FtpForcePasvIp = Storage->ReadEnum(L"FtpForcePasvIp2", FtpForcePasvIp, AutoSwitchMapping);
  821. FtpUseMlsd = Storage->ReadEnum(L"FtpUseMlsd", FtpUseMlsd, AutoSwitchMapping);
  822. FtpAccount = Storage->ReadString(L"FtpAccount", FtpAccount);
  823. FtpPingInterval = Storage->ReadInteger(L"FtpPingInterval", FtpPingInterval);
  824. FtpPingType = static_cast<TPingType>(Storage->ReadInteger(L"FtpPingType", FtpPingType));
  825. FtpTransferActiveImmediately = Storage->ReadEnum(L"FtpTransferActiveImmediately2", FtpTransferActiveImmediately, AutoSwitchMapping);
  826. Ftps = static_cast<TFtps>(Storage->ReadInteger(L"Ftps", Ftps));
  827. FtpListAll = Storage->ReadEnum(L"FtpListAll", FtpListAll, AutoSwitchMapping);
  828. FtpHost = Storage->ReadEnum(L"FtpHost", FtpHost, AutoSwitchMapping);
  829. FtpWorkFromCwd = Storage->ReadEnum(L"FtpWorkFromCwd", Storage->ReadEnum(L"FtpDeleteFromCwd", FtpWorkFromCwd), AutoSwitchMapping);
  830. SslSessionReuse = Storage->ReadBool(L"SslSessionReuse", SslSessionReuse);
  831. TlsCertificateFile = Storage->ReadString(L"TlsCertificateFile", TlsCertificateFile);
  832. FtpProxyLogonType = Storage->ReadInteger(L"FtpProxyLogonType", FtpProxyLogonType);
  833. MinTlsVersion = static_cast<TTlsVersion>(Storage->ReadInteger(L"MinTlsVersion", MinTlsVersion));
  834. MaxTlsVersion = static_cast<TTlsVersion>(Storage->ReadInteger(L"MaxTlsVersion", MaxTlsVersion));
  835. if (Storage->ValueExists(L"EncryptKeyPlain"))
  836. {
  837. EncryptKey = Storage->ReadString(L"EncryptKeyPlain", EncryptKey);
  838. RewritePassword = true;
  839. }
  840. else
  841. {
  842. RawByteString AEncryptKey = Storage->ReadStringAsBinaryData(L"EncryptKey", FEncryptKey);
  843. SET_SESSION_PROPERTY_FROM(EncryptKey, AEncryptKey);
  844. }
  845. WebDavLiberalEscaping = Storage->ReadBool(L"WebDavLiberalEscaping", WebDavLiberalEscaping);
  846. IsWorkspace = Storage->ReadBool(L"IsWorkspace", IsWorkspace);
  847. Link = Storage->ReadString(L"Link", Link);
  848. NameOverride = Storage->ReadString(L"NameOverride", NameOverride);
  849. PuttySettings = Storage->ReadString(L"PuttySettings", PuttySettings);
  850. CustomParam1 = Storage->ReadString(L"CustomParam1", CustomParam1);
  851. CustomParam2 = Storage->ReadString(L"CustomParam2", CustomParam2);
  852. #ifdef TEST
  853. #define KEX_TEST(VALUE, EXPECTED) KexList = VALUE; DebugAssert(KexList == EXPECTED);
  854. #define KEX_DEFAULT L"ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1"
  855. // Empty source should result in default list
  856. KEX_TEST(L"", KEX_DEFAULT);
  857. // Default of pre 5.8.1
  858. KEX_TEST(L"dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN", L"ecdh,dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN");
  859. // Missing first two priority algos, and last non-priority algo
  860. KEX_TEST(L"dh-group14-sha1,dh-group1-sha1,WARN", L"ecdh,dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN");
  861. // Missing first two priority algos, last non-priority algo and WARN
  862. KEX_TEST(L"dh-group14-sha1,dh-group1-sha1", L"ecdh,dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN");
  863. // Old algos, with all but the first below WARN
  864. KEX_TEST(L"dh-gex-sha1,WARN,dh-group14-sha1,dh-group1-sha1,rsa", L"ecdh,dh-gex-sha1,WARN,dh-group14-sha1,dh-group1-sha1,rsa");
  865. // Unknown algo at front
  866. KEX_TEST(L"unknown,ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1", KEX_DEFAULT);
  867. // Unknown algo at back
  868. KEX_TEST(L"ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1,unknown", KEX_DEFAULT);
  869. // Unknown algo in the middle
  870. KEX_TEST(L"ecdh,dh-gex-sha1,dh-group14-sha1,unknown,rsa,WARN,dh-group1-sha1", KEX_DEFAULT);
  871. #undef KEX_DEFAULT
  872. #undef KEX_TEST
  873. #define CIPHER_TEST(VALUE, EXPECTED) CipherList = VALUE; DebugAssert(CipherList == EXPECTED);
  874. #define CIPHER_DEFAULT L"aes,chacha20,blowfish,3des,WARN,arcfour,des"
  875. // Empty source should result in default list
  876. CIPHER_TEST(L"", CIPHER_DEFAULT);
  877. // Default of pre 5.8.1
  878. CIPHER_TEST(L"aes,blowfish,3des,WARN,arcfour,des", L"aes,blowfish,3des,chacha20,WARN,arcfour,des");
  879. // Missing priority algo
  880. CIPHER_TEST(L"chacha20,blowfish,3des,WARN,arcfour,des", CIPHER_DEFAULT);
  881. // Missing non-priority algo
  882. CIPHER_TEST(L"aes,chacha20,3des,WARN,arcfour,des", L"aes,chacha20,3des,blowfish,WARN,arcfour,des");
  883. // Missing last warn algo
  884. CIPHER_TEST(L"aes,blowfish,chacha20,3des,WARN,arcfour", L"aes,blowfish,chacha20,3des,WARN,arcfour,des");
  885. // Missing first warn algo
  886. CIPHER_TEST(L"aes,blowfish,chacha20,3des,WARN,des", L"aes,blowfish,chacha20,3des,WARN,des,arcfour");
  887. #undef CIPHER_DEFAULT
  888. #undef CIPHER_TEST
  889. #define HOSTKEY_TEST(VALUE, EXPECTED) HostKeyList = VALUE; DebugAssert(HostKeyList == EXPECTED);
  890. #define HOSTKEY_DEFAULT L"ed25519,ecdsa,rsa,dsa,WARN"
  891. // Empty source should result in default list
  892. HOSTKEY_TEST(L"", HOSTKEY_DEFAULT);
  893. // Missing priority algo
  894. HOSTKEY_TEST(L"ecdsa,rsa,dsa,WARN", HOSTKEY_DEFAULT);
  895. // Missing non-priority algo
  896. HOSTKEY_TEST(L"ed25519,ecdsa,dsa,WARN", L"ed25519,ecdsa,dsa,rsa,WARN");
  897. #undef HOSTKEY_DEFAULT
  898. #undef HOSTKEY_TEST
  899. #endif
  900. }
  901. //---------------------------------------------------------------------
  902. void __fastcall TSessionData::Load(THierarchicalStorage * Storage, bool PuttyImport)
  903. {
  904. bool RewritePassword = false;
  905. if (Storage->OpenSubKey(InternalStorageKey, False))
  906. {
  907. // In case we are re-loading, reset passwords, to avoid pointless
  908. // re-cryption, while loading username/hostname. And moreover, when
  909. // the password is wrongly encrypted (using a different master password),
  910. // this breaks sites reload and consequently an overal operation,
  911. // such as opening Sites menu
  912. ClearSessionPasswords();
  913. FProxyPassword = L"";
  914. DoLoad(Storage, PuttyImport, RewritePassword, false);
  915. Storage->CloseSubKey();
  916. }
  917. if (RewritePassword)
  918. {
  919. TStorageAccessMode AccessMode = Storage->AccessMode;
  920. Storage->AccessMode = smReadWrite;
  921. try
  922. {
  923. if (Storage->OpenSubKey(InternalStorageKey, true))
  924. {
  925. Storage->DeleteValue(L"PasswordPlain");
  926. if (!Password.IsEmpty())
  927. {
  928. Storage->WriteBinaryDataAsString(L"Password", FPassword);
  929. }
  930. Storage->DeleteValue(L"TunnelPasswordPlain");
  931. if (!TunnelPassword.IsEmpty())
  932. {
  933. Storage->WriteBinaryDataAsString(L"TunnelPassword", FTunnelPassword);
  934. }
  935. Storage->DeleteValue(L"EncryptKeyPlain");
  936. if (!EncryptKey.IsEmpty())
  937. {
  938. Storage->WriteBinaryDataAsString(L"EncryptKey", FEncryptKey);
  939. }
  940. Storage->CloseSubKey();
  941. }
  942. }
  943. catch(...)
  944. {
  945. // ignore errors (like read-only INI file)
  946. }
  947. Storage->AccessMode = AccessMode;
  948. }
  949. FModified = false;
  950. FSource = ssStored;
  951. }
  952. //---------------------------------------------------------------------
  953. void __fastcall TSessionData::DoSave(THierarchicalStorage * Storage,
  954. bool PuttyExport, const TSessionData * Default, bool DoNotEncryptPasswords)
  955. {
  956. // Same as in TCopyParamType::Save
  957. #define WRITE_DATA_EX(TYPE, NAME, PROPERTY, CONV) \
  958. if ((Default != NULL) && (CONV(Default->PROPERTY) == CONV(PROPERTY))) \
  959. { \
  960. Storage->DeleteValue(NAME); \
  961. } \
  962. else \
  963. { \
  964. Storage->Write ## TYPE(NAME, CONV(PROPERTY)); \
  965. }
  966. #define WRITE_DATA_CONV(TYPE, NAME, PROPERTY) WRITE_DATA_EX(TYPE, NAME, PROPERTY, WRITE_DATA_CONV_FUNC)
  967. #define WRITE_DATA(TYPE, PROPERTY) WRITE_DATA_EX(TYPE, TEXT(#PROPERTY), PROPERTY, )
  968. WRITE_DATA(String, HostName);
  969. WRITE_DATA(Integer, PortNumber);
  970. WRITE_DATA_EX(Integer, L"PingInterval", PingInterval / SecsPerMin, );
  971. WRITE_DATA_EX(Integer, L"PingIntervalSecs", PingInterval % SecsPerMin, );
  972. Storage->DeleteValue(L"PingIntervalSec"); // obsolete
  973. WRITE_DATA(Integer, PingType);
  974. WRITE_DATA(Integer, Timeout);
  975. WRITE_DATA(Bool, TryAgent);
  976. WRITE_DATA(Bool, AgentFwd);
  977. WRITE_DATA(Bool, AuthTIS);
  978. WRITE_DATA(Bool, AuthKI);
  979. WRITE_DATA(Bool, AuthKIPassword);
  980. WRITE_DATA(String, Note);
  981. WRITE_DATA(Bool, AuthGSSAPI);
  982. WRITE_DATA(Bool, AuthGSSAPIKEX);
  983. WRITE_DATA(Bool, GSSAPIFwdTGT);
  984. Storage->DeleteValue(L"TryGSSKEX");
  985. Storage->DeleteValue(L"UserNameFromEnvironment");
  986. Storage->DeleteValue("GSSAPIServerChoosesUserName");
  987. Storage->DeleteValue(L"GSSAPITrustDNS");
  988. WRITE_DATA(String, LogicalHostName);
  989. if (PuttyExport)
  990. {
  991. // duplicate kerberos setting with keys of the vintela quest putty
  992. WRITE_DATA_EX(Bool, L"AuthSSPI", AuthGSSAPI, );
  993. WRITE_DATA_EX(Bool, L"SSPIFwdTGT", GSSAPIFwdTGT, );
  994. WRITE_DATA_EX(String, L"KerbPrincipal", LogicalHostName, );
  995. // duplicate kerberos setting with keys of the official putty
  996. WRITE_DATA_EX(Bool, L"GssapiFwd", GSSAPIFwdTGT, );
  997. }
  998. WRITE_DATA(Bool, ChangeUsername);
  999. WRITE_DATA(Bool, Compression);
  1000. WRITE_DATA(Integer, SshProt);
  1001. WRITE_DATA(Bool, Ssh2DES);
  1002. WRITE_DATA(Bool, SshNoUserAuth);
  1003. WRITE_DATA_EX(String, L"Cipher", CipherList, );
  1004. WRITE_DATA_EX(String, L"KEX", KexList, );
  1005. WRITE_DATA_EX(String, L"HostKey", HostKeyList, );
  1006. WRITE_DATA_EX(String, L"GSSLibs", GssLibList, );
  1007. WRITE_DATA_EX(String, L"GSSCustom", GssLibCustom, );
  1008. WRITE_DATA(Integer, AddressFamily);
  1009. WRITE_DATA_EX(String, L"RekeyBytes", RekeyData, );
  1010. WRITE_DATA(Integer, RekeyTime);
  1011. WRITE_DATA(Bool, TcpNoDelay);
  1012. if (PuttyExport)
  1013. {
  1014. WRITE_DATA(StringRaw, UserName);
  1015. // PuTTY is started in its binary directory to allow relative paths when opening PuTTY's own stored session.
  1016. // To allow relative paths in our sessions, we have to expand them for PuTTY.
  1017. WRITE_DATA_EX(StringRaw, L"PublicKeyFile", PublicKeyFile, ExpandFileName);
  1018. }
  1019. else
  1020. {
  1021. WRITE_DATA(String, UserName);
  1022. WRITE_DATA(String, PublicKeyFile);
  1023. WRITE_DATA(Integer, FSProtocol);
  1024. WRITE_DATA(String, LocalDirectory);
  1025. WRITE_DATA(String, RemoteDirectory);
  1026. WRITE_DATA(Bool, SynchronizeBrowsing);
  1027. WRITE_DATA(Bool, UpdateDirectories);
  1028. WRITE_DATA(Bool, CacheDirectories);
  1029. WRITE_DATA(Bool, CacheDirectoryChanges);
  1030. WRITE_DATA(Bool, PreserveDirectoryChanges);
  1031. WRITE_DATA(Bool, ResolveSymlinks);
  1032. WRITE_DATA(Bool, FollowDirectorySymlinks);
  1033. WRITE_DATA_EX(Integer, L"ConsiderDST", DSTMode, );
  1034. WRITE_DATA(Bool, LockInHome);
  1035. // Special is never stored (if it would, login dialog must be modified not to
  1036. // duplicate Special parameter when Special session is loaded and then stored
  1037. // under different name)
  1038. // WRITE_DATA(Bool, Special);
  1039. WRITE_DATA(String, Shell);
  1040. WRITE_DATA(Bool, ClearAliases);
  1041. WRITE_DATA(Bool, UnsetNationalVars);
  1042. WRITE_DATA(String, ListingCommand);
  1043. WRITE_DATA(Bool, IgnoreLsWarnings);
  1044. WRITE_DATA(Integer, SCPLsFullTime);
  1045. WRITE_DATA(Bool, Scp1Compatibility);
  1046. // TimeDifferenceAuto is valid for FTP protocol only.
  1047. // For other protocols it's typically true (default value),
  1048. // but ignored so TimeDifference is still taken into account (SCP only actually)
  1049. if (TimeDifferenceAuto && (FSProtocol == fsFTP))
  1050. {
  1051. // Have to delete it as TimeDifferenceAuto is not saved when enabled,
  1052. // but the default is derived from value of TimeDifference.
  1053. Storage->DeleteValue(L"TimeDifference");
  1054. }
  1055. else
  1056. {
  1057. WRITE_DATA(Float, TimeDifference);
  1058. }
  1059. WRITE_DATA(Bool, TimeDifferenceAuto);
  1060. WRITE_DATA(Bool, DeleteToRecycleBin);
  1061. WRITE_DATA(Bool, OverwrittenToRecycleBin);
  1062. WRITE_DATA(String, RecycleBinPath);
  1063. WRITE_DATA(String, PostLoginCommands);
  1064. WRITE_DATA(String, ReturnVar);
  1065. WRITE_DATA(Bool, ExitCode1IsError);
  1066. WRITE_DATA_EX(Integer, L"LookupUserGroups2", LookupUserGroups, );
  1067. WRITE_DATA(Integer, EOLType);
  1068. WRITE_DATA(Bool, TrimVMSVersions);
  1069. Storage->DeleteValue(L"SFTPUtfBug");
  1070. WRITE_DATA_EX(Integer, L"Utf", NotUtf, );
  1071. WRITE_DATA(Integer, InternalEditorEncoding);
  1072. WRITE_DATA(String, S3DefaultRegion);
  1073. WRITE_DATA(String, S3SessionToken);
  1074. WRITE_DATA(Integer, S3UrlStyle);
  1075. WRITE_DATA(Integer, S3MaxKeys);
  1076. WRITE_DATA(Integer, SendBuf);
  1077. WRITE_DATA(String, SourceAddress);
  1078. WRITE_DATA(String, ProtocolFeatures);
  1079. WRITE_DATA(Bool, SshSimple);
  1080. }
  1081. WRITE_DATA(Integer, ProxyMethod);
  1082. WRITE_DATA(String, ProxyHost);
  1083. WRITE_DATA(Integer, ProxyPort);
  1084. WRITE_DATA(String, ProxyUsername);
  1085. if (ProxyMethod == pmCmd)
  1086. {
  1087. WRITE_DATA_EX(StringRaw, L"ProxyTelnetCommand", ProxyLocalCommand, );
  1088. }
  1089. else
  1090. {
  1091. WRITE_DATA(StringRaw, ProxyTelnetCommand);
  1092. }
  1093. #define WRITE_DATA_CONV_FUNC(X) (((X) + 2) % 3)
  1094. WRITE_DATA_CONV(Integer, L"ProxyDNS", ProxyDNS);
  1095. #undef WRITE_DATA_CONV_FUNC
  1096. WRITE_DATA(Bool, ProxyLocalhost);
  1097. #define WRITE_DATA_CONV_FUNC(X) (2 - (X))
  1098. #define WRITE_BUG(BUG) WRITE_DATA_CONV(Integer, L"Bug" #BUG, Bug[sb##BUG]);
  1099. WRITE_BUG(Ignore1);
  1100. WRITE_BUG(PlainPW1);
  1101. WRITE_BUG(RSA1);
  1102. WRITE_BUG(HMAC2);
  1103. WRITE_BUG(DeriveKey2);
  1104. WRITE_BUG(RSAPad2);
  1105. WRITE_BUG(PKSessID2);
  1106. WRITE_BUG(Rekey2);
  1107. WRITE_BUG(MaxPkt2);
  1108. WRITE_BUG(Ignore2);
  1109. WRITE_BUG(OldGex2);
  1110. WRITE_BUG(WinAdj);
  1111. WRITE_BUG(ChanReq);
  1112. #undef WRITE_BUG
  1113. #undef WRITE_DATA_CONV_FUNC
  1114. Storage->DeleteValue(L"BuggyMAC");
  1115. Storage->DeleteValue(L"AliasGroupList");
  1116. if (PuttyExport)
  1117. {
  1118. WRITE_DATA_EX(String, L"Protocol", GetNormalizedPuttyProtocol(), );
  1119. WRITE_DATA(String, WinTitle);
  1120. }
  1121. if (!PuttyExport)
  1122. {
  1123. WRITE_DATA(String, SftpServer);
  1124. #define WRITE_SFTP_BUG(BUG) WRITE_DATA_EX(Integer, L"SFTP" #BUG "Bug", SFTPBug[sb##BUG], );
  1125. WRITE_SFTP_BUG(Symlink);
  1126. WRITE_SFTP_BUG(SignedTS);
  1127. #undef WRITE_SFTP_BUG
  1128. WRITE_DATA(Integer, SFTPMaxVersion);
  1129. WRITE_DATA(Integer, SFTPMaxPacketSize);
  1130. WRITE_DATA(Integer, SFTPDownloadQueue);
  1131. WRITE_DATA(Integer, SFTPUploadQueue);
  1132. WRITE_DATA(Integer, SFTPListingQueue);
  1133. WRITE_DATA(Integer, SFTPRealPath);
  1134. WRITE_DATA(Integer, Color);
  1135. WRITE_DATA(Bool, Tunnel);
  1136. WRITE_DATA(String, TunnelHostName);
  1137. WRITE_DATA(Integer, TunnelPortNumber);
  1138. WRITE_DATA(String, TunnelUserName);
  1139. WRITE_DATA(String, TunnelPublicKeyFile);
  1140. WRITE_DATA(Integer, TunnelLocalPortNumber);
  1141. WRITE_DATA(Bool, FtpPasvMode);
  1142. WRITE_DATA_EX(Integer, L"FtpForcePasvIp2", FtpForcePasvIp, );
  1143. WRITE_DATA(Integer, FtpUseMlsd);
  1144. WRITE_DATA(String, FtpAccount);
  1145. WRITE_DATA(Integer, FtpPingInterval);
  1146. WRITE_DATA(Integer, FtpPingType);
  1147. WRITE_DATA_EX(Integer, L"FtpTransferActiveImmediately2", FtpTransferActiveImmediately, );
  1148. WRITE_DATA(Integer, Ftps);
  1149. WRITE_DATA(Integer, FtpListAll);
  1150. WRITE_DATA(Integer, FtpHost);
  1151. WRITE_DATA(Integer, FtpWorkFromCwd);
  1152. WRITE_DATA(Bool, SslSessionReuse);
  1153. WRITE_DATA(String, TlsCertificateFile);
  1154. WRITE_DATA(Integer, FtpProxyLogonType);
  1155. WRITE_DATA(Integer, MinTlsVersion);
  1156. WRITE_DATA(Integer, MaxTlsVersion);
  1157. WRITE_DATA(Bool, WebDavLiberalEscaping);
  1158. WRITE_DATA(Bool, IsWorkspace);
  1159. WRITE_DATA(String, Link);
  1160. WRITE_DATA(String, NameOverride);
  1161. WRITE_DATA(String, PuttySettings);
  1162. WRITE_DATA(String, CustomParam1);
  1163. WRITE_DATA(String, CustomParam2);
  1164. }
  1165. // This is for collecting all keys for TSiteRawDialog::AddButtonClick.
  1166. // It should be enough to test for (Default == NULL),
  1167. // the DoNotEncryptPasswords and PuttyExport were added to limit a possible unintended impact.
  1168. bool SaveAll = (Default == NULL) && DoNotEncryptPasswords && !PuttyExport;
  1169. SavePasswords(Storage, PuttyExport, DoNotEncryptPasswords, SaveAll);
  1170. if (PuttyExport)
  1171. {
  1172. WritePuttySettings(Storage, PuttySettings);
  1173. }
  1174. }
  1175. //---------------------------------------------------------------------
  1176. TStrings * __fastcall TSessionData::SaveToOptions(const TSessionData * Default, bool SaveName, bool PuttyExport)
  1177. {
  1178. std::unique_ptr<TStringList> Options(new TStringList());
  1179. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  1180. if (SaveName)
  1181. {
  1182. OptionsStorage->WriteString(L"Name", Name);
  1183. }
  1184. DoSave(OptionsStorage.get(), PuttyExport, Default, true);
  1185. return Options.release();
  1186. }
  1187. //---------------------------------------------------------------------
  1188. void __fastcall TSessionData::Save(THierarchicalStorage * Storage,
  1189. bool PuttyExport, const TSessionData * Default)
  1190. {
  1191. if (Storage->OpenSubKey(InternalStorageKey, true))
  1192. {
  1193. DoSave(Storage, PuttyExport, Default, false);
  1194. Storage->CloseSubKey();
  1195. }
  1196. }
  1197. //---------------------------------------------------------------------
  1198. UnicodeString __fastcall TSessionData::ReadXmlNode(_di_IXMLNode Node, const UnicodeString & Name, const UnicodeString & Default)
  1199. {
  1200. _di_IXMLNode TheNode = Node->ChildNodes->FindNode(Name);
  1201. UnicodeString Result;
  1202. if (TheNode != NULL)
  1203. {
  1204. Result = TheNode->Text.Trim();
  1205. }
  1206. if (Result.IsEmpty())
  1207. {
  1208. Result = Default;
  1209. }
  1210. return Result;
  1211. }
  1212. //---------------------------------------------------------------------
  1213. int __fastcall TSessionData::ReadXmlNode(_di_IXMLNode Node, const UnicodeString & Name, int Default)
  1214. {
  1215. _di_IXMLNode TheNode = Node->ChildNodes->FindNode(Name);
  1216. int Result;
  1217. if (TheNode != NULL)
  1218. {
  1219. Result = StrToIntDef(TheNode->Text.Trim(), Default);
  1220. }
  1221. else
  1222. {
  1223. Result = Default;
  1224. }
  1225. return Result;
  1226. }
  1227. //---------------------------------------------------------------------
  1228. _di_IXMLNode __fastcall TSessionData::FindSettingsNode(_di_IXMLNode Node, const UnicodeString & Name)
  1229. {
  1230. for (int Index = 0; Index < Node->ChildNodes->Count; Index++)
  1231. {
  1232. _di_IXMLNode ChildNode = Node->ChildNodes->Get(Index);
  1233. if (ChildNode->NodeName == L"Setting")
  1234. {
  1235. OleVariant SettingName = ChildNode->GetAttribute(L"name");
  1236. if (SettingName == Name)
  1237. {
  1238. return ChildNode;
  1239. }
  1240. }
  1241. }
  1242. return NULL;
  1243. }
  1244. //---------------------------------------------------------------------
  1245. UnicodeString __fastcall TSessionData::ReadSettingsNode(_di_IXMLNode Node, const UnicodeString & Name, const UnicodeString & Default)
  1246. {
  1247. _di_IXMLNode TheNode = FindSettingsNode(Node, Name);
  1248. UnicodeString Result;
  1249. if (TheNode != NULL)
  1250. {
  1251. Result = TheNode->Text.Trim();
  1252. }
  1253. if (Result.IsEmpty())
  1254. {
  1255. Result = Default;
  1256. }
  1257. return Result;
  1258. }
  1259. //---------------------------------------------------------------------
  1260. int __fastcall TSessionData::ReadSettingsNode(_di_IXMLNode Node, const UnicodeString & Name, int Default)
  1261. {
  1262. _di_IXMLNode TheNode = FindSettingsNode(Node, Name);
  1263. int Result;
  1264. if (TheNode != NULL)
  1265. {
  1266. Result = StrToIntDef(TheNode->Text.Trim(), Default);
  1267. }
  1268. else
  1269. {
  1270. Result = Default;
  1271. }
  1272. return Result;
  1273. }
  1274. //---------------------------------------------------------------------
  1275. void __fastcall TSessionData::ImportFromFilezilla(
  1276. _di_IXMLNode Node, const UnicodeString & Path, _di_IXMLNode SettingsNode)
  1277. {
  1278. Name = UnixIncludeTrailingBackslash(Path) + MakeValidName(ReadXmlNode(Node, L"Name", Name));
  1279. HostName = ReadXmlNode(Node, L"Host", HostName);
  1280. PortNumber = ReadXmlNode(Node, L"Port", PortNumber);
  1281. int AProtocol = ReadXmlNode(Node, L"Protocol", 0);
  1282. // ServerProtocol enum
  1283. switch (AProtocol)
  1284. {
  1285. case 0: // FTP
  1286. default: // UNKNOWN, HTTP, HTTPS, INSECURE_FTP
  1287. FSProtocol = fsFTP;
  1288. break;
  1289. case 1: // SFTP
  1290. FSProtocol = fsSFTP;
  1291. break;
  1292. case 3: // FTPS
  1293. FSProtocol = fsFTP;
  1294. Ftps = ftpsImplicit;
  1295. break;
  1296. case 4: // FTPES
  1297. FSProtocol = fsFTP;
  1298. Ftps = ftpsExplicitTls;
  1299. break;
  1300. }
  1301. // LogonType enum
  1302. int LogonType = ReadXmlNode(Node, L"Logontype", 0);
  1303. if (LogonType == 0) // ANONYMOUS
  1304. {
  1305. UserName = AnonymousUserName;
  1306. Password = AnonymousPassword;
  1307. }
  1308. else
  1309. {
  1310. UserName = ReadXmlNode(Node, L"User", UserName);
  1311. FtpAccount = ReadXmlNode(Node, L"Account", FtpAccount);
  1312. _di_IXMLNode PassNode = Node->ChildNodes->FindNode(L"Pass");
  1313. if (PassNode != NULL)
  1314. {
  1315. UnicodeString APassword = PassNode->Text.Trim();
  1316. OleVariant EncodingValue = PassNode->GetAttribute(L"encoding");
  1317. if (!EncodingValue.IsNull())
  1318. {
  1319. UnicodeString EncodingValueStr = EncodingValue;
  1320. if (SameText(EncodingValueStr, L"base64"))
  1321. {
  1322. TBytes Bytes = DecodeBase64(APassword);
  1323. APassword = TEncoding::UTF8->GetString(Bytes);
  1324. }
  1325. }
  1326. Password = APassword;
  1327. }
  1328. }
  1329. PublicKeyFile = ReadXmlNode(Node, L"Keyfile", PublicKeyFile);
  1330. int DefaultTimeDifference = TimeToSeconds(TimeDifference);
  1331. TimeDifference =
  1332. (double(ReadXmlNode(Node, L"TimezoneOffset", DefaultTimeDifference) / SecsPerDay));
  1333. TimeDifferenceAuto = (TimeDifference == TDateTime());
  1334. UnicodeString PasvMode = ReadXmlNode(Node, L"PasvMode", L"");
  1335. if (SameText(PasvMode, L"MODE_PASSIVE"))
  1336. {
  1337. FtpPasvMode = true;
  1338. }
  1339. else if (SameText(PasvMode, L"MODE_ACTIVE"))
  1340. {
  1341. FtpPasvMode = false;
  1342. }
  1343. UnicodeString EncodingType = ReadXmlNode(Node, L"EncodingType", L"");
  1344. if (SameText(EncodingType, L"Auto"))
  1345. {
  1346. NotUtf = asAuto;
  1347. }
  1348. else if (SameText(EncodingType, L"UTF-8"))
  1349. {
  1350. NotUtf = asOff;
  1351. }
  1352. // todo PostLoginCommands
  1353. Note = ReadXmlNode(Node, L"Comments", Note);
  1354. LocalDirectory = ReadXmlNode(Node, L"LocalDir", LocalDirectory);
  1355. UnicodeString RemoteDir = ReadXmlNode(Node, L"RemoteDir", L"");
  1356. if (!RemoteDir.IsEmpty())
  1357. {
  1358. CutToChar(RemoteDir, L' ', false); // type
  1359. int PrefixSize = StrToIntDef(CutToChar(RemoteDir, L' ', false), 0); // prefix size
  1360. if (PrefixSize > 0)
  1361. {
  1362. RemoteDir.Delete(1, PrefixSize);
  1363. }
  1364. RemoteDirectory = L"/";
  1365. while (!RemoteDir.IsEmpty())
  1366. {
  1367. int SegmentSize = StrToIntDef(CutToChar(RemoteDir, L' ', false), 0);
  1368. UnicodeString Segment = RemoteDir.SubString(1, SegmentSize);
  1369. RemoteDirectory = UnixIncludeTrailingBackslash(RemoteDirectory) + Segment;
  1370. RemoteDir.Delete(1, SegmentSize + 1);
  1371. }
  1372. }
  1373. SynchronizeBrowsing = (ReadXmlNode(Node, L"SyncBrowsing", SynchronizeBrowsing ? 1 : 0) != 0);
  1374. if (SettingsNode != NULL)
  1375. {
  1376. if (UsesSsh && PublicKeyFile.IsEmpty())
  1377. {
  1378. UnicodeString KeyFiles = ReadSettingsNode(SettingsNode, L"SFTP keyfiles", UnicodeString());
  1379. UnicodeString KeyFile = CutToChar(KeyFiles, L'\n', true).Trim();
  1380. KeyFiles = KeyFiles.Trim();
  1381. // If there are more keys, ignore them, as we do not know which one to use
  1382. if (!KeyFile.IsEmpty() && KeyFiles.IsEmpty())
  1383. {
  1384. PublicKeyFile = KeyFile;
  1385. }
  1386. }
  1387. bool BypassProxy = (ReadXmlNode(Node, L"BypassProxy", 0) != 0);
  1388. if (!BypassProxy)
  1389. {
  1390. int FtpProxyType = ReadSettingsNode(SettingsNode, L"FTP Proxy type", -1);
  1391. if (FtpProxyType > 0)
  1392. {
  1393. switch (FtpProxyType)
  1394. {
  1395. case 1:
  1396. FtpProxyLogonType = 2;
  1397. break;
  1398. case 2:
  1399. FtpProxyLogonType = 1;
  1400. break;
  1401. case 3:
  1402. FtpProxyLogonType = 3;
  1403. break;
  1404. case 4:
  1405. // custom
  1406. // TODO: map known sequences to our enumeration
  1407. FtpProxyLogonType = 0;
  1408. break;
  1409. default:
  1410. DebugFail();
  1411. FtpProxyLogonType = 0;
  1412. break;
  1413. }
  1414. ProxyHost = ReadSettingsNode(SettingsNode, L"FTP Proxy host", ProxyHost);
  1415. ProxyUsername = ReadSettingsNode(SettingsNode, L"FTP Proxy user", ProxyUsername);
  1416. ProxyPassword = ReadSettingsNode(SettingsNode, L"FTP Proxy password", ProxyPassword);
  1417. // ProxyPort is not used with FtpProxyLogonType
  1418. }
  1419. else
  1420. {
  1421. int ProxyType = ReadSettingsNode(SettingsNode, L"Proxy type", -1);
  1422. if (ProxyType >= 0)
  1423. {
  1424. switch (ProxyType)
  1425. {
  1426. case 0:
  1427. ProxyMethod = ::pmNone;
  1428. break;
  1429. case 1:
  1430. ProxyMethod = pmHTTP;
  1431. break;
  1432. case 2:
  1433. ProxyMethod = pmSocks5;
  1434. break;
  1435. case 3:
  1436. ProxyMethod = pmSocks4;
  1437. break;
  1438. default:
  1439. DebugFail();
  1440. ProxyMethod = ::pmNone;
  1441. break;
  1442. }
  1443. ProxyHost = ReadSettingsNode(SettingsNode, L"Proxy host", ProxyHost);
  1444. ProxyPort = ReadSettingsNode(SettingsNode, L"Proxy port", ProxyPort);
  1445. ProxyUsername = ReadSettingsNode(SettingsNode, L"Proxy user", ProxyUsername);
  1446. ProxyPassword = ReadSettingsNode(SettingsNode, L"Proxy password", ProxyPassword);
  1447. }
  1448. }
  1449. }
  1450. }
  1451. }
  1452. //---------------------------------------------------------------------
  1453. void __fastcall TSessionData::SavePasswords(THierarchicalStorage * Storage, bool PuttyExport, bool DoNotEncryptPasswords, bool SaveAll)
  1454. {
  1455. if (!Configuration->DisablePasswordStoring && !PuttyExport && (!FPassword.IsEmpty() || SaveAll))
  1456. {
  1457. if (DoNotEncryptPasswords)
  1458. {
  1459. Storage->WriteString(L"PasswordPlain", Password);
  1460. Storage->DeleteValue(L"Password");
  1461. }
  1462. else
  1463. {
  1464. Storage->WriteBinaryDataAsString(L"Password", StronglyRecryptPassword(FPassword, UserName+HostName));
  1465. Storage->DeleteValue(L"PasswordPlain");
  1466. }
  1467. }
  1468. else
  1469. {
  1470. Storage->DeleteValue(L"Password");
  1471. Storage->DeleteValue(L"PasswordPlain");
  1472. }
  1473. if (PuttyExport)
  1474. {
  1475. // save password unencrypted
  1476. Storage->WriteString(L"ProxyPassword", ProxyPassword);
  1477. }
  1478. else
  1479. {
  1480. if (DoNotEncryptPasswords)
  1481. {
  1482. if (!FProxyPassword.IsEmpty() || SaveAll)
  1483. {
  1484. Storage->WriteString(L"ProxyPassword", ProxyPassword);
  1485. }
  1486. else
  1487. {
  1488. Storage->DeleteValue(L"ProxyPassword");
  1489. }
  1490. Storage->DeleteValue(L"ProxyPasswordEnc");
  1491. }
  1492. else
  1493. {
  1494. // save password encrypted
  1495. if (!FProxyPassword.IsEmpty() || SaveAll)
  1496. {
  1497. Storage->WriteBinaryDataAsString(L"ProxyPasswordEnc", StronglyRecryptPassword(FProxyPassword, ProxyUsername+ProxyHost));
  1498. }
  1499. else
  1500. {
  1501. Storage->DeleteValue(L"ProxyPasswordEnc");
  1502. }
  1503. Storage->DeleteValue(L"ProxyPassword");
  1504. }
  1505. if (DoNotEncryptPasswords)
  1506. {
  1507. if (!FTunnelPassword.IsEmpty() || SaveAll)
  1508. {
  1509. Storage->WriteString(L"TunnelPasswordPlain", TunnelPassword);
  1510. }
  1511. else
  1512. {
  1513. Storage->DeleteValue(L"TunnelPasswordPlain");
  1514. }
  1515. }
  1516. else
  1517. {
  1518. if (!Configuration->DisablePasswordStoring && (!FTunnelPassword.IsEmpty() || SaveAll))
  1519. {
  1520. Storage->WriteBinaryDataAsString(L"TunnelPassword", StronglyRecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName));
  1521. }
  1522. else
  1523. {
  1524. Storage->DeleteValue(L"TunnelPassword");
  1525. }
  1526. }
  1527. if (DoNotEncryptPasswords)
  1528. {
  1529. if (!FEncryptKey.IsEmpty() || SaveAll)
  1530. {
  1531. Storage->WriteString(L"EncryptKeyPlain", EncryptKey);
  1532. }
  1533. else
  1534. {
  1535. Storage->DeleteValue(L"EncryptKeyPlain");
  1536. }
  1537. Storage->DeleteValue(L"EncryptKey");
  1538. }
  1539. else
  1540. {
  1541. if (!FEncryptKey.IsEmpty() || SaveAll)
  1542. {
  1543. Storage->WriteBinaryDataAsString(L"EncryptKey", StronglyRecryptPassword(FEncryptKey, UserName+HostName));
  1544. }
  1545. else
  1546. {
  1547. Storage->DeleteValue(L"EncryptKey");
  1548. }
  1549. Storage->DeleteValue(L"EncryptKeyPlain");
  1550. }
  1551. }
  1552. }
  1553. //---------------------------------------------------------------------
  1554. void __fastcall TSessionData::RecryptPasswords()
  1555. {
  1556. Password = Password;
  1557. NewPassword = NewPassword;
  1558. ProxyPassword = ProxyPassword;
  1559. TunnelPassword = TunnelPassword;
  1560. Passphrase = Passphrase;
  1561. EncryptKey = EncryptKey;
  1562. }
  1563. //---------------------------------------------------------------------
  1564. bool __fastcall TSessionData::HasPassword()
  1565. {
  1566. return !FPassword.IsEmpty();
  1567. }
  1568. //---------------------------------------------------------------------
  1569. bool __fastcall TSessionData::HasAnySessionPassword()
  1570. {
  1571. // Keep in sync with ClearSessionPasswords
  1572. return
  1573. HasPassword() ||
  1574. !FTunnelPassword.IsEmpty() ||
  1575. // will probably be never used
  1576. !FNewPassword.IsEmpty();
  1577. }
  1578. //---------------------------------------------------------------------
  1579. bool __fastcall TSessionData::HasAnyPassword()
  1580. {
  1581. // Keep in sync with MaskPasswords
  1582. return
  1583. HasAnySessionPassword() ||
  1584. !FProxyPassword.IsEmpty() ||
  1585. !FEncryptKey.IsEmpty() ||
  1586. !FPassphrase.IsEmpty();
  1587. }
  1588. //---------------------------------------------------------------------
  1589. void __fastcall TSessionData::ClearSessionPasswords()
  1590. {
  1591. // Keep in sync with HasAnySessionPassword
  1592. FPassword = L"";
  1593. FNewPassword = L"";
  1594. FTunnelPassword = L"";
  1595. }
  1596. //---------------------------------------------------------------------
  1597. void __fastcall TSessionData::Modify()
  1598. {
  1599. FModified = true;
  1600. if (FSource == ssStored)
  1601. {
  1602. FSource = ssStoredModified;
  1603. }
  1604. }
  1605. //---------------------------------------------------------------------
  1606. UnicodeString __fastcall TSessionData::GetSource()
  1607. {
  1608. switch (FSource)
  1609. {
  1610. case ::ssNone:
  1611. return L"Ad-Hoc site";
  1612. case ssStored:
  1613. return L"Site";
  1614. case ssStoredModified:
  1615. return L"Modified site";
  1616. default:
  1617. DebugFail();
  1618. return L"";
  1619. }
  1620. }
  1621. //---------------------------------------------------------------------
  1622. void __fastcall TSessionData::SaveRecryptedPasswords(THierarchicalStorage * Storage)
  1623. {
  1624. if (Storage->OpenSubKey(InternalStorageKey, true))
  1625. {
  1626. try
  1627. {
  1628. RecryptPasswords();
  1629. SavePasswords(Storage, false, false, false);
  1630. }
  1631. __finally
  1632. {
  1633. Storage->CloseSubKey();
  1634. }
  1635. }
  1636. }
  1637. //---------------------------------------------------------------------
  1638. void __fastcall TSessionData::Remove(THierarchicalStorage * Storage, const UnicodeString & Name)
  1639. {
  1640. Storage->RecursiveDeleteSubKey(Name);
  1641. }
  1642. //---------------------------------------------------------------------
  1643. void __fastcall TSessionData::Remove()
  1644. {
  1645. bool SessionList = true;
  1646. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  1647. try
  1648. {
  1649. Storage->Explicit = true;
  1650. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  1651. {
  1652. Remove(Storage, InternalStorageKey);
  1653. }
  1654. }
  1655. __finally
  1656. {
  1657. delete Storage;
  1658. }
  1659. }
  1660. //---------------------------------------------------------------------
  1661. void __fastcall TSessionData::CacheHostKeyIfNotCached()
  1662. {
  1663. UnicodeString KeyType = KeyTypeFromFingerprint(HostKey);
  1664. // Should allow importing to INI file as ImportHostKeys
  1665. UnicodeString TargetKey = Configuration->RegistryStorageKey + L"\\" + Configuration->SshHostKeysSubKey;
  1666. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(TargetKey));
  1667. Storage->AccessMode = smReadWrite;
  1668. if (Storage->OpenRootKey(true))
  1669. {
  1670. UnicodeString HostKeyName = PuttyMungeStr(FORMAT(L"%s@%d:%s", (KeyType, PortNumber, HostName)));
  1671. if (!Storage->ValueExists(HostKeyName))
  1672. {
  1673. // fingerprint is a checksum of a host key, so it cannot be translated back to host key,
  1674. // so we store fingerprint and TSecureShell::VerifyHostKey was
  1675. // modified to accept also fingerprint
  1676. Storage->WriteString(HostKeyName, HostKey);
  1677. }
  1678. }
  1679. }
  1680. //---------------------------------------------------------------------
  1681. inline void __fastcall MoveStr(UnicodeString & Source, UnicodeString * Dest, int Count)
  1682. {
  1683. if (Dest != NULL)
  1684. {
  1685. (*Dest) += Source.SubString(1, Count);
  1686. }
  1687. Source.Delete(1, Count);
  1688. }
  1689. //---------------------------------------------------------------------
  1690. bool __fastcall TSessionData::DoIsProtocolUrl(
  1691. const UnicodeString & Url, const UnicodeString & Protocol, int & ProtocolLen)
  1692. {
  1693. bool Result = SameText(Url.SubString(1, Protocol.Length() + 1), Protocol + L":");
  1694. if (Result)
  1695. {
  1696. ProtocolLen = Protocol.Length() + 1;
  1697. }
  1698. return Result;
  1699. }
  1700. //---------------------------------------------------------------------
  1701. bool __fastcall TSessionData::IsProtocolUrl(
  1702. const UnicodeString & Url, const UnicodeString & Protocol, int & ProtocolLen)
  1703. {
  1704. return
  1705. DoIsProtocolUrl(Url, Protocol, ProtocolLen) ||
  1706. DoIsProtocolUrl(Url, WinSCPProtocolPrefix + Protocol, ProtocolLen);
  1707. }
  1708. //---------------------------------------------------------------------
  1709. bool __fastcall TSessionData::IsSensitiveOption(const UnicodeString & Option)
  1710. {
  1711. return
  1712. SameText(Option, PassphraseOption) ||
  1713. SameText(Option, PASSWORD_SWITCH) ||
  1714. SameText(Option, NEWPASSWORD_SWITCH);
  1715. }
  1716. //---------------------------------------------------------------------
  1717. bool __fastcall TSessionData::IsOptionWithParameters(const UnicodeString & Option)
  1718. {
  1719. return SameText(Option, RawSettingsOption);
  1720. }
  1721. //---------------------------------------------------------------------
  1722. bool __fastcall TSessionData::MaskPasswordInOptionParameter(const UnicodeString & Option, UnicodeString & Param)
  1723. {
  1724. bool Result = false;
  1725. if (SameText(Option, RawSettingsOption))
  1726. {
  1727. int P = Param.Pos(L"=");
  1728. if (P > 0)
  1729. {
  1730. // TStrings.IndexOfName does not trim
  1731. UnicodeString Key = Param.SubString(1, P - 1);
  1732. if (SameText(Key, L"ProxyPassword") ||
  1733. SameText(Key, L"ProxyPasswordEnc") ||
  1734. SameText(Key, L"TunnelPassword") ||
  1735. SameText(Key, L"TunnelPasswordPlain") ||
  1736. SameText(Key, L"EncryptKey"))
  1737. {
  1738. Param = Key + L"=" + PasswordMask;
  1739. Result = true;
  1740. }
  1741. }
  1742. }
  1743. return Result;
  1744. }
  1745. //---------------------------------------------------------------------
  1746. void __fastcall TSessionData::MaskPasswords()
  1747. {
  1748. // Keep in sync with HasAnyPassword
  1749. if (!Password.IsEmpty())
  1750. {
  1751. Password = PasswordMask;
  1752. }
  1753. if (!NewPassword.IsEmpty())
  1754. {
  1755. NewPassword = PasswordMask;
  1756. }
  1757. if (!ProxyPassword.IsEmpty())
  1758. {
  1759. ProxyPassword = PasswordMask;
  1760. }
  1761. if (!TunnelPassword.IsEmpty())
  1762. {
  1763. TunnelPassword = PasswordMask;
  1764. }
  1765. if (!EncryptKey.IsEmpty())
  1766. {
  1767. EncryptKey = PasswordMask;
  1768. }
  1769. if (!Passphrase.IsEmpty())
  1770. {
  1771. Passphrase = PasswordMask;
  1772. }
  1773. }
  1774. //---------------------------------------------------------------------
  1775. bool __fastcall TSessionData::ParseUrl(UnicodeString Url, TOptions * Options,
  1776. TStoredSessionList * StoredSessions, bool & DefaultsOnly, UnicodeString * FileName,
  1777. bool * AProtocolDefined, UnicodeString * MaskedUrl, int Flags)
  1778. {
  1779. bool ProtocolDefined = false;
  1780. bool PortNumberDefined = false;
  1781. TFSProtocol AFSProtocol;
  1782. int APortNumber;
  1783. TFtps AFtps = ftpsNone;
  1784. int ProtocolLen = 0;
  1785. if (IsProtocolUrl(Url, ScpProtocol, ProtocolLen))
  1786. {
  1787. AFSProtocol = fsSCPonly;
  1788. APortNumber = SshPortNumber;
  1789. MoveStr(Url, MaskedUrl, ProtocolLen);
  1790. ProtocolDefined = true;
  1791. }
  1792. else if (IsProtocolUrl(Url, SftpProtocol, ProtocolLen))
  1793. {
  1794. AFSProtocol = fsSFTPonly;
  1795. APortNumber = SshPortNumber;
  1796. MoveStr(Url, MaskedUrl, ProtocolLen);
  1797. ProtocolDefined = true;
  1798. }
  1799. else if (IsProtocolUrl(Url, FtpProtocol, ProtocolLen))
  1800. {
  1801. AFSProtocol = fsFTP;
  1802. Ftps = ftpsNone;
  1803. APortNumber = FtpPortNumber;
  1804. MoveStr(Url, MaskedUrl, ProtocolLen);
  1805. ProtocolDefined = true;
  1806. }
  1807. else if (IsProtocolUrl(Url, FtpsProtocol, ProtocolLen))
  1808. {
  1809. AFSProtocol = fsFTP;
  1810. AFtps = ftpsImplicit;
  1811. APortNumber = FtpsImplicitPortNumber;
  1812. MoveStr(Url, MaskedUrl, ProtocolLen);
  1813. ProtocolDefined = true;
  1814. }
  1815. else if (IsProtocolUrl(Url, FtpesProtocol, ProtocolLen))
  1816. {
  1817. AFSProtocol = fsFTP;
  1818. AFtps = ftpsExplicitTls;
  1819. APortNumber = FtpPortNumber;
  1820. MoveStr(Url, MaskedUrl, ProtocolLen);
  1821. ProtocolDefined = true;
  1822. }
  1823. else if (IsProtocolUrl(Url, WebDAVProtocol, ProtocolLen) ||
  1824. IsProtocolUrl(Url, HttpProtocol, ProtocolLen))
  1825. {
  1826. AFSProtocol = fsWebDAV;
  1827. AFtps = ftpsNone;
  1828. APortNumber = HTTPPortNumber;
  1829. MoveStr(Url, MaskedUrl, ProtocolLen);
  1830. ProtocolDefined = true;
  1831. }
  1832. else if (IsProtocolUrl(Url, WebDAVSProtocol, ProtocolLen) ||
  1833. IsProtocolUrl(Url, HttpsProtocol, ProtocolLen))
  1834. {
  1835. AFSProtocol = fsWebDAV;
  1836. AFtps = ftpsImplicit;
  1837. APortNumber = HTTPSPortNumber;
  1838. MoveStr(Url, MaskedUrl, ProtocolLen);
  1839. ProtocolDefined = true;
  1840. }
  1841. else if (IsProtocolUrl(Url, S3Protocol, ProtocolLen))
  1842. {
  1843. AFSProtocol = fsS3;
  1844. AFtps = ftpsImplicit;
  1845. APortNumber = HTTPSPortNumber;
  1846. MoveStr(Url, MaskedUrl, ProtocolLen);
  1847. ProtocolDefined = true;
  1848. }
  1849. else if (IsProtocolUrl(Url, SshProtocol, ProtocolLen))
  1850. {
  1851. // For most uses, handling ssh:// the same way as sftp://
  1852. // The only place where a difference is made is GetLoginData() in WinMain.cpp
  1853. AFSProtocol = fsSFTPonly;
  1854. PuttyProtocol = PuttySshProtocol;
  1855. APortNumber = SshPortNumber;
  1856. MoveStr(Url, MaskedUrl, ProtocolLen);
  1857. ProtocolDefined = true;
  1858. }
  1859. if (ProtocolDefined && (Url.SubString(1, 2) == L"//"))
  1860. {
  1861. MoveStr(Url, MaskedUrl, 2);
  1862. }
  1863. if (AProtocolDefined != NULL)
  1864. {
  1865. *AProtocolDefined = ProtocolDefined;
  1866. }
  1867. bool Unsafe = FLAGSET(Flags, pufUnsafe);
  1868. if (!Url.IsEmpty())
  1869. {
  1870. UnicodeString DecodedUrl = DecodeUrlChars(Url);
  1871. // lookup stored session even if protocol was defined
  1872. // (this allows setting for example default username for host
  1873. // by creating stored session named by host)
  1874. TSessionData * Data = NULL;
  1875. // When using to paste URL on Login dialog, we do not want to lookup the stored sites
  1876. if ((StoredSessions != NULL) &&
  1877. (!ProtocolDefined || FLAGSET(Flags, pufAllowStoredSiteWithProtocol)))
  1878. {
  1879. // this can be optimized as the list is sorted
  1880. for (Integer Index = 0; Index < StoredSessions->CountIncludingHidden; Index++)
  1881. {
  1882. TSessionData * AData = (TSessionData *)StoredSessions->Items[Index];
  1883. if (!AData->IsWorkspace)
  1884. {
  1885. bool Match = false;
  1886. // Comparison optimizations as this is called many times
  1887. // e.g. when updating jumplist
  1888. if ((AData->Name.Length() == DecodedUrl.Length()) &&
  1889. SameText(AData->Name, DecodedUrl))
  1890. {
  1891. Match = true;
  1892. }
  1893. else if ((AData->Name.Length() < DecodedUrl.Length()) &&
  1894. (DecodedUrl[AData->Name.Length() + 1] == L'/') &&
  1895. // StrLIComp is an equivalent of SameText
  1896. (StrLIComp(AData->Name.c_str(), DecodedUrl.c_str(), AData->Name.Length()) == 0))
  1897. {
  1898. Match = true;
  1899. }
  1900. if (Match)
  1901. {
  1902. Data = AData;
  1903. break;
  1904. }
  1905. }
  1906. }
  1907. }
  1908. UnicodeString ARemoteDirectory;
  1909. if (Data != NULL)
  1910. {
  1911. Assign(Data);
  1912. int P = 1;
  1913. while (!AnsiSameText(DecodeUrlChars(Url.SubString(1, P)), Data->Name))
  1914. {
  1915. P++;
  1916. DebugAssert(P <= Url.Length());
  1917. }
  1918. ARemoteDirectory = Url.SubString(P + 1, Url.Length() - P);
  1919. if (Data->Hidden)
  1920. {
  1921. Data->Remove();
  1922. StoredSessions->Remove(Data);
  1923. // only modified, implicit
  1924. StoredSessions->Save(false, false);
  1925. }
  1926. if (MaskedUrl != NULL)
  1927. {
  1928. (*MaskedUrl) += Url;
  1929. }
  1930. }
  1931. else
  1932. {
  1933. // This happens when pasting URL on Login dialog
  1934. if (StoredSessions != NULL)
  1935. {
  1936. CopyData(StoredSessions->DefaultSettings);
  1937. }
  1938. Name = L"";
  1939. int PSlash = Url.Pos(L"/");
  1940. if (PSlash == 0)
  1941. {
  1942. PSlash = Url.Length() + 1;
  1943. }
  1944. UnicodeString ConnectInfo = Url.SubString(1, PSlash - 1);
  1945. int P = ConnectInfo.LastDelimiter(L"@");
  1946. UnicodeString UserInfo;
  1947. UnicodeString HostInfo;
  1948. if (P > 0)
  1949. {
  1950. UserInfo = ConnectInfo.SubString(1, P - 1);
  1951. HostInfo = ConnectInfo.SubString(P + 1, ConnectInfo.Length() - P);
  1952. }
  1953. else
  1954. {
  1955. HostInfo = ConnectInfo;
  1956. }
  1957. UnicodeString OrigHostInfo = HostInfo;
  1958. if ((HostInfo.Length() >= 2) && (HostInfo[1] == L'[') && ((P = HostInfo.Pos(L"]")) > 0))
  1959. {
  1960. HostName = HostInfo.SubString(2, P - 2);
  1961. HostInfo.Delete(1, P);
  1962. if (!HostInfo.IsEmpty() && (HostInfo[1] == L':'))
  1963. {
  1964. HostInfo.Delete(1, 1);
  1965. }
  1966. }
  1967. else
  1968. {
  1969. HostName = DecodeUrlChars(CutToChar(HostInfo, L':', true));
  1970. }
  1971. // expanded from ?: operator, as it caused strange "access violation" errors
  1972. if (!HostInfo.IsEmpty())
  1973. {
  1974. int APortNumber = StrToIntDef(DecodeUrlChars(HostInfo), -1);
  1975. if ((APortNumber > 0) && (APortNumber <= 65535))
  1976. {
  1977. PortNumber = APortNumber;
  1978. PortNumberDefined = true;
  1979. }
  1980. }
  1981. else if (ProtocolDefined)
  1982. {
  1983. PortNumber = APortNumber;
  1984. }
  1985. if (ProtocolDefined)
  1986. {
  1987. Ftps = AFtps;
  1988. }
  1989. UnicodeString UserInfoWithoutConnectionParams = CutToChar(UserInfo, UrlParamSeparator, false);
  1990. UnicodeString ConnectionParams = UserInfo;
  1991. UserInfo = UserInfoWithoutConnectionParams;
  1992. std::unique_ptr<TStrings> RawSettings(new TStringList());
  1993. while (!ConnectionParams.IsEmpty())
  1994. {
  1995. UnicodeString ConnectionParam = CutToChar(ConnectionParams, UrlParamSeparator, false);
  1996. UnicodeString ConnectionParamName = CutToChar(ConnectionParam, UrlParamValueSeparator, false);
  1997. if (SameText(ConnectionParamName, UrlHostKeyParamName))
  1998. {
  1999. HostKey = DecodeUrlChars(ConnectionParam);
  2000. FOverrideCachedHostKey = false;
  2001. }
  2002. else if (StartsText(UrlRawSettingsParamNamePrefix, ConnectionParamName))
  2003. {
  2004. UnicodeString AName = RightStr(ConnectionParamName, ConnectionParamName.Length() - UrlRawSettingsParamNamePrefix.Length());
  2005. AName = DecodeUrlChars(AName);
  2006. UnicodeString Value = DecodeUrlChars(ConnectionParam);
  2007. if (SameText(AName, L"Name"))
  2008. {
  2009. Name = Value;
  2010. }
  2011. else
  2012. {
  2013. RawSettings->Values[AName] = Value;
  2014. }
  2015. }
  2016. }
  2017. if (RawSettings->Count > 0) // optimization
  2018. {
  2019. ApplyRawSettings(RawSettings.get(), FLAGSET(Flags, pufUnsafe));
  2020. }
  2021. bool HasPassword = (UserInfo.Pos(L':') > 0);
  2022. UnicodeString RawUserName = CutToChar(UserInfo, L':', false);
  2023. UserName = DecodeUrlChars(RawUserName);
  2024. Password = DecodeUrlChars(UserInfo);
  2025. if (HasPassword && Password.IsEmpty())
  2026. {
  2027. Password = EmptyString;
  2028. }
  2029. UnicodeString RemoteDirectoryWithSessionParams = Url.SubString(PSlash, Url.Length() - PSlash + 1);
  2030. ARemoteDirectory = CutToChar(RemoteDirectoryWithSessionParams, UrlParamSeparator, false);
  2031. UnicodeString SessionParams = RemoteDirectoryWithSessionParams;
  2032. // We should handle session params in "stored session" branch too.
  2033. // And particularly if there's a "save" param, we should actually not try to match the
  2034. // URL against site names
  2035. while (!SessionParams.IsEmpty())
  2036. {
  2037. UnicodeString SessionParam = CutToChar(SessionParams, UrlParamSeparator, false);
  2038. UnicodeString SessionParamName = CutToChar(SessionParam, UrlParamValueSeparator, false);
  2039. if (SameText(SessionParamName, UrlSaveParamName))
  2040. {
  2041. FSaveOnly = (StrToIntDef(SessionParam, 1) != 0);
  2042. }
  2043. }
  2044. if (MaskedUrl != NULL)
  2045. {
  2046. (*MaskedUrl) += RawUserName;
  2047. if (HasPassword)
  2048. {
  2049. (*MaskedUrl) += L":" + PasswordMask;
  2050. }
  2051. if (!RawUserName.IsEmpty() || HasPassword)
  2052. {
  2053. (*MaskedUrl) += L"@";
  2054. }
  2055. (*MaskedUrl) += OrigHostInfo + ARemoteDirectory;
  2056. }
  2057. }
  2058. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != L"/"))
  2059. {
  2060. if ((ARemoteDirectory[ARemoteDirectory.Length()] != L'/') &&
  2061. (FileName != NULL))
  2062. {
  2063. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  2064. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  2065. }
  2066. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  2067. }
  2068. DefaultsOnly = false;
  2069. }
  2070. else
  2071. {
  2072. // This happens when pasting URL on Login dialog
  2073. if (StoredSessions != NULL)
  2074. {
  2075. CopyData(StoredSessions->DefaultSettings);
  2076. }
  2077. DefaultsOnly = true;
  2078. }
  2079. if (ProtocolDefined)
  2080. {
  2081. FSProtocol = AFSProtocol;
  2082. }
  2083. if (Options != NULL)
  2084. {
  2085. // we deliberately do keep defaultonly to false, in presence of any option,
  2086. // as the option should not make session "connectable"
  2087. UnicodeString Value;
  2088. if (Options->FindSwitch(USERNAME_SWITCH, Value))
  2089. {
  2090. UserName = Value;
  2091. }
  2092. if (Options->FindSwitch(PASSWORD_SWITCH, Value))
  2093. {
  2094. Password = Value;
  2095. }
  2096. if (Options->FindSwitch(SESSIONNAME_SWICH, Value))
  2097. {
  2098. Name = Value;
  2099. }
  2100. if (Options->FindSwitch(NEWPASSWORD_SWITCH, Value))
  2101. {
  2102. ChangePassword = true;
  2103. NewPassword = Value;
  2104. }
  2105. if (Options->FindSwitch(L"privatekey", Value))
  2106. {
  2107. PublicKeyFile = Value;
  2108. }
  2109. if (Options->FindSwitch(L"clientcert", Value))
  2110. {
  2111. TlsCertificateFile = Value;
  2112. }
  2113. if (Options->FindSwitch(PassphraseOption, Value))
  2114. {
  2115. Passphrase = Value;
  2116. }
  2117. if (Options->FindSwitch(L"timeout", Value))
  2118. {
  2119. Timeout = StrToInt(Value);
  2120. }
  2121. if (Options->FindSwitch(L"hostkey", Value) ||
  2122. Options->FindSwitch(L"certificate", Value))
  2123. {
  2124. HostKey = Value;
  2125. FOverrideCachedHostKey = true;
  2126. }
  2127. FtpPasvMode = Options->SwitchValue(L"passive", FtpPasvMode);
  2128. if (Options->FindSwitch(L"implicit"))
  2129. {
  2130. bool Enabled = Options->SwitchValue(L"implicit", true);
  2131. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  2132. if (!PortNumberDefined && Enabled)
  2133. {
  2134. PortNumber = FtpsImplicitPortNumber;
  2135. }
  2136. }
  2137. // BACKWARD COMPATIBILITY with 5.5.x
  2138. if (Options->FindSwitch(L"explicitssl"))
  2139. {
  2140. bool Enabled = Options->SwitchValue(L"explicitssl", true);
  2141. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  2142. if (!PortNumberDefined && Enabled)
  2143. {
  2144. PortNumber = FtpPortNumber;
  2145. }
  2146. }
  2147. if (Options->FindSwitch(L"explicit") ||
  2148. // BACKWARD COMPATIBILITY with 5.5.x
  2149. Options->FindSwitch(L"explicittls"))
  2150. {
  2151. UnicodeString SwitchName =
  2152. Options->FindSwitch(L"explicit") ? L"explicit" : L"explicittls";
  2153. bool Enabled = Options->SwitchValue(SwitchName, true);
  2154. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  2155. if (!PortNumberDefined && Enabled)
  2156. {
  2157. PortNumber = FtpPortNumber;
  2158. }
  2159. }
  2160. if (Options->FindSwitch(RawSettingsOption))
  2161. {
  2162. std::unique_ptr<TStrings> RawSettings(new TStringList());
  2163. if (Options->FindSwitch(RawSettingsOption, RawSettings.get()))
  2164. {
  2165. ApplyRawSettings(RawSettings.get(), Unsafe);
  2166. }
  2167. }
  2168. }
  2169. return true;
  2170. }
  2171. //---------------------------------------------------------------------
  2172. void __fastcall TSessionData::ApplyRawSettings(TStrings * RawSettings, bool Unsafe)
  2173. {
  2174. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawSettings, false));
  2175. ApplyRawSettings(OptionsStorage.get(), Unsafe);
  2176. }
  2177. //---------------------------------------------------------------------
  2178. void __fastcall TSessionData::ApplyRawSettings(THierarchicalStorage * Storage, bool Unsafe)
  2179. {
  2180. bool Dummy;
  2181. DoLoad(Storage, false, Dummy, Unsafe);
  2182. }
  2183. //---------------------------------------------------------------------
  2184. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  2185. {
  2186. FOrigHostName = HostName;
  2187. FOrigPortNumber = PortNumber;
  2188. FOrigProxyMethod = ProxyMethod;
  2189. HostName = L"127.0.0.1";
  2190. PortNumber = APortNumber;
  2191. // proxy settings is used for tunnel
  2192. ProxyMethod = ::pmNone;
  2193. FLogicalHostName = FOrigHostName;
  2194. }
  2195. //---------------------------------------------------------------------
  2196. void __fastcall TSessionData::RollbackTunnel()
  2197. {
  2198. HostName = FOrigHostName;
  2199. PortNumber = FOrigPortNumber;
  2200. ProxyMethod = FOrigProxyMethod;
  2201. FLogicalHostName = L"";
  2202. }
  2203. //---------------------------------------------------------------------
  2204. void __fastcall TSessionData::ExpandEnvironmentVariables()
  2205. {
  2206. HostName = HostNameExpanded;
  2207. UserName = UserNameExpanded;
  2208. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  2209. }
  2210. //---------------------------------------------------------------------
  2211. void __fastcall TSessionData::ValidatePath(const UnicodeString Path)
  2212. {
  2213. // noop
  2214. }
  2215. //---------------------------------------------------------------------
  2216. void __fastcall TSessionData::ValidateName(const UnicodeString Name)
  2217. {
  2218. // keep consistent with MakeValidName
  2219. if (Name.LastDelimiter(L"/") > 0)
  2220. {
  2221. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, L"/")));
  2222. }
  2223. }
  2224. //---------------------------------------------------------------------
  2225. UnicodeString __fastcall TSessionData::MakeValidName(const UnicodeString & Name)
  2226. {
  2227. // keep consistent with ValidateName
  2228. return ReplaceStr(Name, L"/", L"\\");
  2229. }
  2230. //---------------------------------------------------------------------
  2231. RawByteString __fastcall TSessionData::EncryptPassword(const UnicodeString & Password, UnicodeString Key)
  2232. {
  2233. return Configuration->EncryptPassword(Password, Key);
  2234. }
  2235. //---------------------------------------------------------------------
  2236. RawByteString __fastcall TSessionData::StronglyRecryptPassword(const RawByteString & Password, UnicodeString Key)
  2237. {
  2238. return Configuration->StronglyRecryptPassword(Password, Key);
  2239. }
  2240. //---------------------------------------------------------------------
  2241. UnicodeString __fastcall TSessionData::DecryptPassword(const RawByteString & Password, UnicodeString Key)
  2242. {
  2243. UnicodeString Result;
  2244. try
  2245. {
  2246. Result = Configuration->DecryptPassword(Password, Key);
  2247. }
  2248. catch(EAbort &)
  2249. {
  2250. // silently ignore aborted prompts for master password and return empty password
  2251. }
  2252. return Result;
  2253. }
  2254. //---------------------------------------------------------------------
  2255. bool __fastcall TSessionData::GetCanLogin()
  2256. {
  2257. return !FHostName.IsEmpty();
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. UnicodeString __fastcall TSessionData::GetSessionKey()
  2261. {
  2262. UnicodeString Result = FORMAT(L"%s@%s", (UserName, HostName));
  2263. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  2264. {
  2265. Result += FORMAT(L":%d", (PortNumber));
  2266. }
  2267. return Result;
  2268. }
  2269. //---------------------------------------------------------------------
  2270. UnicodeString __fastcall TSessionData::GetInternalStorageKey()
  2271. {
  2272. // This is probably useless remnant of previous use of this method from OpenSessionInPutty
  2273. // that needs the method to return something even for ad-hoc sessions
  2274. if (Name.IsEmpty())
  2275. {
  2276. return SessionKey;
  2277. }
  2278. else
  2279. {
  2280. return Name;
  2281. }
  2282. }
  2283. //---------------------------------------------------------------------
  2284. UnicodeString __fastcall TSessionData::GetStorageKey()
  2285. {
  2286. return SessionName;
  2287. }
  2288. //---------------------------------------------------------------------
  2289. UnicodeString __fastcall TSessionData::FormatSiteKey(const UnicodeString & HostName, int PortNumber)
  2290. {
  2291. return FORMAT(L"%s:%d", (HostName, PortNumber));
  2292. }
  2293. //---------------------------------------------------------------------
  2294. UnicodeString __fastcall TSessionData::GetSiteKey()
  2295. {
  2296. return FormatSiteKey(HostNameExpanded, PortNumber);
  2297. }
  2298. //---------------------------------------------------------------------
  2299. void __fastcall TSessionData::SetHostName(UnicodeString value)
  2300. {
  2301. if (FHostName != value)
  2302. {
  2303. // HostName is key for password encryption
  2304. UnicodeString XPassword = Password;
  2305. UnicodeString XNewPassword = NewPassword;
  2306. UnicodeString XEncryptKey = EncryptKey;
  2307. // This is now hardly used as hostname is parsed directly on login dialog.
  2308. // But can be used when importing sites from PuTTY, as it allows same format too.
  2309. int P = value.LastDelimiter(L"@");
  2310. if (P > 0)
  2311. {
  2312. UserName = value.SubString(1, P - 1);
  2313. value = value.SubString(P + 1, value.Length() - P);
  2314. }
  2315. FHostName = value;
  2316. Modify();
  2317. Password = XPassword;
  2318. NewPassword = XNewPassword;
  2319. EncryptKey = XEncryptKey;
  2320. Shred(XPassword);
  2321. Shred(XNewPassword);
  2322. Shred(XEncryptKey);
  2323. }
  2324. }
  2325. //---------------------------------------------------------------------
  2326. UnicodeString __fastcall TSessionData::GetHostNameExpanded()
  2327. {
  2328. return ::ExpandEnvironmentVariables(HostName);
  2329. }
  2330. //---------------------------------------------------------------------
  2331. UnicodeString TSessionData::GetHostNameSource()
  2332. {
  2333. UnicodeString Result;
  2334. if (HostName != HostNameExpanded)
  2335. {
  2336. Result = HostName;
  2337. }
  2338. return Result;
  2339. }
  2340. //---------------------------------------------------------------------
  2341. void __fastcall TSessionData::SetPortNumber(int value)
  2342. {
  2343. SET_SESSION_PROPERTY(PortNumber);
  2344. }
  2345. //---------------------------------------------------------------------------
  2346. void __fastcall TSessionData::SetShell(UnicodeString value)
  2347. {
  2348. SET_SESSION_PROPERTY(Shell);
  2349. }
  2350. //---------------------------------------------------------------------------
  2351. void __fastcall TSessionData::SetSftpServer(UnicodeString value)
  2352. {
  2353. SET_SESSION_PROPERTY(SftpServer);
  2354. }
  2355. //---------------------------------------------------------------------------
  2356. void __fastcall TSessionData::SetClearAliases(bool value)
  2357. {
  2358. SET_SESSION_PROPERTY(ClearAliases);
  2359. }
  2360. //---------------------------------------------------------------------------
  2361. void __fastcall TSessionData::SetListingCommand(UnicodeString value)
  2362. {
  2363. SET_SESSION_PROPERTY(ListingCommand);
  2364. }
  2365. //---------------------------------------------------------------------------
  2366. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  2367. {
  2368. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  2369. }
  2370. //---------------------------------------------------------------------------
  2371. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  2372. {
  2373. SET_SESSION_PROPERTY(UnsetNationalVars);
  2374. }
  2375. //---------------------------------------------------------------------
  2376. void __fastcall TSessionData::SetUserName(UnicodeString value)
  2377. {
  2378. // Avoid password recryption (what may popup master password prompt)
  2379. if (FUserName != value)
  2380. {
  2381. // UserName is key for password encryption
  2382. UnicodeString XPassword = Password;
  2383. UnicodeString XNewPassword = NewPassword;
  2384. UnicodeString XEncryptKey = EncryptKey;
  2385. SET_SESSION_PROPERTY(UserName);
  2386. Password = XPassword;
  2387. NewPassword = XNewPassword;
  2388. EncryptKey = XEncryptKey;
  2389. Shred(XPassword);
  2390. Shred(XNewPassword);
  2391. Shred(XEncryptKey);
  2392. }
  2393. }
  2394. //---------------------------------------------------------------------
  2395. UnicodeString __fastcall TSessionData::GetUserNameExpanded()
  2396. {
  2397. return ::ExpandEnvironmentVariables(UserName);
  2398. }
  2399. //---------------------------------------------------------------------
  2400. UnicodeString TSessionData::GetUserNameSource()
  2401. {
  2402. UnicodeString Result;
  2403. if (UserName != UserNameExpanded)
  2404. {
  2405. Result = UserName;
  2406. }
  2407. return Result;
  2408. }
  2409. //---------------------------------------------------------------------
  2410. void __fastcall TSessionData::SetPassword(UnicodeString avalue)
  2411. {
  2412. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2413. SET_SESSION_PROPERTY(Password);
  2414. }
  2415. //---------------------------------------------------------------------
  2416. UnicodeString __fastcall TSessionData::GetPassword() const
  2417. {
  2418. return DecryptPassword(FPassword, UserName+HostName);
  2419. }
  2420. //---------------------------------------------------------------------
  2421. void __fastcall TSessionData::SetNewPassword(UnicodeString avalue)
  2422. {
  2423. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  2424. SET_SESSION_PROPERTY(NewPassword);
  2425. }
  2426. //---------------------------------------------------------------------
  2427. UnicodeString __fastcall TSessionData::GetNewPassword() const
  2428. {
  2429. return DecryptPassword(FNewPassword, UserName+HostName);
  2430. }
  2431. //---------------------------------------------------------------------
  2432. void __fastcall TSessionData::SetChangePassword(bool value)
  2433. {
  2434. SET_SESSION_PROPERTY(ChangePassword);
  2435. }
  2436. //---------------------------------------------------------------------
  2437. void __fastcall TSessionData::SetPingInterval(int value)
  2438. {
  2439. SET_SESSION_PROPERTY(PingInterval);
  2440. }
  2441. //---------------------------------------------------------------------
  2442. void __fastcall TSessionData::SetTryAgent(bool value)
  2443. {
  2444. SET_SESSION_PROPERTY(TryAgent);
  2445. }
  2446. //---------------------------------------------------------------------
  2447. void __fastcall TSessionData::SetAgentFwd(bool value)
  2448. {
  2449. SET_SESSION_PROPERTY(AgentFwd);
  2450. }
  2451. //---------------------------------------------------------------------
  2452. void __fastcall TSessionData::SetAuthTIS(bool value)
  2453. {
  2454. SET_SESSION_PROPERTY(AuthTIS);
  2455. }
  2456. //---------------------------------------------------------------------
  2457. void __fastcall TSessionData::SetAuthKI(bool value)
  2458. {
  2459. SET_SESSION_PROPERTY(AuthKI);
  2460. }
  2461. //---------------------------------------------------------------------
  2462. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  2463. {
  2464. SET_SESSION_PROPERTY(AuthKIPassword);
  2465. }
  2466. //---------------------------------------------------------------------
  2467. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  2468. {
  2469. SET_SESSION_PROPERTY(AuthGSSAPI);
  2470. }
  2471. //---------------------------------------------------------------------
  2472. void __fastcall TSessionData::SetAuthGSSAPIKEX(bool value)
  2473. {
  2474. SET_SESSION_PROPERTY(AuthGSSAPIKEX);
  2475. }
  2476. //---------------------------------------------------------------------
  2477. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  2478. {
  2479. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  2480. }
  2481. //---------------------------------------------------------------------
  2482. void __fastcall TSessionData::SetChangeUsername(bool value)
  2483. {
  2484. SET_SESSION_PROPERTY(ChangeUsername);
  2485. }
  2486. //---------------------------------------------------------------------
  2487. void __fastcall TSessionData::SetCompression(bool value)
  2488. {
  2489. SET_SESSION_PROPERTY(Compression);
  2490. }
  2491. //---------------------------------------------------------------------
  2492. void __fastcall TSessionData::SetSshProt(TSshProt value)
  2493. {
  2494. SET_SESSION_PROPERTY(SshProt);
  2495. }
  2496. //---------------------------------------------------------------------
  2497. void __fastcall TSessionData::SetSsh2DES(bool value)
  2498. {
  2499. SET_SESSION_PROPERTY(Ssh2DES);
  2500. }
  2501. //---------------------------------------------------------------------
  2502. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  2503. {
  2504. SET_SESSION_PROPERTY(SshNoUserAuth);
  2505. }
  2506. //---------------------------------------------------------------------
  2507. UnicodeString __fastcall TSessionData::GetSshProtStr()
  2508. {
  2509. return SshProtList[FSshProt];
  2510. }
  2511. //---------------------------------------------------------------------
  2512. bool __fastcall TSessionData::GetUsesSsh()
  2513. {
  2514. return IsSshProtocol(FSProtocol);
  2515. }
  2516. //---------------------------------------------------------------------
  2517. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  2518. {
  2519. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2520. SET_SESSION_PROPERTY(Ciphers[Index]);
  2521. }
  2522. //---------------------------------------------------------------------
  2523. TCipher __fastcall TSessionData::GetCipher(int Index) const
  2524. {
  2525. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  2526. return FCiphers[Index];
  2527. }
  2528. //---------------------------------------------------------------------
  2529. template<class AlgoT>
  2530. void __fastcall TSessionData::SetAlgoList(AlgoT * List, const AlgoT * DefaultList, const UnicodeString * Names,
  2531. int Count, AlgoT WarnAlgo, UnicodeString value)
  2532. {
  2533. std::vector<bool> Used(Count); // initialized to false
  2534. std::vector<AlgoT> NewList(Count);
  2535. bool HasWarnAlgo = (WarnAlgo >= AlgoT());
  2536. const AlgoT * WarnPtr;
  2537. int WarnDefaultIndex;
  2538. if (!HasWarnAlgo)
  2539. {
  2540. WarnPtr = NULL;
  2541. WarnDefaultIndex = -1;
  2542. }
  2543. else
  2544. {
  2545. WarnPtr = std::find(DefaultList, DefaultList + Count, WarnAlgo);
  2546. DebugAssert(WarnPtr != NULL);
  2547. WarnDefaultIndex = (WarnPtr - DefaultList);
  2548. }
  2549. int Index = 0;
  2550. while (!value.IsEmpty())
  2551. {
  2552. UnicodeString AlgoStr = CutToChar(value, L',', true);
  2553. for (int Algo = 0; Algo < Count; Algo++)
  2554. {
  2555. if (!AlgoStr.CompareIC(Names[Algo]) &&
  2556. !Used[Algo] && DebugAlwaysTrue(Index < Count))
  2557. {
  2558. NewList[Index] = (AlgoT)Algo;
  2559. Used[Algo] = true;
  2560. Index++;
  2561. break;
  2562. }
  2563. }
  2564. }
  2565. if (HasWarnAlgo && !Used[WarnAlgo] && DebugAlwaysTrue(Index < Count))
  2566. {
  2567. NewList[Index] = WarnAlgo;
  2568. Used[WarnAlgo] = true;
  2569. Index++;
  2570. }
  2571. int WarnIndex = -1;
  2572. if (HasWarnAlgo)
  2573. {
  2574. WarnIndex = std::find(NewList.begin(), NewList.end(), WarnAlgo) - NewList.begin();
  2575. }
  2576. bool Priority = true;
  2577. for (int DefaultIndex = 0; (DefaultIndex < Count); DefaultIndex++)
  2578. {
  2579. AlgoT DefaultAlgo = DefaultList[DefaultIndex];
  2580. if (!Used[DefaultAlgo] && DebugAlwaysTrue(Index < Count))
  2581. {
  2582. int TargetIndex;
  2583. // Unused algs that are prioritized in the default list,
  2584. // should be merged before the existing custom list
  2585. if (Priority)
  2586. {
  2587. TargetIndex = DefaultIndex;
  2588. }
  2589. else
  2590. {
  2591. if (HasWarnAlgo && (DefaultIndex < WarnDefaultIndex))
  2592. {
  2593. TargetIndex = WarnIndex;
  2594. }
  2595. else
  2596. {
  2597. TargetIndex = Index;
  2598. }
  2599. }
  2600. NewList.insert(NewList.begin() + TargetIndex, DefaultAlgo);
  2601. DebugAssert(NewList.back() == AlgoT());
  2602. NewList.pop_back();
  2603. if (HasWarnAlgo && (TargetIndex <= WarnIndex))
  2604. {
  2605. WarnIndex++;
  2606. }
  2607. Index++;
  2608. }
  2609. else
  2610. {
  2611. Priority = false;
  2612. }
  2613. }
  2614. if (!std::equal(NewList.begin(), NewList.end(), List))
  2615. {
  2616. std::copy(NewList.begin(), NewList.end(), List);
  2617. Modify();
  2618. }
  2619. }
  2620. //---------------------------------------------------------------------
  2621. void __fastcall TSessionData::SetCipherList(UnicodeString value)
  2622. {
  2623. SetAlgoList(FCiphers, DefaultCipherList, CipherNames, CIPHER_COUNT, cipWarn, value);
  2624. }
  2625. //---------------------------------------------------------------------
  2626. UnicodeString __fastcall TSessionData::GetCipherList() const
  2627. {
  2628. UnicodeString Result;
  2629. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  2630. {
  2631. Result += UnicodeString(Index ? L"," : L"") + CipherNames[Cipher[Index]];
  2632. }
  2633. return Result;
  2634. }
  2635. //---------------------------------------------------------------------
  2636. void __fastcall TSessionData::SetKex(int Index, TKex value)
  2637. {
  2638. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2639. SET_SESSION_PROPERTY(Kex[Index]);
  2640. }
  2641. //---------------------------------------------------------------------
  2642. TKex __fastcall TSessionData::GetKex(int Index) const
  2643. {
  2644. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  2645. return FKex[Index];
  2646. }
  2647. //---------------------------------------------------------------------
  2648. void __fastcall TSessionData::SetKexList(UnicodeString value)
  2649. {
  2650. SetAlgoList(FKex, DefaultKexList, KexNames, KEX_COUNT, kexWarn, value);
  2651. }
  2652. //---------------------------------------------------------------------
  2653. UnicodeString __fastcall TSessionData::GetKexList() const
  2654. {
  2655. UnicodeString Result;
  2656. for (int Index = 0; Index < KEX_COUNT; Index++)
  2657. {
  2658. Result += UnicodeString(Index ? L"," : L"") + KexNames[Kex[Index]];
  2659. }
  2660. return Result;
  2661. }
  2662. //---------------------------------------------------------------------
  2663. void __fastcall TSessionData::SetHostKeys(int Index, THostKey value)
  2664. {
  2665. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2666. SET_SESSION_PROPERTY(HostKeys[Index]);
  2667. }
  2668. //---------------------------------------------------------------------
  2669. THostKey __fastcall TSessionData::GetHostKeys(int Index) const
  2670. {
  2671. DebugAssert(Index >= 0 && Index < HOSTKEY_COUNT);
  2672. return FHostKeys[Index];
  2673. }
  2674. //---------------------------------------------------------------------
  2675. void __fastcall TSessionData::SetHostKeyList(UnicodeString value)
  2676. {
  2677. SetAlgoList(FHostKeys, DefaultHostKeyList, HostKeyNames, HOSTKEY_COUNT, hkWarn, value);
  2678. }
  2679. //---------------------------------------------------------------------
  2680. UnicodeString __fastcall TSessionData::GetHostKeyList() const
  2681. {
  2682. UnicodeString Result;
  2683. for (int Index = 0; Index < HOSTKEY_COUNT; Index++)
  2684. {
  2685. Result += UnicodeString(Index ? L"," : L"") + HostKeyNames[HostKeys[Index]];
  2686. }
  2687. return Result;
  2688. }
  2689. //---------------------------------------------------------------------
  2690. void __fastcall TSessionData::SetGssLib(int Index, TGssLib value)
  2691. {
  2692. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2693. SET_SESSION_PROPERTY(GssLib[Index]);
  2694. }
  2695. //---------------------------------------------------------------------
  2696. TGssLib __fastcall TSessionData::GetGssLib(int Index) const
  2697. {
  2698. DebugAssert(Index >= 0 && Index < GSSLIB_COUNT);
  2699. return FGssLib[Index];
  2700. }
  2701. //---------------------------------------------------------------------
  2702. void __fastcall TSessionData::SetGssLibList(UnicodeString value)
  2703. {
  2704. SetAlgoList(FGssLib, DefaultGssLibList, GssLibNames, GSSLIB_COUNT, TGssLib(-1), value);
  2705. }
  2706. //---------------------------------------------------------------------
  2707. UnicodeString __fastcall TSessionData::GetGssLibList() const
  2708. {
  2709. UnicodeString Result;
  2710. for (int Index = 0; Index < GSSLIB_COUNT; Index++)
  2711. {
  2712. Result += UnicodeString(Index ? L"," : L"") + GssLibNames[GssLib[Index]];
  2713. }
  2714. return Result;
  2715. }
  2716. //---------------------------------------------------------------------
  2717. void __fastcall TSessionData::SetGssLibCustom(UnicodeString value)
  2718. {
  2719. SET_SESSION_PROPERTY(GssLibCustom);
  2720. }
  2721. //---------------------------------------------------------------------
  2722. void __fastcall TSessionData::SetPublicKeyFile(UnicodeString value)
  2723. {
  2724. if (FPublicKeyFile != value)
  2725. {
  2726. // PublicKeyFile is key for Passphrase encryption
  2727. UnicodeString XPassphrase = Passphrase;
  2728. // StripPathQuotes should not be needed as we do not feed quotes anymore
  2729. FPublicKeyFile = StripPathQuotes(value);
  2730. Modify();
  2731. Passphrase = XPassphrase;
  2732. Shred(XPassphrase);
  2733. }
  2734. }
  2735. //---------------------------------------------------------------------
  2736. void __fastcall TSessionData::SetPassphrase(UnicodeString avalue)
  2737. {
  2738. RawByteString value = EncryptPassword(avalue, PublicKeyFile);
  2739. SET_SESSION_PROPERTY(Passphrase);
  2740. }
  2741. //---------------------------------------------------------------------
  2742. UnicodeString __fastcall TSessionData::GetPassphrase() const
  2743. {
  2744. return DecryptPassword(FPassphrase, PublicKeyFile);
  2745. }
  2746. //---------------------------------------------------------------------
  2747. void __fastcall TSessionData::SetReturnVar(UnicodeString value)
  2748. {
  2749. SET_SESSION_PROPERTY(ReturnVar);
  2750. }
  2751. //---------------------------------------------------------------------
  2752. void __fastcall TSessionData::SetExitCode1IsError(bool value)
  2753. {
  2754. SET_SESSION_PROPERTY(ExitCode1IsError);
  2755. }
  2756. //---------------------------------------------------------------------------
  2757. void __fastcall TSessionData::SetLookupUserGroups(TAutoSwitch value)
  2758. {
  2759. SET_SESSION_PROPERTY(LookupUserGroups);
  2760. }
  2761. //---------------------------------------------------------------------------
  2762. void __fastcall TSessionData::SetEOLType(TEOLType value)
  2763. {
  2764. SET_SESSION_PROPERTY(EOLType);
  2765. }
  2766. //---------------------------------------------------------------------------
  2767. void __fastcall TSessionData::SetTrimVMSVersions(bool value)
  2768. {
  2769. SET_SESSION_PROPERTY(TrimVMSVersions);
  2770. }
  2771. //---------------------------------------------------------------------------
  2772. TDateTime __fastcall TSessionData::GetTimeoutDT()
  2773. {
  2774. return SecToDateTime(Timeout);
  2775. }
  2776. //---------------------------------------------------------------------------
  2777. void __fastcall TSessionData::SetTimeout(int value)
  2778. {
  2779. SET_SESSION_PROPERTY(Timeout);
  2780. }
  2781. //---------------------------------------------------------------------------
  2782. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  2783. {
  2784. SET_SESSION_PROPERTY(FSProtocol);
  2785. }
  2786. //---------------------------------------------------------------------
  2787. UnicodeString __fastcall TSessionData::GetFSProtocolStr()
  2788. {
  2789. DebugAssert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  2790. return FSProtocolNames[FSProtocol];
  2791. }
  2792. //---------------------------------------------------------------------------
  2793. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  2794. {
  2795. if (value != DetectReturnVar)
  2796. {
  2797. ReturnVar = value ? L"" : L"$?";
  2798. }
  2799. }
  2800. //---------------------------------------------------------------------------
  2801. bool __fastcall TSessionData::GetDetectReturnVar()
  2802. {
  2803. return ReturnVar.IsEmpty();
  2804. }
  2805. //---------------------------------------------------------------------------
  2806. void __fastcall TSessionData::SetDefaultShell(bool value)
  2807. {
  2808. if (value != DefaultShell)
  2809. {
  2810. Shell = value ? L"" : L"/bin/bash";
  2811. }
  2812. }
  2813. //---------------------------------------------------------------------------
  2814. bool __fastcall TSessionData::GetDefaultShell()
  2815. {
  2816. return Shell.IsEmpty();
  2817. }
  2818. //---------------------------------------------------------------------------
  2819. void __fastcall TSessionData::SetPuttyProtocol(UnicodeString value)
  2820. {
  2821. SET_SESSION_PROPERTY(PuttyProtocol);
  2822. }
  2823. //---------------------------------------------------------------------
  2824. UnicodeString __fastcall TSessionData::GetNormalizedPuttyProtocol() const
  2825. {
  2826. return DefaultStr(PuttyProtocol, PuttySshProtocol);
  2827. }
  2828. //---------------------------------------------------------------------
  2829. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  2830. {
  2831. unsigned short hour, min, sec, msec;
  2832. value.DecodeTime(&hour, &min, &sec, &msec);
  2833. PingInterval = ((int)hour)*SecsPerHour + ((int)min)*SecsPerMin + sec;
  2834. }
  2835. //---------------------------------------------------------------------------
  2836. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  2837. {
  2838. return SecToDateTime(PingInterval);
  2839. }
  2840. //---------------------------------------------------------------------------
  2841. void __fastcall TSessionData::SetPingType(TPingType value)
  2842. {
  2843. SET_SESSION_PROPERTY(PingType);
  2844. }
  2845. //---------------------------------------------------------------------------
  2846. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  2847. {
  2848. SET_SESSION_PROPERTY(AddressFamily);
  2849. }
  2850. //---------------------------------------------------------------------------
  2851. void __fastcall TSessionData::SetRekeyData(UnicodeString value)
  2852. {
  2853. SET_SESSION_PROPERTY(RekeyData);
  2854. }
  2855. //---------------------------------------------------------------------------
  2856. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  2857. {
  2858. SET_SESSION_PROPERTY(RekeyTime);
  2859. }
  2860. //---------------------------------------------------------------------
  2861. UnicodeString __fastcall TSessionData::GetDefaultSessionName()
  2862. {
  2863. UnicodeString Result;
  2864. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  2865. {
  2866. // If we ever choose to include port number,
  2867. // we have to escape IPv6 literals in HostName
  2868. Result = FORMAT(L"%s@%s", (UserName, HostName));
  2869. }
  2870. else if (!HostName.IsEmpty())
  2871. {
  2872. Result = HostName;
  2873. }
  2874. else
  2875. {
  2876. Result = L"session";
  2877. }
  2878. Result = MakeValidName(Result);
  2879. return Result;
  2880. }
  2881. //---------------------------------------------------------------------
  2882. UnicodeString __fastcall TSessionData::GetNameWithoutHiddenPrefix()
  2883. {
  2884. UnicodeString Result = Name;
  2885. if (Hidden)
  2886. {
  2887. Result = Result.SubString(TNamedObjectList::HiddenPrefix.Length() + 1, Result.Length() - TNamedObjectList::HiddenPrefix.Length());
  2888. }
  2889. return Result;
  2890. }
  2891. //---------------------------------------------------------------------
  2892. bool __fastcall TSessionData::HasSessionName()
  2893. {
  2894. return (!GetNameWithoutHiddenPrefix().IsEmpty() && (Name != DefaultName));
  2895. }
  2896. //---------------------------------------------------------------------
  2897. UnicodeString __fastcall TSessionData::GetSessionName()
  2898. {
  2899. UnicodeString Result;
  2900. if (HasSessionName())
  2901. {
  2902. Result = GetNameWithoutHiddenPrefix();
  2903. }
  2904. else
  2905. {
  2906. Result = DefaultSessionName;
  2907. }
  2908. return Result;
  2909. }
  2910. //---------------------------------------------------------------------
  2911. bool __fastcall TSessionData::IsSecure()
  2912. {
  2913. bool Result;
  2914. switch (FSProtocol)
  2915. {
  2916. case fsSCPonly:
  2917. case fsSFTP:
  2918. case fsSFTPonly:
  2919. Result = true;
  2920. break;
  2921. case fsFTP:
  2922. case fsWebDAV:
  2923. case fsS3:
  2924. Result = (Ftps != ftpsNone);
  2925. break;
  2926. default:
  2927. DebugFail();
  2928. break;
  2929. }
  2930. return Result;
  2931. }
  2932. //---------------------------------------------------------------------
  2933. UnicodeString __fastcall TSessionData::GetProtocolUrl(bool HttpForWebDAV)
  2934. {
  2935. UnicodeString Url;
  2936. switch (FSProtocol)
  2937. {
  2938. case fsSCPonly:
  2939. Url = ScpProtocol;
  2940. break;
  2941. default:
  2942. DebugFail();
  2943. // fallback
  2944. case fsSFTP:
  2945. case fsSFTPonly:
  2946. Url = SftpProtocol;
  2947. break;
  2948. case fsFTP:
  2949. if (Ftps == ftpsImplicit)
  2950. {
  2951. Url = FtpsProtocol;
  2952. }
  2953. else if ((Ftps == ftpsExplicitTls) || (Ftps == ftpsExplicitSsl))
  2954. {
  2955. Url = FtpesProtocol;
  2956. }
  2957. else
  2958. {
  2959. Url = FtpProtocol;
  2960. }
  2961. break;
  2962. case fsWebDAV:
  2963. if (HttpForWebDAV)
  2964. {
  2965. if (Ftps == ftpsImplicit)
  2966. {
  2967. Url = HttpsProtocol;
  2968. }
  2969. else
  2970. {
  2971. Url = HttpProtocol;
  2972. }
  2973. }
  2974. else
  2975. {
  2976. if (Ftps == ftpsImplicit)
  2977. {
  2978. Url = WebDAVSProtocol;
  2979. }
  2980. else
  2981. {
  2982. Url = WebDAVProtocol;
  2983. }
  2984. }
  2985. break;
  2986. case fsS3:
  2987. Url = S3Protocol;
  2988. break;
  2989. }
  2990. Url += ProtocolSeparator;
  2991. return Url;
  2992. }
  2993. //---------------------------------------------------------------------
  2994. static bool HasIP6LiteralBrackets(const UnicodeString & HostName)
  2995. {
  2996. return
  2997. (HostName.Length() >= 2) &&
  2998. (HostName[1] == L'[') &&
  2999. (HostName[HostName.Length()] == L']');
  3000. }
  3001. //---------------------------------------------------------------------
  3002. static UnicodeString StripIP6LiteralBrackets(const UnicodeString & HostName)
  3003. {
  3004. UnicodeString Result = HostName;
  3005. if (DebugAlwaysTrue(HasIP6LiteralBrackets(Result)))
  3006. {
  3007. Result = Result.SubString(2, Result.Length() - 2);
  3008. }
  3009. return Result;
  3010. }
  3011. //---------------------------------------------------------------------
  3012. bool __fastcall IsIPv6Literal(const UnicodeString & HostName)
  3013. {
  3014. UnicodeString Buf = HostName;
  3015. if (HasIP6LiteralBrackets(Buf))
  3016. {
  3017. Buf = StripIP6LiteralBrackets(Buf);
  3018. }
  3019. int Colons = 0;
  3020. bool Result = true;
  3021. for (int Index = 1; Result && (Index <= Buf.Length()); Index++)
  3022. {
  3023. wchar_t C = Buf[Index];
  3024. if (C == L'%')
  3025. {
  3026. break;
  3027. }
  3028. else if (C == L':')
  3029. {
  3030. Colons++;
  3031. }
  3032. else
  3033. {
  3034. Result = IsHex(C);
  3035. }
  3036. }
  3037. Result = Result && (Colons >= 2);
  3038. return Result;
  3039. }
  3040. //---------------------------------------------------------------------
  3041. UnicodeString __fastcall EscapeIPv6Literal(const UnicodeString & IP)
  3042. {
  3043. UnicodeString Result = IP;
  3044. if (!HasIP6LiteralBrackets(Result))
  3045. {
  3046. Result = L"[" + IP + L"]";
  3047. }
  3048. return Result;
  3049. }
  3050. //---------------------------------------------------------------------
  3051. TStrings * __fastcall TSessionData::GetRawSettingsForUrl()
  3052. {
  3053. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  3054. std::unique_ptr<TSessionData> SessionData(Clone());
  3055. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  3056. SessionData->HostName = FactoryDefaults->HostName;
  3057. SessionData->PortNumber = FactoryDefaults->PortNumber;
  3058. SessionData->UserName = FactoryDefaults->UserName;
  3059. SessionData->Password = FactoryDefaults->Password;
  3060. SessionData->Ftps = FactoryDefaults->Ftps;
  3061. SessionData->HostKey = FactoryDefaults->HostKey;
  3062. SessionData->CopyNonCoreData(FactoryDefaults.get());
  3063. // Cannot be decided in SaveToOptions as it does not have HostName and UserName, so it cannot calculate DefaultSessionName.
  3064. bool SaveName = HasSessionName() && (Name != DefaultSessionName);
  3065. return SessionData->SaveToOptions(FactoryDefaults.get(), SaveName, false);
  3066. }
  3067. //---------------------------------------------------------------------
  3068. bool __fastcall TSessionData::HasRawSettingsForUrl()
  3069. {
  3070. std::unique_ptr<TStrings> RawSettings(GetRawSettingsForUrl());
  3071. return (RawSettings->Count > 0);
  3072. }
  3073. //---------------------------------------------------------------------
  3074. UnicodeString __fastcall TSessionData::GenerateSessionUrl(unsigned int Flags)
  3075. {
  3076. UnicodeString Url;
  3077. if (FLAGSET(Flags, sufSpecific))
  3078. {
  3079. Url += WinSCPProtocolPrefix;
  3080. }
  3081. Url += GetProtocolUrl(FLAGSET(Flags, sufHttpForWebDAV));
  3082. if (FLAGSET(Flags, sufUserName) && !UserNameExpanded.IsEmpty())
  3083. {
  3084. Url += EncodeUrlString(UserNameExpanded);
  3085. if (FLAGSET(Flags, sufPassword) && !Password.IsEmpty())
  3086. {
  3087. Url += L":" + EncodeUrlString(NormalizeString(Password));
  3088. }
  3089. if (FLAGSET(Flags, sufHostKey) && !HostKey.IsEmpty())
  3090. {
  3091. UnicodeString KeyName;
  3092. UnicodeString Fingerprint = HostKey;
  3093. NormalizeFingerprint(Fingerprint, KeyName);
  3094. UnicodeString S = Fingerprint;
  3095. if (!KeyName.IsEmpty())
  3096. {
  3097. S = KeyName + NormalizedFingerprintSeparator + S;
  3098. }
  3099. S = Base64ToUrlSafe(S); // Noop for MD5 (both in SSH host keys and TLS/SSL)
  3100. S = MD5ToUrlSafe(S); // TLS/SSL fingerprints
  3101. UnicodeString S2 = EncodeUrlString(S);
  3102. DebugAssert(S2 == S2); // There should be nothing left for encoding
  3103. Url +=
  3104. UnicodeString(UrlParamSeparator) + UrlHostKeyParamName +
  3105. UnicodeString(UrlParamValueSeparator) + S2;
  3106. }
  3107. if (FLAGSET(Flags, sufRawSettings))
  3108. {
  3109. std::unique_ptr<TStrings> RawSettings(GetRawSettingsForUrl());
  3110. for (int Index = 0; Index < RawSettings->Count; Index++)
  3111. {
  3112. Url +=
  3113. UnicodeString(UrlParamSeparator) +
  3114. UrlRawSettingsParamNamePrefix + EncodeUrlString(LowerCase(RawSettings->Names[Index])) +
  3115. UnicodeString(UrlParamValueSeparator) + EncodeUrlString(RawSettings->ValueFromIndex[Index]);
  3116. }
  3117. }
  3118. Url += L"@";
  3119. }
  3120. DebugAssert(!HostNameExpanded.IsEmpty());
  3121. if (IsIPv6Literal(HostNameExpanded))
  3122. {
  3123. Url += EscapeIPv6Literal(HostNameExpanded);
  3124. }
  3125. else
  3126. {
  3127. Url += EncodeUrlString(HostNameExpanded);
  3128. }
  3129. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  3130. {
  3131. Url += L":" + IntToStr(PortNumber);
  3132. }
  3133. Url += L"/";
  3134. return Url;
  3135. }
  3136. //---------------------------------------------------------------------
  3137. UnicodeString ScriptCommandOpenLink(TraceInitStr(ScriptCommandLink(L"open")));
  3138. //---------------------------------------------------------------------
  3139. void __fastcall TSessionData::AddSwitch(
  3140. UnicodeString & Result, const UnicodeString & Name, bool Rtf)
  3141. {
  3142. Result += RtfSwitch(Name, ScriptCommandOpenLink, Rtf);
  3143. }
  3144. //---------------------------------------------------------------------
  3145. void __fastcall TSessionData::AddSwitch(
  3146. UnicodeString & Result, const UnicodeString & Name, const UnicodeString & Value, bool Rtf)
  3147. {
  3148. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  3149. }
  3150. //---------------------------------------------------------------------
  3151. void __fastcall TSessionData::AddSwitch(
  3152. UnicodeString & Result, const UnicodeString & Name, int Value, bool Rtf)
  3153. {
  3154. Result += RtfSwitch(Name, ScriptCommandOpenLink, Value, Rtf);
  3155. }
  3156. //---------------------------------------------------------------------
  3157. void __fastcall TSessionData::LookupLastFingerprint()
  3158. {
  3159. UnicodeString FingerprintType;
  3160. if (IsSshProtocol(FSProtocol))
  3161. {
  3162. FingerprintType = SshFingerprintType;
  3163. }
  3164. else if (Ftps != ftpsNone)
  3165. {
  3166. FingerprintType = TlsFingerprintType;
  3167. }
  3168. if (!FingerprintType.IsEmpty())
  3169. {
  3170. HostKey = Configuration->LastFingerprint(SiteKey, FingerprintType);
  3171. }
  3172. }
  3173. //---------------------------------------------------------------------
  3174. UnicodeString __fastcall TSessionData::GenerateOpenCommandArgs(bool Rtf)
  3175. {
  3176. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  3177. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  3178. SessionData->Assign(this);
  3179. UnicodeString Result = SessionData->GenerateSessionUrl(sufOpen);
  3180. // Before we reset the FSProtocol
  3181. bool AUsesSsh = SessionData->UsesSsh;
  3182. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  3183. if (SessionData->FSProtocol != fsSFTPonly)
  3184. {
  3185. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  3186. }
  3187. SessionData->HostName = FactoryDefaults->HostName;
  3188. SessionData->PortNumber = FactoryDefaults->PortNumber;
  3189. SessionData->UserName = FactoryDefaults->UserName;
  3190. SessionData->Password = FactoryDefaults->Password;
  3191. SessionData->CopyNonCoreData(FactoryDefaults.get());
  3192. SessionData->Ftps = FactoryDefaults->Ftps;
  3193. if (SessionData->HostKey != FactoryDefaults->HostKey)
  3194. {
  3195. UnicodeString SwitchName = AUsesSsh ? L"hostkey" : L"certificate";
  3196. AddSwitch(Result, SwitchName, SessionData->HostKey, Rtf);
  3197. SessionData->HostKey = FactoryDefaults->HostKey;
  3198. }
  3199. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  3200. {
  3201. AddSwitch(Result, L"privatekey", SessionData->PublicKeyFile, Rtf);
  3202. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  3203. }
  3204. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  3205. {
  3206. AddSwitch(Result, L"clientcert", SessionData->TlsCertificateFile, Rtf);
  3207. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  3208. }
  3209. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  3210. {
  3211. AddSwitch(Result, PassphraseOption, SessionData->Passphrase, Rtf);
  3212. SessionData->Passphrase = FactoryDefaults->Passphrase;
  3213. }
  3214. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  3215. {
  3216. AddSwitch(Result, L"passive", SessionData->FtpPasvMode ? 1 : 0, Rtf);
  3217. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  3218. }
  3219. if (SessionData->Timeout != FactoryDefaults->Timeout)
  3220. {
  3221. AddSwitch(Result, L"timeout", SessionData->Timeout, Rtf);
  3222. SessionData->Timeout = FactoryDefaults->Timeout;
  3223. }
  3224. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get(), false, false));
  3225. if (RawSettings->Count > 0)
  3226. {
  3227. AddSwitch(Result, RawSettingsOption, Rtf);
  3228. Result += StringsToParams(RawSettings.get());
  3229. }
  3230. return Result;
  3231. }
  3232. //---------------------------------------------------------------------
  3233. UnicodeString SessionOptionsClassName(L"SessionOptions");
  3234. //---------------------------------------------------------------------
  3235. void __fastcall TSessionData::AddAssemblyProperty(
  3236. UnicodeString & Result, TAssemblyLanguage Language,
  3237. const UnicodeString & Name, const UnicodeString & Type,
  3238. const UnicodeString & Member)
  3239. {
  3240. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Type, Member, false);
  3241. }
  3242. //---------------------------------------------------------------------
  3243. void __fastcall TSessionData::AddAssemblyProperty(
  3244. UnicodeString & Result, TAssemblyLanguage Language,
  3245. const UnicodeString & Name, const UnicodeString & Value)
  3246. {
  3247. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  3248. }
  3249. //---------------------------------------------------------------------
  3250. void __fastcall TSessionData::AddAssemblyProperty(
  3251. UnicodeString & Result, TAssemblyLanguage Language,
  3252. const UnicodeString & Name, int Value)
  3253. {
  3254. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  3255. }
  3256. //---------------------------------------------------------------------
  3257. void __fastcall TSessionData::AddAssemblyProperty(
  3258. UnicodeString & Result, TAssemblyLanguage Language,
  3259. const UnicodeString & Name, bool Value)
  3260. {
  3261. Result += AssemblyProperty(Language, SessionOptionsClassName, Name, Value, false);
  3262. }
  3263. //---------------------------------------------------------------------
  3264. void __fastcall TSessionData::GenerateAssemblyCode(
  3265. TAssemblyLanguage Language, UnicodeString & Head, UnicodeString & Tail, int & Indent)
  3266. {
  3267. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  3268. std::unique_ptr<TSessionData> SessionData(Clone());
  3269. switch (Language)
  3270. {
  3271. case alCSharp:
  3272. case alVBNET:
  3273. // noop
  3274. break;
  3275. case alPowerShell:
  3276. Head +=
  3277. AssemblyCommentLine(Language, LoadStr(CODE_PS_ADD_TYPE)) +
  3278. RtfKeyword(L"Add-Type") + RtfText(" -Path ") + AssemblyString(Language, "WinSCPnet.dll") + RtfPara +
  3279. RtfPara;
  3280. break;
  3281. default:
  3282. DebugFail();
  3283. break;
  3284. }
  3285. Head +=
  3286. AssemblyCommentLine(Language, LoadStr(CODE_SESSION_OPTIONS)) +
  3287. AssemblyNewClassInstanceStart(Language, SessionOptionsClassName, false);
  3288. UnicodeString ProtocolMember;
  3289. switch (SessionData->FSProtocol)
  3290. {
  3291. case fsSCPonly:
  3292. ProtocolMember = "Scp";
  3293. break;
  3294. default:
  3295. DebugFail();
  3296. // fallback
  3297. case fsSFTP:
  3298. case fsSFTPonly:
  3299. ProtocolMember = "Sftp";
  3300. break;
  3301. case fsFTP:
  3302. ProtocolMember = "Ftp";
  3303. break;
  3304. case fsWebDAV:
  3305. ProtocolMember = "Webdav";
  3306. break;
  3307. case fsS3:
  3308. ProtocolMember = "S3";
  3309. break;
  3310. }
  3311. // Before we reset the FSProtocol
  3312. bool AUsesSsh = SessionData->UsesSsh;
  3313. // Protocol is set unconditionally, we want even the default SFTP
  3314. AddAssemblyProperty(Head, Language, L"Protocol", L"Protocol", ProtocolMember);
  3315. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  3316. if (SessionData->FSProtocol != fsSFTPonly)
  3317. {
  3318. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  3319. }
  3320. if (SessionData->HostName != FactoryDefaults->HostName)
  3321. {
  3322. AddAssemblyProperty(Head, Language, L"HostName", HostName);
  3323. SessionData->HostName = FactoryDefaults->HostName;
  3324. }
  3325. int ADefaultPort = DefaultPort(FSProtocol, Ftps);
  3326. if (SessionData->PortNumber != ADefaultPort)
  3327. {
  3328. AddAssemblyProperty(Head, Language, L"PortNumber", PortNumber);
  3329. }
  3330. SessionData->PortNumber = FactoryDefaults->PortNumber;
  3331. if (SessionData->UserName != FactoryDefaults->UserName)
  3332. {
  3333. AddAssemblyProperty(Head, Language, L"UserName", UserName);
  3334. SessionData->UserName = FactoryDefaults->UserName;
  3335. }
  3336. if (SessionData->Password != FactoryDefaults->Password)
  3337. {
  3338. AddAssemblyProperty(Head, Language, L"Password", NormalizeString(Password));
  3339. SessionData->Password = FactoryDefaults->Password;
  3340. }
  3341. SessionData->CopyNonCoreData(FactoryDefaults.get());
  3342. if (SessionData->Ftps != FactoryDefaults->Ftps)
  3343. {
  3344. // SessionData->FSProtocol is reset already
  3345. switch (FSProtocol)
  3346. {
  3347. case fsFTP:
  3348. {
  3349. UnicodeString FtpSecureMember;
  3350. switch (SessionData->Ftps)
  3351. {
  3352. case ftpsNone:
  3353. // noop
  3354. break;
  3355. case ftpsImplicit:
  3356. FtpSecureMember = L"Implicit";
  3357. break;
  3358. case ftpsExplicitTls:
  3359. case ftpsExplicitSsl:
  3360. FtpSecureMember = L"Explicit";
  3361. break;
  3362. default:
  3363. DebugFail();
  3364. break;
  3365. }
  3366. AddAssemblyProperty(Head, Language, L"FtpSecure", L"FtpSecure", FtpSecureMember);
  3367. }
  3368. break;
  3369. case fsWebDAV:
  3370. AddAssemblyProperty(Head, Language, L"WebdavSecure", (SessionData->Ftps != ftpsNone));
  3371. break;
  3372. case fsS3:
  3373. // implicit
  3374. break;
  3375. default:
  3376. DebugFail();
  3377. break;
  3378. }
  3379. SessionData->Ftps = FactoryDefaults->Ftps;
  3380. }
  3381. if (SessionData->HostKey != FactoryDefaults->HostKey)
  3382. {
  3383. UnicodeString PropertyName = AUsesSsh ? L"SshHostKeyFingerprint" : L"TlsHostCertificateFingerprint";
  3384. AddAssemblyProperty(Head, Language, PropertyName, SessionData->HostKey);
  3385. SessionData->HostKey = FactoryDefaults->HostKey;
  3386. }
  3387. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  3388. {
  3389. AddAssemblyProperty(Head, Language, L"SshPrivateKeyPath", SessionData->PublicKeyFile);
  3390. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  3391. }
  3392. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  3393. {
  3394. AddAssemblyProperty(Head, Language, L"TlsClientCertificatePath", SessionData->TlsCertificateFile);
  3395. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  3396. }
  3397. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  3398. {
  3399. AddAssemblyProperty(Head, Language, L"PrivateKeyPassphrase", SessionData->Passphrase);
  3400. SessionData->Passphrase = FactoryDefaults->Passphrase;
  3401. }
  3402. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  3403. {
  3404. AddAssemblyProperty(Head, Language, L"FtpMode", L"FtpMode", (SessionData->FtpPasvMode ? L"Passive" : L"Active"));
  3405. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  3406. }
  3407. if (SessionData->Timeout != FactoryDefaults->Timeout)
  3408. {
  3409. AddAssemblyProperty(Head, Language, L"TimeoutInMilliseconds", SessionData->Timeout * 1000);
  3410. SessionData->Timeout = FactoryDefaults->Timeout;
  3411. }
  3412. Head += AssemblyNewClassInstanceEnd(Language, false);
  3413. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get(), false, false));
  3414. UnicodeString SessionOptionsVariableName = AssemblyVariableName(Language, SessionOptionsClassName);
  3415. if (RawSettings->Count > 0)
  3416. {
  3417. Head +=
  3418. RtfPara +
  3419. AssemblyAddRawSettings(Language, RawSettings.get(), SessionOptionsClassName, L"AddRawSettings");
  3420. }
  3421. Head += RtfPara;
  3422. UnicodeString Indentation = L" ";
  3423. UnicodeString SessionVariableName = AssemblyVariableName(Language, SessionClassName);
  3424. UnicodeString RtfSessionClass = RtfLibraryClass(SessionClassName);
  3425. UnicodeString RtfSessionOpenMethod = RtfLibraryMethod(SessionClassName, L"Open", false);
  3426. UnicodeString NewSessionInstance = AssemblyNewClassInstance(Language, SessionClassName, false);
  3427. UnicodeString OpenCall =
  3428. Indentation + AssemblyCommentLine(Language, LoadStr(CODE_CONNECT)) +
  3429. Indentation + RtfText(SessionVariableName + L".") + RtfSessionOpenMethod + RtfText(L"(" + SessionOptionsVariableName + L")") +
  3430. AssemblyStatementSeparator(Language) + RtfPara;
  3431. switch (Language)
  3432. {
  3433. case alCSharp:
  3434. Head +=
  3435. RtfKeyword(L"using") + RtfText(" (") + NewSessionInstance + RtfText(L"())") + RtfPara +
  3436. RtfText(L"{") + RtfPara +
  3437. OpenCall;
  3438. Tail =
  3439. RtfText(L"}") + RtfPara;
  3440. break;
  3441. case alVBNET:
  3442. Head +=
  3443. RtfKeyword(L"Using") + RtfText(L" ") + NewSessionInstance + RtfPara +
  3444. OpenCall;
  3445. Tail =
  3446. RtfKeyword(L"End Using") + RtfPara;
  3447. break;
  3448. case alPowerShell:
  3449. Head +=
  3450. NewSessionInstance + RtfPara +
  3451. RtfPara +
  3452. RtfKeyword(L"try") + RtfPara +
  3453. RtfText(L"{") + RtfPara +
  3454. OpenCall;
  3455. Tail =
  3456. RtfText(L"}") + RtfPara +
  3457. RtfKeyword(L"finally") + RtfPara +
  3458. RtfText(L"{") + RtfPara +
  3459. RtfText(Indentation + SessionVariableName + L".") +
  3460. RtfLibraryMethod(SessionClassName, L"Dispose", false) + RtfText(L"()") + RtfPara +
  3461. RtfText(L"}") + RtfPara;
  3462. break;
  3463. }
  3464. Head += RtfPara;
  3465. Indent = 4; // the same for all languages so far
  3466. }
  3467. //---------------------------------------------------------------------
  3468. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  3469. {
  3470. SET_SESSION_PROPERTY(TimeDifference);
  3471. }
  3472. //---------------------------------------------------------------------
  3473. void __fastcall TSessionData::SetTimeDifferenceAuto(bool value)
  3474. {
  3475. SET_SESSION_PROPERTY(TimeDifferenceAuto);
  3476. }
  3477. //---------------------------------------------------------------------
  3478. void __fastcall TSessionData::SetLocalDirectory(UnicodeString value)
  3479. {
  3480. SET_SESSION_PROPERTY(LocalDirectory);
  3481. }
  3482. //---------------------------------------------------------------------
  3483. UnicodeString __fastcall TSessionData::GetLocalDirectoryExpanded()
  3484. {
  3485. return ExpandFileName(::ExpandEnvironmentVariables(LocalDirectory));
  3486. }
  3487. //---------------------------------------------------------------------
  3488. void __fastcall TSessionData::SetRemoteDirectory(UnicodeString value)
  3489. {
  3490. SET_SESSION_PROPERTY(RemoteDirectory);
  3491. }
  3492. //---------------------------------------------------------------------
  3493. void __fastcall TSessionData::SetSynchronizeBrowsing(bool value)
  3494. {
  3495. SET_SESSION_PROPERTY(SynchronizeBrowsing);
  3496. }
  3497. //---------------------------------------------------------------------
  3498. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  3499. {
  3500. SET_SESSION_PROPERTY(UpdateDirectories);
  3501. }
  3502. //---------------------------------------------------------------------
  3503. void __fastcall TSessionData::SetCacheDirectories(bool value)
  3504. {
  3505. SET_SESSION_PROPERTY(CacheDirectories);
  3506. }
  3507. //---------------------------------------------------------------------
  3508. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  3509. {
  3510. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  3511. }
  3512. //---------------------------------------------------------------------
  3513. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  3514. {
  3515. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  3516. }
  3517. //---------------------------------------------------------------------
  3518. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  3519. {
  3520. SET_SESSION_PROPERTY(ResolveSymlinks);
  3521. }
  3522. //---------------------------------------------------------------------
  3523. void __fastcall TSessionData::SetFollowDirectorySymlinks(bool value)
  3524. {
  3525. SET_SESSION_PROPERTY(FollowDirectorySymlinks);
  3526. }
  3527. //---------------------------------------------------------------------------
  3528. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  3529. {
  3530. SET_SESSION_PROPERTY(DSTMode);
  3531. }
  3532. //---------------------------------------------------------------------------
  3533. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  3534. {
  3535. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  3536. }
  3537. //---------------------------------------------------------------------------
  3538. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  3539. {
  3540. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  3541. }
  3542. //---------------------------------------------------------------------------
  3543. void __fastcall TSessionData::SetRecycleBinPath(UnicodeString value)
  3544. {
  3545. SET_SESSION_PROPERTY(RecycleBinPath);
  3546. }
  3547. //---------------------------------------------------------------------------
  3548. void __fastcall TSessionData::SetPostLoginCommands(UnicodeString value)
  3549. {
  3550. SET_SESSION_PROPERTY(PostLoginCommands);
  3551. }
  3552. //---------------------------------------------------------------------
  3553. void __fastcall TSessionData::SetLockInHome(bool value)
  3554. {
  3555. SET_SESSION_PROPERTY(LockInHome);
  3556. }
  3557. //---------------------------------------------------------------------
  3558. void __fastcall TSessionData::SetSpecial(bool value)
  3559. {
  3560. SET_SESSION_PROPERTY(Special);
  3561. }
  3562. //---------------------------------------------------------------------------
  3563. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  3564. {
  3565. SET_SESSION_PROPERTY(Scp1Compatibility);
  3566. }
  3567. //---------------------------------------------------------------------
  3568. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  3569. {
  3570. SET_SESSION_PROPERTY(TcpNoDelay);
  3571. }
  3572. //---------------------------------------------------------------------
  3573. void __fastcall TSessionData::SetSendBuf(int value)
  3574. {
  3575. SET_SESSION_PROPERTY(SendBuf);
  3576. }
  3577. //---------------------------------------------------------------------
  3578. void __fastcall TSessionData::SetSourceAddress(const UnicodeString & value)
  3579. {
  3580. SET_SESSION_PROPERTY(SourceAddress);
  3581. }
  3582. //---------------------------------------------------------------------
  3583. void __fastcall TSessionData::SetProtocolFeatures(const UnicodeString & value)
  3584. {
  3585. SET_SESSION_PROPERTY(ProtocolFeatures);
  3586. }
  3587. //---------------------------------------------------------------------
  3588. void __fastcall TSessionData::SetSshSimple(bool value)
  3589. {
  3590. SET_SESSION_PROPERTY(SshSimple);
  3591. }
  3592. //---------------------------------------------------------------------
  3593. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  3594. {
  3595. SET_SESSION_PROPERTY(ProxyMethod);
  3596. }
  3597. //---------------------------------------------------------------------
  3598. void __fastcall TSessionData::SetProxyHost(UnicodeString value)
  3599. {
  3600. SET_SESSION_PROPERTY(ProxyHost);
  3601. }
  3602. //---------------------------------------------------------------------
  3603. void __fastcall TSessionData::SetProxyPort(int value)
  3604. {
  3605. SET_SESSION_PROPERTY(ProxyPort);
  3606. }
  3607. //---------------------------------------------------------------------
  3608. void __fastcall TSessionData::SetProxyUsername(UnicodeString value)
  3609. {
  3610. SET_SESSION_PROPERTY(ProxyUsername);
  3611. }
  3612. //---------------------------------------------------------------------
  3613. void __fastcall TSessionData::SetProxyPassword(UnicodeString avalue)
  3614. {
  3615. RawByteString value = EncryptPassword(avalue, ProxyUsername+ProxyHost);
  3616. SET_SESSION_PROPERTY(ProxyPassword);
  3617. }
  3618. //---------------------------------------------------------------------
  3619. UnicodeString __fastcall TSessionData::GetProxyPassword() const
  3620. {
  3621. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  3622. }
  3623. //---------------------------------------------------------------------
  3624. void __fastcall TSessionData::SetProxyTelnetCommand(UnicodeString value)
  3625. {
  3626. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  3627. }
  3628. //---------------------------------------------------------------------
  3629. void __fastcall TSessionData::SetProxyLocalCommand(UnicodeString value)
  3630. {
  3631. SET_SESSION_PROPERTY(ProxyLocalCommand);
  3632. }
  3633. //---------------------------------------------------------------------
  3634. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  3635. {
  3636. SET_SESSION_PROPERTY(ProxyDNS);
  3637. }
  3638. //---------------------------------------------------------------------
  3639. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  3640. {
  3641. SET_SESSION_PROPERTY(ProxyLocalhost);
  3642. }
  3643. //---------------------------------------------------------------------
  3644. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  3645. {
  3646. SET_SESSION_PROPERTY(FtpProxyLogonType);
  3647. }
  3648. //---------------------------------------------------------------------
  3649. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  3650. {
  3651. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3652. SET_SESSION_PROPERTY(Bugs[Bug]);
  3653. }
  3654. //---------------------------------------------------------------------
  3655. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  3656. {
  3657. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  3658. return FBugs[Bug];
  3659. }
  3660. //---------------------------------------------------------------------
  3661. void __fastcall TSessionData::SetPuttySettings(UnicodeString value)
  3662. {
  3663. SET_SESSION_PROPERTY(PuttySettings);
  3664. }
  3665. //---------------------------------------------------------------------
  3666. void __fastcall TSessionData::SetCustomParam1(UnicodeString value)
  3667. {
  3668. SET_SESSION_PROPERTY(CustomParam1);
  3669. }
  3670. //---------------------------------------------------------------------
  3671. void __fastcall TSessionData::SetCustomParam2(UnicodeString value)
  3672. {
  3673. SET_SESSION_PROPERTY(CustomParam2);
  3674. }
  3675. //---------------------------------------------------------------------
  3676. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  3677. {
  3678. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  3679. }
  3680. //---------------------------------------------------------------------
  3681. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  3682. {
  3683. SET_SESSION_PROPERTY(SFTPUploadQueue);
  3684. }
  3685. //---------------------------------------------------------------------
  3686. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  3687. {
  3688. SET_SESSION_PROPERTY(SFTPListingQueue);
  3689. }
  3690. //---------------------------------------------------------------------
  3691. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  3692. {
  3693. SET_SESSION_PROPERTY(SFTPMaxVersion);
  3694. }
  3695. //---------------------------------------------------------------------
  3696. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  3697. {
  3698. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  3699. }
  3700. //---------------------------------------------------------------------
  3701. void __fastcall TSessionData::SetSFTPRealPath(TAutoSwitch value)
  3702. {
  3703. SET_SESSION_PROPERTY(SFTPRealPath);
  3704. }
  3705. //---------------------------------------------------------------------
  3706. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  3707. {
  3708. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3709. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  3710. }
  3711. //---------------------------------------------------------------------
  3712. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  3713. {
  3714. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  3715. return FSFTPBugs[Bug];
  3716. }
  3717. //---------------------------------------------------------------------
  3718. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  3719. {
  3720. SET_SESSION_PROPERTY(SCPLsFullTime);
  3721. }
  3722. //---------------------------------------------------------------------------
  3723. void __fastcall TSessionData::SetColor(int value)
  3724. {
  3725. SET_SESSION_PROPERTY(Color);
  3726. }
  3727. //---------------------------------------------------------------------------
  3728. void __fastcall TSessionData::SetTunnel(bool value)
  3729. {
  3730. SET_SESSION_PROPERTY(Tunnel);
  3731. }
  3732. //---------------------------------------------------------------------
  3733. void __fastcall TSessionData::SetTunnelHostName(UnicodeString value)
  3734. {
  3735. if (FTunnelHostName != value)
  3736. {
  3737. // HostName is key for password encryption
  3738. UnicodeString XTunnelPassword = TunnelPassword;
  3739. int P = value.LastDelimiter(L"@");
  3740. if (P > 0)
  3741. {
  3742. TunnelUserName = value.SubString(1, P - 1);
  3743. value = value.SubString(P + 1, value.Length() - P);
  3744. }
  3745. FTunnelHostName = value;
  3746. Modify();
  3747. TunnelPassword = XTunnelPassword;
  3748. Shred(XTunnelPassword);
  3749. }
  3750. }
  3751. //---------------------------------------------------------------------
  3752. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  3753. {
  3754. SET_SESSION_PROPERTY(TunnelPortNumber);
  3755. }
  3756. //---------------------------------------------------------------------
  3757. void __fastcall TSessionData::SetTunnelUserName(UnicodeString value)
  3758. {
  3759. // Avoid password recryption (what may popup master password prompt)
  3760. if (FTunnelUserName != value)
  3761. {
  3762. // TunnelUserName is key for password encryption
  3763. UnicodeString XTunnelPassword = TunnelPassword;
  3764. SET_SESSION_PROPERTY(TunnelUserName);
  3765. TunnelPassword = XTunnelPassword;
  3766. Shred(XTunnelPassword);
  3767. }
  3768. }
  3769. //---------------------------------------------------------------------
  3770. void __fastcall TSessionData::SetTunnelPassword(UnicodeString avalue)
  3771. {
  3772. RawByteString value = EncryptPassword(avalue, TunnelUserName+TunnelHostName);
  3773. SET_SESSION_PROPERTY(TunnelPassword);
  3774. }
  3775. //---------------------------------------------------------------------
  3776. UnicodeString __fastcall TSessionData::GetTunnelPassword() const
  3777. {
  3778. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  3779. }
  3780. //---------------------------------------------------------------------
  3781. void __fastcall TSessionData::SetTunnelPublicKeyFile(UnicodeString value)
  3782. {
  3783. if (FTunnelPublicKeyFile != value)
  3784. {
  3785. // StripPathQuotes should not be needed as we do not feed quotes anymore
  3786. FTunnelPublicKeyFile = StripPathQuotes(value);
  3787. Modify();
  3788. }
  3789. }
  3790. //---------------------------------------------------------------------
  3791. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  3792. {
  3793. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  3794. }
  3795. //---------------------------------------------------------------------
  3796. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  3797. {
  3798. return (FTunnelLocalPortNumber <= 0);
  3799. }
  3800. //---------------------------------------------------------------------
  3801. void __fastcall TSessionData::SetTunnelPortFwd(UnicodeString value)
  3802. {
  3803. SET_SESSION_PROPERTY(TunnelPortFwd);
  3804. }
  3805. //---------------------------------------------------------------------
  3806. void __fastcall TSessionData::SetTunnelHostKey(UnicodeString value)
  3807. {
  3808. SET_SESSION_PROPERTY(TunnelHostKey);
  3809. }
  3810. //---------------------------------------------------------------------
  3811. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  3812. {
  3813. SET_SESSION_PROPERTY(FtpPasvMode);
  3814. }
  3815. //---------------------------------------------------------------------
  3816. void __fastcall TSessionData::SetFtpForcePasvIp(TAutoSwitch value)
  3817. {
  3818. SET_SESSION_PROPERTY(FtpForcePasvIp);
  3819. }
  3820. //---------------------------------------------------------------------
  3821. void __fastcall TSessionData::SetFtpUseMlsd(TAutoSwitch value)
  3822. {
  3823. SET_SESSION_PROPERTY(FtpUseMlsd);
  3824. }
  3825. //---------------------------------------------------------------------
  3826. void __fastcall TSessionData::SetFtpAccount(UnicodeString value)
  3827. {
  3828. SET_SESSION_PROPERTY(FtpAccount);
  3829. }
  3830. //---------------------------------------------------------------------
  3831. void __fastcall TSessionData::SetFtpPingInterval(int value)
  3832. {
  3833. SET_SESSION_PROPERTY(FtpPingInterval);
  3834. }
  3835. //---------------------------------------------------------------------------
  3836. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  3837. {
  3838. return SecToDateTime(FtpPingInterval);
  3839. }
  3840. //---------------------------------------------------------------------------
  3841. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  3842. {
  3843. SET_SESSION_PROPERTY(FtpPingType);
  3844. }
  3845. //---------------------------------------------------------------------------
  3846. void __fastcall TSessionData::SetFtpTransferActiveImmediately(TAutoSwitch value)
  3847. {
  3848. SET_SESSION_PROPERTY(FtpTransferActiveImmediately);
  3849. }
  3850. //---------------------------------------------------------------------------
  3851. void __fastcall TSessionData::SetFtps(TFtps value)
  3852. {
  3853. SET_SESSION_PROPERTY(Ftps);
  3854. }
  3855. //---------------------------------------------------------------------------
  3856. void __fastcall TSessionData::SetMinTlsVersion(TTlsVersion value)
  3857. {
  3858. SET_SESSION_PROPERTY(MinTlsVersion);
  3859. }
  3860. //---------------------------------------------------------------------------
  3861. void __fastcall TSessionData::SetMaxTlsVersion(TTlsVersion value)
  3862. {
  3863. SET_SESSION_PROPERTY(MaxTlsVersion);
  3864. }
  3865. //---------------------------------------------------------------------------
  3866. void __fastcall TSessionData::SetLogicalHostName(UnicodeString value)
  3867. {
  3868. SET_SESSION_PROPERTY(LogicalHostName);
  3869. }
  3870. //---------------------------------------------------------------------
  3871. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  3872. {
  3873. SET_SESSION_PROPERTY(FtpListAll);
  3874. }
  3875. //---------------------------------------------------------------------
  3876. void __fastcall TSessionData::SetFtpHost(TAutoSwitch value)
  3877. {
  3878. SET_SESSION_PROPERTY(FtpHost);
  3879. }
  3880. //---------------------------------------------------------------------
  3881. void __fastcall TSessionData::SetFtpWorkFromCwd(TAutoSwitch value)
  3882. {
  3883. SET_SESSION_PROPERTY(FtpWorkFromCwd);
  3884. }
  3885. //---------------------------------------------------------------------
  3886. void __fastcall TSessionData::SetSslSessionReuse(bool value)
  3887. {
  3888. SET_SESSION_PROPERTY(SslSessionReuse);
  3889. }
  3890. //---------------------------------------------------------------------
  3891. void __fastcall TSessionData::SetTlsCertificateFile(UnicodeString value)
  3892. {
  3893. SET_SESSION_PROPERTY(TlsCertificateFile);
  3894. }
  3895. //---------------------------------------------------------------------
  3896. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  3897. {
  3898. SET_SESSION_PROPERTY(NotUtf);
  3899. }
  3900. //---------------------------------------------------------------------
  3901. void __fastcall TSessionData::SetInternalEditorEncoding(int value)
  3902. {
  3903. SET_SESSION_PROPERTY(InternalEditorEncoding);
  3904. }
  3905. //---------------------------------------------------------------------
  3906. void __fastcall TSessionData::SetS3DefaultRegion(UnicodeString value)
  3907. {
  3908. SET_SESSION_PROPERTY(S3DefaultRegion);
  3909. }
  3910. //---------------------------------------------------------------------
  3911. void __fastcall TSessionData::SetS3SessionToken(UnicodeString value)
  3912. {
  3913. SET_SESSION_PROPERTY(S3SessionToken);
  3914. }
  3915. //---------------------------------------------------------------------
  3916. void __fastcall TSessionData::SetS3UrlStyle(TS3UrlStyle value)
  3917. {
  3918. SET_SESSION_PROPERTY(S3UrlStyle);
  3919. }
  3920. //---------------------------------------------------------------------
  3921. void __fastcall TSessionData::SetS3MaxKeys(TAutoSwitch value)
  3922. {
  3923. SET_SESSION_PROPERTY(S3MaxKeys);
  3924. }
  3925. //---------------------------------------------------------------------
  3926. void __fastcall TSessionData::SetIsWorkspace(bool value)
  3927. {
  3928. SET_SESSION_PROPERTY(IsWorkspace);
  3929. }
  3930. //---------------------------------------------------------------------
  3931. void __fastcall TSessionData::SetLink(UnicodeString value)
  3932. {
  3933. SET_SESSION_PROPERTY(Link);
  3934. }
  3935. //---------------------------------------------------------------------
  3936. void __fastcall TSessionData::SetNameOverride(UnicodeString value)
  3937. {
  3938. SET_SESSION_PROPERTY(NameOverride);
  3939. }
  3940. //---------------------------------------------------------------------
  3941. void __fastcall TSessionData::SetHostKey(UnicodeString value)
  3942. {
  3943. SET_SESSION_PROPERTY(HostKey);
  3944. }
  3945. //---------------------------------------------------------------------
  3946. void __fastcall TSessionData::SetNote(UnicodeString value)
  3947. {
  3948. SET_SESSION_PROPERTY(Note);
  3949. }
  3950. //---------------------------------------------------------------------
  3951. void __fastcall TSessionData::SetWinTitle(UnicodeString value)
  3952. {
  3953. SET_SESSION_PROPERTY(WinTitle);
  3954. }
  3955. //---------------------------------------------------------------------
  3956. UnicodeString __fastcall TSessionData::GetEncryptKey() const
  3957. {
  3958. return DecryptPassword(FEncryptKey, UserName+HostName);
  3959. }
  3960. //---------------------------------------------------------------------
  3961. void __fastcall TSessionData::SetEncryptKey(UnicodeString avalue)
  3962. {
  3963. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  3964. SET_SESSION_PROPERTY(EncryptKey);
  3965. }
  3966. //---------------------------------------------------------------------
  3967. void __fastcall TSessionData::SetWebDavLiberalEscaping(bool value)
  3968. {
  3969. SET_SESSION_PROPERTY(WebDavLiberalEscaping);
  3970. }
  3971. //---------------------------------------------------------------------
  3972. UnicodeString __fastcall TSessionData::GetInfoTip()
  3973. {
  3974. if (UsesSsh)
  3975. {
  3976. return FMTLOAD(SESSION_INFO_TIP2,
  3977. (HostName, UserName,
  3978. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  3979. FSProtocolStr));
  3980. }
  3981. else
  3982. {
  3983. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  3984. (HostName, UserName, FSProtocolStr));
  3985. }
  3986. }
  3987. //---------------------------------------------------------------------
  3988. UnicodeString __fastcall TSessionData::ExtractLocalName(const UnicodeString & Name)
  3989. {
  3990. UnicodeString Result = Name;
  3991. int P = Result.LastDelimiter(L"/");
  3992. if (P > 0)
  3993. {
  3994. Result.Delete(1, P);
  3995. }
  3996. return Result;
  3997. }
  3998. //---------------------------------------------------------------------
  3999. UnicodeString __fastcall TSessionData::GetLocalName()
  4000. {
  4001. UnicodeString Result;
  4002. if (HasSessionName())
  4003. {
  4004. Result = ExtractLocalName(Name);
  4005. }
  4006. else
  4007. {
  4008. Result = DefaultSessionName;
  4009. }
  4010. return Result;
  4011. }
  4012. //---------------------------------------------------------------------
  4013. UnicodeString __fastcall TSessionData::ExtractFolderName(const UnicodeString & Name)
  4014. {
  4015. UnicodeString Result;
  4016. int P = Name.LastDelimiter(L"/");
  4017. if (P > 0)
  4018. {
  4019. Result = Name.SubString(1, P - 1);
  4020. }
  4021. return Result;
  4022. }
  4023. //---------------------------------------------------------------------
  4024. UnicodeString __fastcall TSessionData::GetFolderName()
  4025. {
  4026. UnicodeString Result;
  4027. if (HasSessionName() || IsWorkspace)
  4028. {
  4029. Result = ExtractFolderName(Name);
  4030. }
  4031. return Result;
  4032. }
  4033. //---------------------------------------------------------------------
  4034. UnicodeString __fastcall TSessionData::ComposePath(
  4035. const UnicodeString & Path, const UnicodeString & Name)
  4036. {
  4037. return UnixIncludeTrailingBackslash(Path) + Name;
  4038. }
  4039. //---------------------------------------------------------------------
  4040. void __fastcall TSessionData::DisableAuthentationsExceptPassword()
  4041. {
  4042. SshNoUserAuth = false;
  4043. AuthTIS = false;
  4044. AuthKI = false;
  4045. AuthKIPassword = false;
  4046. AuthGSSAPI = false;
  4047. AuthGSSAPIKEX = false;
  4048. PublicKeyFile = L"";
  4049. TlsCertificateFile = L"";
  4050. Passphrase = L"";
  4051. TryAgent = false;
  4052. }
  4053. //---------------------------------------------------------------------
  4054. TStrings * TSessionData::GetAllOptionNames(bool PuttyExport)
  4055. {
  4056. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  4057. return FactoryDefaults->SaveToOptions(NULL, false, PuttyExport);
  4058. }
  4059. //=== TStoredSessionList ----------------------------------------------
  4060. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  4061. TNamedObjectList(), FReadOnly(aReadOnly)
  4062. {
  4063. DebugAssert(Configuration);
  4064. FDefaultSettings = new TSessionData(DefaultName);
  4065. FPendingRemovals.reset(new TStringList());
  4066. }
  4067. //---------------------------------------------------------------------
  4068. __fastcall TStoredSessionList::~TStoredSessionList()
  4069. {
  4070. DebugAssert(Configuration);
  4071. delete FDefaultSettings;
  4072. }
  4073. //---------------------------------------------------------------------
  4074. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  4075. bool AsModified, bool UseDefaults, bool PuttyImport)
  4076. {
  4077. TStringList *SubKeys = new TStringList();
  4078. TList * Loaded = new TList;
  4079. try
  4080. {
  4081. DebugAssert(AutoSort);
  4082. AutoSort = false;
  4083. bool WasEmpty = (Count == 0);
  4084. Storage->GetSubKeyNames(SubKeys);
  4085. for (int Index = 0; Index < SubKeys->Count; Index++)
  4086. {
  4087. UnicodeString SessionName = SubKeys->Strings[Index];
  4088. bool ValidName = true;
  4089. try
  4090. {
  4091. TSessionData::ValidatePath(SessionName);
  4092. }
  4093. catch(...)
  4094. {
  4095. ValidName = false;
  4096. }
  4097. if (ValidName)
  4098. {
  4099. TSessionData * SessionData;
  4100. if (SessionName == FDefaultSettings->Name)
  4101. {
  4102. SessionData = FDefaultSettings;
  4103. }
  4104. else
  4105. {
  4106. // if the list was empty before loading, do not waste time trying to
  4107. // find existing sites to overwrite (we rely on underlying storage
  4108. // to secure uniqueness of the key names)
  4109. if (WasEmpty)
  4110. {
  4111. SessionData = NULL;
  4112. }
  4113. else
  4114. {
  4115. SessionData = (TSessionData*)FindByName(SessionName);
  4116. }
  4117. }
  4118. if ((SessionData != FDefaultSettings) || !UseDefaults)
  4119. {
  4120. if (SessionData == NULL)
  4121. {
  4122. SessionData = new TSessionData(L"");
  4123. if (UseDefaults)
  4124. {
  4125. SessionData->CopyData(DefaultSettings);
  4126. }
  4127. SessionData->Name = SessionName;
  4128. Add(SessionData);
  4129. }
  4130. Loaded->Add(SessionData);
  4131. SessionData->Load(Storage, PuttyImport);
  4132. if (AsModified)
  4133. {
  4134. SessionData->Modified = true;
  4135. }
  4136. }
  4137. }
  4138. }
  4139. if (!AsModified)
  4140. {
  4141. for (int Index = 0; Index < TObjectList::Count; Index++)
  4142. {
  4143. if (Loaded->IndexOf(Items[Index]) < 0)
  4144. {
  4145. Delete(Index);
  4146. Index--;
  4147. }
  4148. }
  4149. }
  4150. }
  4151. __finally
  4152. {
  4153. AutoSort = true;
  4154. AlphaSort();
  4155. delete SubKeys;
  4156. delete Loaded;
  4157. }
  4158. }
  4159. //---------------------------------------------------------------------
  4160. void __fastcall TStoredSessionList::Reload()
  4161. {
  4162. if (Count <= Configuration->DontReloadMoreThanSessions)
  4163. {
  4164. bool SessionList = true;
  4165. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateScpStorage(SessionList));
  4166. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  4167. {
  4168. Load(Storage.get());
  4169. }
  4170. }
  4171. }
  4172. //---------------------------------------------------------------------
  4173. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  4174. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  4175. TSessionData * FactoryDefaults)
  4176. {
  4177. if (All || Data->Modified)
  4178. {
  4179. if (RecryptPasswordOnly)
  4180. {
  4181. Data->SaveRecryptedPasswords(Storage);
  4182. }
  4183. else
  4184. {
  4185. Data->Save(Storage, false, FactoryDefaults);
  4186. }
  4187. }
  4188. }
  4189. //---------------------------------------------------------------------
  4190. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  4191. bool All, bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  4192. {
  4193. TSessionData * FactoryDefaults = new TSessionData(L"");
  4194. try
  4195. {
  4196. while (FPendingRemovals->Count > 0)
  4197. {
  4198. TSessionData::Remove(Storage, FPendingRemovals->Strings[0]);
  4199. FPendingRemovals->Delete(0);
  4200. }
  4201. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  4202. for (int Index = 0; Index < CountIncludingHidden; Index++)
  4203. {
  4204. TSessionData * SessionData = (TSessionData *)Items[Index];
  4205. try
  4206. {
  4207. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  4208. }
  4209. catch (Exception & E)
  4210. {
  4211. UnicodeString Message;
  4212. if (RecryptPasswordOnly && DebugAlwaysTrue(RecryptPasswordErrors != NULL) &&
  4213. ExceptionMessage(&E, Message))
  4214. {
  4215. RecryptPasswordErrors->Add(FORMAT("%s: %s", (SessionData->SessionName, Message)));
  4216. }
  4217. else
  4218. {
  4219. throw;
  4220. }
  4221. }
  4222. }
  4223. }
  4224. __finally
  4225. {
  4226. delete FactoryDefaults;
  4227. }
  4228. }
  4229. //---------------------------------------------------------------------
  4230. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  4231. {
  4232. DoSave(Storage, All, false, NULL);
  4233. }
  4234. //---------------------------------------------------------------------
  4235. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit,
  4236. bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  4237. {
  4238. bool SessionList = true;
  4239. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  4240. try
  4241. {
  4242. Storage->AccessMode = smReadWrite;
  4243. Storage->Explicit = Explicit;
  4244. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  4245. {
  4246. DoSave(Storage, All, RecryptPasswordOnly, RecryptPasswordErrors);
  4247. }
  4248. }
  4249. __finally
  4250. {
  4251. delete Storage;
  4252. }
  4253. Saved();
  4254. }
  4255. //---------------------------------------------------------------------
  4256. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  4257. {
  4258. DoSave(All, Explicit, false, NULL);
  4259. }
  4260. //---------------------------------------------------------------------
  4261. void __fastcall TStoredSessionList::RecryptPasswords(TStrings * RecryptPasswordErrors)
  4262. {
  4263. DoSave(true, true, true, RecryptPasswordErrors);
  4264. }
  4265. //---------------------------------------------------------------------
  4266. void __fastcall TStoredSessionList::Saved()
  4267. {
  4268. FDefaultSettings->Modified = false;
  4269. for (int Index = 0; Index < CountIncludingHidden; Index++)
  4270. {
  4271. ((TSessionData *)Items[Index])->Modified = false;
  4272. }
  4273. }
  4274. //---------------------------------------------------------------------
  4275. void __fastcall TStoredSessionList::ImportLevelFromFilezilla(
  4276. _di_IXMLNode Node, const UnicodeString & Path, _di_IXMLNode SettingsNode)
  4277. {
  4278. for (int Index = 0; Index < Node->ChildNodes->Count; Index++)
  4279. {
  4280. _di_IXMLNode ChildNode = Node->ChildNodes->Get(Index);
  4281. if (ChildNode->NodeName == L"Server")
  4282. {
  4283. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  4284. SessionData->CopyData(DefaultSettings);
  4285. SessionData->ImportFromFilezilla(ChildNode, Path, SettingsNode);
  4286. Add(SessionData.release());
  4287. }
  4288. else if (ChildNode->NodeName == L"Folder")
  4289. {
  4290. UnicodeString Name;
  4291. for (int Index = 0; Index < ChildNode->ChildNodes->Count; Index++)
  4292. {
  4293. _di_IXMLNode PossibleTextMode = ChildNode->ChildNodes->Get(Index);
  4294. if (PossibleTextMode->NodeType == ntText)
  4295. {
  4296. UnicodeString NodeValue = PossibleTextMode->NodeValue;
  4297. AddToList(Name, NodeValue.Trim(), L" ");
  4298. }
  4299. }
  4300. Name = TSessionData::MakeValidName(Name).Trim();
  4301. ImportLevelFromFilezilla(ChildNode, TSessionData::ComposePath(Path, Name), SettingsNode);
  4302. }
  4303. }
  4304. }
  4305. //---------------------------------------------------------------------
  4306. void __fastcall TStoredSessionList::ImportFromFilezilla(
  4307. const UnicodeString FileName, const UnicodeString ConfigurationFileName)
  4308. {
  4309. // not sure if the document must exists if we want to use its node
  4310. _di_IXMLDocument ConfigurationDocument;
  4311. _di_IXMLNode SettingsNode;
  4312. if (FileExists(ApiPath(ConfigurationFileName)))
  4313. {
  4314. ConfigurationDocument = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  4315. ConfigurationDocument->LoadFromFile(ConfigurationFileName);
  4316. _di_IXMLNode FileZilla3Node = ConfigurationDocument->ChildNodes->FindNode(L"FileZilla3");
  4317. if (FileZilla3Node != NULL)
  4318. {
  4319. SettingsNode = FileZilla3Node->ChildNodes->FindNode(L"Settings");
  4320. }
  4321. }
  4322. const _di_IXMLDocument Document = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  4323. Document->LoadFromFile(FileName);
  4324. _di_IXMLNode FileZilla3Node = Document->ChildNodes->FindNode(L"FileZilla3");
  4325. if (FileZilla3Node != NULL)
  4326. {
  4327. _di_IXMLNode ServersNode = FileZilla3Node->ChildNodes->FindNode(L"Servers");
  4328. if (ServersNode != NULL)
  4329. {
  4330. ImportLevelFromFilezilla(ServersNode, L"", SettingsNode);
  4331. }
  4332. }
  4333. }
  4334. //---------------------------------------------------------------------
  4335. void __fastcall TStoredSessionList::ImportFromKnownHosts(TStrings * Lines)
  4336. {
  4337. bool SessionList = false;
  4338. std::unique_ptr<THierarchicalStorage> HostKeyStorage(Configuration->CreateScpStorage(SessionList));
  4339. std::unique_ptr<TStrings> KeyList(new TStringList());
  4340. if (OpenHostKeysSubKey(HostKeyStorage.get(), false))
  4341. {
  4342. HostKeyStorage->GetValueNames(KeyList.get());
  4343. }
  4344. HostKeyStorage.reset(NULL);
  4345. UnicodeString FirstError;
  4346. for (int Index = 0; Index < Lines->Count; Index++)
  4347. {
  4348. try
  4349. {
  4350. UnicodeString Line = Lines->Strings[Index];
  4351. Line = Trim(Line);
  4352. if (!Line.IsEmpty() && (Line[1] != L';'))
  4353. {
  4354. int P = Pos(L' ', Line);
  4355. if (P > 0)
  4356. {
  4357. UnicodeString HostNameStr = Line.SubString(1, P - 1);
  4358. Line = Line.SubString(P + 1, Line.Length() - P);
  4359. P = Pos(L',', HostNameStr);
  4360. if (P > 0)
  4361. {
  4362. HostNameStr.SetLength(P - 1);
  4363. }
  4364. P = Pos(L':', HostNameStr);
  4365. int PortNumber = -1;
  4366. if (P > 0)
  4367. {
  4368. UnicodeString PortNumberStr = HostNameStr.SubString(P + 1, HostNameStr.Length() - P);
  4369. PortNumber = StrToInt(PortNumberStr);
  4370. HostNameStr.SetLength(P - 1);
  4371. }
  4372. if (HasIP6LiteralBrackets(HostNameStr))
  4373. {
  4374. HostNameStr = StripIP6LiteralBrackets(HostNameStr);
  4375. }
  4376. UnicodeString NameStr = HostNameStr;
  4377. if (PortNumber >= 0)
  4378. {
  4379. NameStr = FORMAT(L"%s:%d", (NameStr, PortNumber));
  4380. }
  4381. std::unique_ptr<TSessionData> SessionDataOwner;
  4382. TSessionData * SessionData = dynamic_cast<TSessionData *>(FindByName(NameStr));
  4383. if (SessionData == NULL)
  4384. {
  4385. SessionData = new TSessionData(L"");
  4386. SessionDataOwner.reset(SessionData);
  4387. SessionData->CopyData(DefaultSettings);
  4388. SessionData->Name = NameStr;
  4389. SessionData->HostName = HostNameStr;
  4390. if (PortNumber >= 0)
  4391. {
  4392. SessionData->PortNumber = PortNumber;
  4393. }
  4394. }
  4395. const struct ssh_keyalg * Algorithm;
  4396. UnicodeString Key = ParseOpenSshPubLine(Line, Algorithm);
  4397. UnicodeString KeyKey =
  4398. FORMAT(L"%s@%d:%s", (Algorithm->cache_id, SessionData->PortNumber, HostNameStr));
  4399. UnicodeString HostKey =
  4400. FORMAT(L"%s:%s=%s", (Algorithm->ssh_id, KeyKey, Key));
  4401. UnicodeString HostKeyList = SessionData->HostKey;
  4402. AddToList(HostKeyList, HostKey, L";");
  4403. SessionData->HostKey = HostKeyList;
  4404. // If there's at least one unknown key type for this host, select it
  4405. if (KeyList->IndexOf(KeyKey) < 0)
  4406. {
  4407. SessionData->Selected = true;
  4408. }
  4409. if (SessionDataOwner.get() != NULL)
  4410. {
  4411. Add(SessionDataOwner.release());
  4412. }
  4413. }
  4414. }
  4415. }
  4416. catch (Exception & E)
  4417. {
  4418. if (FirstError.IsEmpty())
  4419. {
  4420. FirstError = E.Message;
  4421. }
  4422. }
  4423. }
  4424. if (Count == 0)
  4425. {
  4426. UnicodeString Message = LoadStr(KNOWN_HOSTS_NO_SITES);
  4427. if (!FirstError.IsEmpty())
  4428. {
  4429. Message = FORMAT(L"%s\n(%s)", (Message, FirstError));
  4430. }
  4431. throw Exception(Message);
  4432. }
  4433. }
  4434. //---------------------------------------------------------------------
  4435. void __fastcall TStoredSessionList::Export(const UnicodeString FileName)
  4436. {
  4437. THierarchicalStorage * Storage = TIniFileStorage::CreateFromPath(FileName);
  4438. try
  4439. {
  4440. Storage->AccessMode = smReadWrite;
  4441. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  4442. {
  4443. Save(Storage, true);
  4444. }
  4445. }
  4446. __finally
  4447. {
  4448. delete Storage;
  4449. }
  4450. }
  4451. //---------------------------------------------------------------------
  4452. void __fastcall TStoredSessionList::SelectAll(bool Select)
  4453. {
  4454. for (int Index = 0; Index < Count; Index++)
  4455. Sessions[Index]->Selected = Select;
  4456. }
  4457. //---------------------------------------------------------------------
  4458. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  4459. bool OnlySelected, TList * Imported)
  4460. {
  4461. for (int Index = 0; Index < From->Count; Index++)
  4462. {
  4463. if (!OnlySelected || From->Sessions[Index]->Selected)
  4464. {
  4465. TSessionData *Session = new TSessionData(L"");
  4466. Session->Assign(From->Sessions[Index]);
  4467. Session->Modified = true;
  4468. Session->MakeUniqueIn(this);
  4469. Add(Session);
  4470. if (Imported != NULL)
  4471. {
  4472. Imported->Add(Session);
  4473. }
  4474. }
  4475. }
  4476. // only modified, explicit
  4477. Save(false, true);
  4478. }
  4479. //---------------------------------------------------------------------
  4480. void __fastcall TStoredSessionList::SelectSessionsToImport
  4481. (TStoredSessionList * Dest, bool SSHOnly)
  4482. {
  4483. for (int Index = 0; Index < Count; Index++)
  4484. {
  4485. Sessions[Index]->Selected =
  4486. (!SSHOnly || (Sessions[Index]->GetNormalizedPuttyProtocol() == PuttySshProtocol)) &&
  4487. !Dest->FindByName(Sessions[Index]->Name);
  4488. }
  4489. }
  4490. //---------------------------------------------------------------------
  4491. void __fastcall TStoredSessionList::Cleanup()
  4492. {
  4493. try
  4494. {
  4495. if (Configuration->Storage == stRegistry) Clear();
  4496. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  4497. try
  4498. {
  4499. Storage->AccessMode = smReadWrite;
  4500. if (Storage->OpenRootKey(False))
  4501. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  4502. }
  4503. __finally
  4504. {
  4505. delete Storage;
  4506. }
  4507. }
  4508. catch (Exception &E)
  4509. {
  4510. throw ExtException(&E, LoadStr(CLEANUP_SESSIONS_ERROR));
  4511. }
  4512. }
  4513. //---------------------------------------------------------------------------
  4514. void __fastcall TStoredSessionList::UpdateStaticUsage()
  4515. {
  4516. int SCP = 0;
  4517. int SFTP = 0;
  4518. int FTP = 0;
  4519. int FTPS = 0;
  4520. int WebDAV = 0;
  4521. int WebDAVS = 0;
  4522. int S3 = 0;
  4523. int Password = 0;
  4524. int Advanced = 0;
  4525. int Color = 0;
  4526. int Note = 0;
  4527. int Tunnel = 0;
  4528. bool Folders = false;
  4529. bool Workspaces = false;
  4530. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  4531. std::unique_ptr<TStringList> DifferentAdvancedProperties(CreateSortedStringList());
  4532. for (int Index = 0; Index < Count; Index++)
  4533. {
  4534. TSessionData * Data = Sessions[Index];
  4535. if (Data->IsWorkspace)
  4536. {
  4537. Workspaces = true;
  4538. }
  4539. else
  4540. {
  4541. switch (Data->FSProtocol)
  4542. {
  4543. case fsSCPonly:
  4544. SCP++;
  4545. break;
  4546. case fsSFTP:
  4547. case fsSFTPonly:
  4548. SFTP++;
  4549. break;
  4550. case fsFTP:
  4551. if (Data->Ftps == ftpsNone)
  4552. {
  4553. FTP++;
  4554. }
  4555. else
  4556. {
  4557. FTPS++;
  4558. }
  4559. break;
  4560. case fsWebDAV:
  4561. if (Data->Ftps == ftpsNone)
  4562. {
  4563. WebDAV++;
  4564. }
  4565. else
  4566. {
  4567. WebDAVS++;
  4568. }
  4569. break;
  4570. case fsS3:
  4571. S3++;
  4572. break;
  4573. }
  4574. if (Data->HasAnySessionPassword())
  4575. {
  4576. Password++;
  4577. }
  4578. if (Data->Color != 0)
  4579. {
  4580. Color++;
  4581. }
  4582. if (!Data->Note.IsEmpty())
  4583. {
  4584. Note++;
  4585. }
  4586. // This would not work for passwords, as they are compared in their encrypted form.
  4587. // But there are no passwords set in factory defaults anyway.
  4588. if (!Data->IsSame(FactoryDefaults.get(), true, DifferentAdvancedProperties.get(), false))
  4589. {
  4590. Advanced++;
  4591. }
  4592. if (Data->Tunnel)
  4593. {
  4594. Tunnel++;
  4595. }
  4596. if (!Data->FolderName.IsEmpty())
  4597. {
  4598. Folders = true;
  4599. }
  4600. }
  4601. }
  4602. Configuration->Usage->Set(L"StoredSessionsCountSCP", SCP);
  4603. Configuration->Usage->Set(L"StoredSessionsCountSFTP", SFTP);
  4604. Configuration->Usage->Set(L"StoredSessionsCountFTP", FTP);
  4605. Configuration->Usage->Set(L"StoredSessionsCountFTPS", FTPS);
  4606. Configuration->Usage->Set(L"StoredSessionsCountWebDAV", WebDAV);
  4607. Configuration->Usage->Set(L"StoredSessionsCountWebDAVS", WebDAVS);
  4608. Configuration->Usage->Set(L"StoredSessionsCountS3", S3);
  4609. Configuration->Usage->Set(L"StoredSessionsCountPassword", Password);
  4610. Configuration->Usage->Set(L"StoredSessionsCountColor", Color);
  4611. Configuration->Usage->Set(L"StoredSessionsCountNote", Note);
  4612. Configuration->Usage->Set(L"StoredSessionsCountAdvanced", Advanced);
  4613. DifferentAdvancedProperties->Delimiter = L',';
  4614. Configuration->Usage->Set(L"StoredSessionsAdvancedSettings", DifferentAdvancedProperties->DelimitedText);
  4615. Configuration->Usage->Set(L"StoredSessionsCountTunnel", Tunnel);
  4616. // actually default might be true, see below for when the default is actually used
  4617. bool CustomDefaultStoredSession = false;
  4618. try
  4619. {
  4620. // this can throw, when the default session settings have password set
  4621. // (and no other basic property, like hostname/username),
  4622. // and master password is enabled as we are called before master password
  4623. // handler is set
  4624. CustomDefaultStoredSession = !FDefaultSettings->IsSame(FactoryDefaults.get(), false);
  4625. }
  4626. catch (...)
  4627. {
  4628. }
  4629. Configuration->Usage->Set(L"UsingDefaultStoredSession", CustomDefaultStoredSession);
  4630. Configuration->Usage->Set(L"UsingStoredSessionsFolders", Folders);
  4631. Configuration->Usage->Set(L"UsingWorkspaces", Workspaces);
  4632. }
  4633. //---------------------------------------------------------------------------
  4634. TSessionData * __fastcall TStoredSessionList::FindSame(TSessionData * Data)
  4635. {
  4636. TSessionData * Result;
  4637. if (Data->Hidden || Data->Name.IsEmpty() || Data->IsWorkspace)
  4638. {
  4639. Result = NULL;
  4640. }
  4641. else
  4642. {
  4643. Result = dynamic_cast<TSessionData *>(FindByName(Data->Name));
  4644. }
  4645. return Result;
  4646. }
  4647. //---------------------------------------------------------------------------
  4648. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  4649. {
  4650. for (int Index = 0; Index < Count; Index++)
  4651. if (Data == Sessions[Index]) return Index;
  4652. return -1;
  4653. }
  4654. //---------------------------------------------------------------------------
  4655. TSessionData * __fastcall TStoredSessionList::NewSession(
  4656. UnicodeString SessionName, TSessionData * Session)
  4657. {
  4658. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  4659. if (!DuplicateSession)
  4660. {
  4661. DuplicateSession = new TSessionData(L"");
  4662. DuplicateSession->Assign(Session);
  4663. DuplicateSession->Name = SessionName;
  4664. // make sure, that new stored session is saved to registry
  4665. DuplicateSession->Modified = true;
  4666. Add(DuplicateSession);
  4667. }
  4668. else
  4669. {
  4670. DuplicateSession->Assign(Session);
  4671. DuplicateSession->Name = SessionName;
  4672. DuplicateSession->Modified = true;
  4673. }
  4674. // list was saved here before to default storage, but it would not allow
  4675. // to work with special lists (export/import) not using default storage
  4676. return DuplicateSession;
  4677. }
  4678. //---------------------------------------------------------------------------
  4679. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  4680. {
  4681. DebugAssert(FDefaultSettings);
  4682. if (FDefaultSettings != value)
  4683. {
  4684. FDefaultSettings->Assign(value);
  4685. // make sure default settings are saved
  4686. FDefaultSettings->Modified = true;
  4687. FDefaultSettings->Name = DefaultName;
  4688. if (!FReadOnly)
  4689. {
  4690. // only modified, explicit
  4691. Save(false, true);
  4692. }
  4693. }
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. bool __fastcall TStoredSessionList::OpenHostKeysSubKey(THierarchicalStorage * Storage, bool CanCreate)
  4697. {
  4698. return
  4699. Storage->OpenRootKey(CanCreate) &&
  4700. Storage->OpenSubKey(Configuration->SshHostKeysSubKey, CanCreate);
  4701. }
  4702. //---------------------------------------------------------------------------
  4703. THierarchicalStorage * __fastcall TStoredSessionList::CreateHostKeysStorageForWritting()
  4704. {
  4705. bool SessionList = false;
  4706. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateScpStorage(SessionList));
  4707. Storage->Explicit = true;
  4708. Storage->AccessMode = smReadWrite;
  4709. return Storage.release();
  4710. }
  4711. //---------------------------------------------------------------------------
  4712. void __fastcall TStoredSessionList::ImportHostKeys(
  4713. THierarchicalStorage * SourceStorage, THierarchicalStorage * TargetStorage, TStoredSessionList * Sessions, bool OnlySelected)
  4714. {
  4715. if (OpenHostKeysSubKey(SourceStorage, false) &&
  4716. OpenHostKeysSubKey(TargetStorage, true))
  4717. {
  4718. std::auto_ptr<TStringList> KeyList(new TStringList());
  4719. SourceStorage->GetValueNames(KeyList.get());
  4720. DebugAssert(Sessions != NULL);
  4721. for (int Index = 0; Index < Sessions->Count; Index++)
  4722. {
  4723. TSessionData * Session = Sessions->Sessions[Index];
  4724. if (!OnlySelected || Session->Selected)
  4725. {
  4726. UnicodeString HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostNameExpanded)));
  4727. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  4728. {
  4729. UnicodeString KeyName = KeyList->Strings[KeyIndex];
  4730. if (EndsText(HostKeyName, KeyName))
  4731. {
  4732. TargetStorage->WriteStringRaw(KeyName, SourceStorage->ReadStringRaw(KeyName, L""));
  4733. }
  4734. }
  4735. }
  4736. }
  4737. }
  4738. }
  4739. //---------------------------------------------------------------------------
  4740. void __fastcall TStoredSessionList::ImportHostKeys(
  4741. const UnicodeString & SourceKey, TStoredSessionList * Sessions, bool OnlySelected)
  4742. {
  4743. std::auto_ptr<THierarchicalStorage> TargetStorage(CreateHostKeysStorageForWritting());
  4744. std::auto_ptr<THierarchicalStorage> SourceStorage(new TRegistryStorage(SourceKey));
  4745. ImportHostKeys(SourceStorage.get(), TargetStorage.get(), Sessions, OnlySelected);
  4746. }
  4747. //---------------------------------------------------------------------------
  4748. void __fastcall TStoredSessionList::ImportSelectedKnownHosts(TStoredSessionList * Sessions)
  4749. {
  4750. std::unique_ptr<THierarchicalStorage> Storage(CreateHostKeysStorageForWritting());
  4751. if (OpenHostKeysSubKey(Storage.get(), true))
  4752. {
  4753. for (int Index = 0; Index < Sessions->Count; Index++)
  4754. {
  4755. TSessionData * Session = Sessions->Sessions[Index];
  4756. if (Session->Selected)
  4757. {
  4758. UnicodeString Algs;
  4759. UnicodeString HostKeys = Session->HostKey;
  4760. while (!HostKeys.IsEmpty())
  4761. {
  4762. UnicodeString HostKey = CutToChar(HostKeys, L';', true);
  4763. // skip alg
  4764. CutToChar(HostKey, L':', true);
  4765. UnicodeString Key = CutToChar(HostKey, L'=', true);
  4766. Storage->WriteStringRaw(Key, HostKey);
  4767. }
  4768. }
  4769. }
  4770. }
  4771. }
  4772. //---------------------------------------------------------------------------
  4773. bool __fastcall TStoredSessionList::IsFolderOrWorkspace(
  4774. const UnicodeString & Name, bool Workspace)
  4775. {
  4776. bool Result = false;
  4777. TSessionData * FirstData = NULL;
  4778. if (!Name.IsEmpty())
  4779. {
  4780. for (int Index = 0; !Result && (Index < Count); Index++)
  4781. {
  4782. Result = Sessions[Index]->IsInFolderOrWorkspace(Name);
  4783. if (Result)
  4784. {
  4785. FirstData = Sessions[Index];
  4786. }
  4787. }
  4788. }
  4789. return
  4790. Result &&
  4791. DebugAlwaysTrue(FirstData != NULL) &&
  4792. (FirstData->IsWorkspace == Workspace);
  4793. }
  4794. //---------------------------------------------------------------------------
  4795. bool __fastcall TStoredSessionList::IsFolder(const UnicodeString & Name)
  4796. {
  4797. return IsFolderOrWorkspace(Name, false);
  4798. }
  4799. //---------------------------------------------------------------------------
  4800. bool __fastcall TStoredSessionList::IsWorkspace(const UnicodeString & Name)
  4801. {
  4802. return IsFolderOrWorkspace(Name, true);
  4803. }
  4804. //---------------------------------------------------------------------------
  4805. TSessionData * __fastcall TStoredSessionList::CheckIsInFolderOrWorkspaceAndResolve(
  4806. TSessionData * Data, const UnicodeString & Name)
  4807. {
  4808. if (Data->IsInFolderOrWorkspace(Name))
  4809. {
  4810. Data = ResolveWorkspaceData(Data);
  4811. if ((Data != NULL) && Data->CanLogin &&
  4812. DebugAlwaysTrue(Data->Link.IsEmpty()))
  4813. {
  4814. return Data;
  4815. }
  4816. }
  4817. return NULL;
  4818. }
  4819. //---------------------------------------------------------------------------
  4820. void __fastcall TStoredSessionList::GetFolderOrWorkspace(const UnicodeString & Name, TList * List)
  4821. {
  4822. DoGetFolderOrWorkspace(Name, List, false);
  4823. }
  4824. //---------------------------------------------------------------------------
  4825. void __fastcall TStoredSessionList::DoGetFolderOrWorkspace(const UnicodeString & Name, TList * List, bool NoRecrypt)
  4826. {
  4827. for (int Index = 0; (Index < Count); Index++)
  4828. {
  4829. TSessionData * RawData = Sessions[Index];
  4830. TSessionData * Data =
  4831. CheckIsInFolderOrWorkspaceAndResolve(RawData, Name);
  4832. if (Data != NULL)
  4833. {
  4834. TSessionData * Data2 = new TSessionData(L"");
  4835. if (NoRecrypt)
  4836. {
  4837. Data2->CopyDataNoRecrypt(Data);
  4838. }
  4839. else
  4840. {
  4841. Data2->Assign(Data);
  4842. }
  4843. if (!RawData->Link.IsEmpty() && (DebugAlwaysTrue(Data != RawData)) &&
  4844. // BACKWARD COMPATIBILITY
  4845. // When loading pre-5.6.4 workspace, that does not have state saved,
  4846. // do not overwrite the site "state" defaults
  4847. // with (empty) workspace state
  4848. RawData->HasStateData())
  4849. {
  4850. Data2->CopyStateData(RawData);
  4851. }
  4852. if (!RawData->NameOverride.IsEmpty())
  4853. {
  4854. Data2->Name = RawData->NameOverride;
  4855. }
  4856. else if (RawData->Link.IsEmpty() && RawData->IsWorkspace)
  4857. {
  4858. // Newly opened ad-hoc session has no name, so restore the workspace that way too.
  4859. // Otherwise we would persist the generated internal workspace name as a real name.
  4860. Data2->Name = UnicodeString();
  4861. }
  4862. List->Add(Data2);
  4863. }
  4864. }
  4865. }
  4866. //---------------------------------------------------------------------------
  4867. TStrings * __fastcall TStoredSessionList::GetFolderOrWorkspaceList(
  4868. const UnicodeString & Name)
  4869. {
  4870. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4871. DoGetFolderOrWorkspace(Name, DataList.get(), true);
  4872. std::unique_ptr<TStringList> Result(new TStringList());
  4873. for (int Index = 0; (Index < DataList->Count); Index++)
  4874. {
  4875. Result->Add(dynamic_cast<TSessionData *>(DataList->Items[Index])->SessionName);
  4876. }
  4877. return Result.release();
  4878. }
  4879. //---------------------------------------------------------------------------
  4880. TStrings * __fastcall TStoredSessionList::GetWorkspaces()
  4881. {
  4882. std::unique_ptr<TStringList> Result(CreateSortedStringList());
  4883. for (int Index = 0; (Index < Count); Index++)
  4884. {
  4885. TSessionData * Data = Sessions[Index];
  4886. if (Data->IsWorkspace)
  4887. {
  4888. Result->Add(Data->FolderName);
  4889. }
  4890. }
  4891. return Result.release();
  4892. }
  4893. //---------------------------------------------------------------------------
  4894. void __fastcall TStoredSessionList::NewWorkspace(
  4895. UnicodeString Name, TList * DataList)
  4896. {
  4897. for (int Index = 0; (Index < Count); Index++)
  4898. {
  4899. TSessionData * Data = Sessions[Index];
  4900. if (Data->IsInFolderOrWorkspace(Name))
  4901. {
  4902. FPendingRemovals->Add(Data->Name);
  4903. Remove(Data);
  4904. Index--;
  4905. }
  4906. }
  4907. for (int Index = 0; (Index < DataList->Count); Index++)
  4908. {
  4909. TSessionData * Data = static_cast<TSessionData *>(DataList->Items[Index]);
  4910. TSessionData * Data2 = new TSessionData(L"");
  4911. Data2->Assign(Data);
  4912. Data2->Name = TSessionData::ComposePath(Name, Data->Name);
  4913. // make sure, that new stored session is saved to registry
  4914. Data2->Modified = true;
  4915. Add(Data2);
  4916. }
  4917. }
  4918. //---------------------------------------------------------------------------
  4919. bool __fastcall TStoredSessionList::HasAnyWorkspace()
  4920. {
  4921. bool Result = false;
  4922. for (int Index = 0; !Result && (Index < Count); Index++)
  4923. {
  4924. TSessionData * Data = Sessions[Index];
  4925. Result = Data->IsWorkspace;
  4926. }
  4927. return Result;
  4928. }
  4929. //---------------------------------------------------------------------------
  4930. TSessionData * __fastcall TStoredSessionList::ParseUrl(UnicodeString Url,
  4931. TOptions * Options, bool & DefaultsOnly, UnicodeString * FileName,
  4932. bool * AProtocolDefined, UnicodeString * MaskedUrl, int Flags)
  4933. {
  4934. TSessionData * Data = new TSessionData(L"");
  4935. try
  4936. {
  4937. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined, MaskedUrl, Flags);
  4938. }
  4939. catch(...)
  4940. {
  4941. delete Data;
  4942. throw;
  4943. }
  4944. return Data;
  4945. }
  4946. //---------------------------------------------------------------------
  4947. bool __fastcall TStoredSessionList::IsUrl(UnicodeString Url)
  4948. {
  4949. bool DefaultsOnly;
  4950. bool ProtocolDefined = false;
  4951. std::unique_ptr<TSessionData> ParsedData(ParseUrl(Url, NULL, DefaultsOnly, NULL, &ProtocolDefined));
  4952. bool Result = ProtocolDefined;
  4953. return Result;
  4954. }
  4955. //---------------------------------------------------------------------
  4956. TSessionData * __fastcall TStoredSessionList::ResolveWorkspaceData(TSessionData * Data)
  4957. {
  4958. if (!Data->Link.IsEmpty())
  4959. {
  4960. Data = dynamic_cast<TSessionData *>(FindByName(Data->Link));
  4961. if (Data != NULL)
  4962. {
  4963. Data = ResolveWorkspaceData(Data);
  4964. }
  4965. }
  4966. return Data;
  4967. }
  4968. //---------------------------------------------------------------------
  4969. TSessionData * __fastcall TStoredSessionList::SaveWorkspaceData(TSessionData * Data, int Index)
  4970. {
  4971. std::unique_ptr<TSessionData> Result(new TSessionData(L""));
  4972. TSessionData * SameData = StoredSessions->FindSame(Data);
  4973. if (SameData != NULL)
  4974. {
  4975. Result->CopyStateData(Data);
  4976. Result->Link = Data->Name;
  4977. }
  4978. else
  4979. {
  4980. Result->Assign(Data);
  4981. Result->NameOverride = Data->Name;
  4982. }
  4983. Result->IsWorkspace = true;
  4984. Result->Name = IntToHex(Index, 4); // See HasSessionName()
  4985. return Result.release();
  4986. }
  4987. //---------------------------------------------------------------------
  4988. bool __fastcall TStoredSessionList::CanLogin(TSessionData * Data)
  4989. {
  4990. Data = ResolveWorkspaceData(Data);
  4991. return (Data != NULL) && Data->CanLogin;
  4992. }
  4993. //---------------------------------------------------------------------
  4994. UnicodeString GetExpandedLogFileName(UnicodeString LogFileName, TDateTime Started, TSessionData * SessionData)
  4995. {
  4996. // StripPathQuotes should not be needed as we do not feed quotes anymore
  4997. UnicodeString ANewFileName = StripPathQuotes(ExpandEnvironmentVariables(LogFileName));
  4998. for (int Index = 1; Index < ANewFileName.Length(); Index++)
  4999. {
  5000. if (ANewFileName[Index] == L'!')
  5001. {
  5002. UnicodeString Replacement;
  5003. // keep consistent with TFileCustomCommand::PatternReplacement
  5004. switch (tolower(ANewFileName[Index + 1]))
  5005. {
  5006. case L'y':
  5007. Replacement = FormatDateTime(L"yyyy", Started);
  5008. break;
  5009. case L'm':
  5010. Replacement = FormatDateTime(L"mm", Started);
  5011. break;
  5012. case L'd':
  5013. Replacement = FormatDateTime(L"dd", Started);
  5014. break;
  5015. case L't':
  5016. Replacement = FormatDateTime(L"hhnnss", Started);
  5017. break;
  5018. case 'p':
  5019. Replacement = IntToStr(static_cast<int>(GetCurrentProcessId()));
  5020. break;
  5021. case L'@':
  5022. if (SessionData != NULL)
  5023. {
  5024. Replacement = MakeValidFileName(SessionData->HostNameExpanded);
  5025. }
  5026. else
  5027. {
  5028. Replacement = L"nohost";
  5029. }
  5030. break;
  5031. case L's':
  5032. if (SessionData != NULL)
  5033. {
  5034. Replacement = MakeValidFileName(SessionData->SessionName);
  5035. }
  5036. else
  5037. {
  5038. Replacement = L"nosession";
  5039. }
  5040. break;
  5041. case L'!':
  5042. Replacement = L"!";
  5043. break;
  5044. default:
  5045. Replacement = UnicodeString(L"!") + ANewFileName[Index + 1];
  5046. break;
  5047. }
  5048. ANewFileName.Delete(Index, 2);
  5049. ANewFileName.Insert(Replacement, Index);
  5050. Index += Replacement.Length() - 1;
  5051. }
  5052. }
  5053. return ANewFileName;
  5054. }
  5055. //---------------------------------------------------------------------
  5056. bool __fastcall IsSshProtocol(TFSProtocol FSProtocol)
  5057. {
  5058. return
  5059. (FSProtocol == fsSFTPonly) || (FSProtocol == fsSFTP) ||
  5060. (FSProtocol == fsSCPonly);
  5061. }
  5062. //---------------------------------------------------------------------------
  5063. int __fastcall DefaultPort(TFSProtocol FSProtocol, TFtps Ftps)
  5064. {
  5065. int Result;
  5066. switch (FSProtocol)
  5067. {
  5068. case fsFTP:
  5069. if (Ftps == ftpsImplicit)
  5070. {
  5071. Result = FtpsImplicitPortNumber;
  5072. }
  5073. else
  5074. {
  5075. Result = FtpPortNumber;
  5076. }
  5077. break;
  5078. case fsWebDAV:
  5079. case fsS3:
  5080. if (Ftps == ftpsNone)
  5081. {
  5082. Result = HTTPPortNumber;
  5083. }
  5084. else
  5085. {
  5086. Result = HTTPSPortNumber;
  5087. }
  5088. break;
  5089. default:
  5090. if (IsSshProtocol(FSProtocol))
  5091. {
  5092. Result = SshPortNumber;
  5093. }
  5094. else
  5095. {
  5096. DebugFail();
  5097. Result = -1;
  5098. }
  5099. break;
  5100. }
  5101. return Result;
  5102. }
  5103. //---------------------------------------------------------------------