SessionData.cpp 155 KB

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