SessionData.cpp 114 KB

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