SessionData.cpp 136 KB

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