SessionData.cpp 148 KB

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