SessionData.cpp 111 KB

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