SessionData.cpp 142 KB

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