SessionData.cpp 104 KB

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