SessionData.cpp 145 KB

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