SessionData.cpp 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341
  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"};
  24. const wchar_t KexNames[KEX_COUNT][20] = {L"WARN", L"dh-group1-sha1", L"dh-group14-sha1", L"dh-gex-sha1", L"rsa" };
  25. const wchar_t SshProtList[][10] = {L"1 only", L"1", L"2", L"2 only"};
  26. const TCipher DefaultCipherList[CIPHER_COUNT] =
  27. { cipAES, cipBlowfish, cip3DES, cipWarn, cipArcfour, cipDES };
  28. const TKex DefaultKexList[KEX_COUNT] =
  29. { 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. while (!SessionParams.IsEmpty())
  1479. {
  1480. UnicodeString SessionParam = CutToChar(SessionParams, UrlParamSeparator, false);
  1481. UnicodeString SessionParamName = CutToChar(SessionParam, UrlParamValueSeparator, false);
  1482. if (AnsiSameText(SessionParamName, UrlSaveParamName))
  1483. {
  1484. FSaveOnly = (StrToIntDef(SessionParam, 1) != 0);
  1485. }
  1486. }
  1487. if (MaskedUrl != NULL)
  1488. {
  1489. (*MaskedUrl) += RawUserName;
  1490. if (!UserInfo.IsEmpty())
  1491. {
  1492. (*MaskedUrl) += L":" + PasswordMask;
  1493. }
  1494. if (!RawUserName.IsEmpty() || !UserInfo.IsEmpty())
  1495. {
  1496. (*MaskedUrl) += L"@";
  1497. }
  1498. (*MaskedUrl) += OrigHostInfo + ARemoteDirectory;
  1499. }
  1500. }
  1501. if (!ARemoteDirectory.IsEmpty() && (ARemoteDirectory != L"/"))
  1502. {
  1503. if ((ARemoteDirectory[ARemoteDirectory.Length()] != L'/') &&
  1504. (FileName != NULL))
  1505. {
  1506. *FileName = DecodeUrlChars(UnixExtractFileName(ARemoteDirectory));
  1507. ARemoteDirectory = UnixExtractFilePath(ARemoteDirectory);
  1508. }
  1509. RemoteDirectory = DecodeUrlChars(ARemoteDirectory);
  1510. }
  1511. DefaultsOnly = false;
  1512. }
  1513. else
  1514. {
  1515. // This happens when pasting URL on Login dialog
  1516. if (StoredSessions != NULL)
  1517. {
  1518. CopyData(StoredSessions->DefaultSettings);
  1519. }
  1520. DefaultsOnly = true;
  1521. }
  1522. if (ProtocolDefined)
  1523. {
  1524. FSProtocol = AFSProtocol;
  1525. }
  1526. if (Options != NULL)
  1527. {
  1528. // we deliberately do keep defaultonly to false, in presence of any option,
  1529. // as the option should not make session "connectable"
  1530. UnicodeString Value;
  1531. if (Options->FindSwitch(SESSIONNAME_SWICH, Value))
  1532. {
  1533. Name = Value;
  1534. }
  1535. if (Options->FindSwitch(L"privatekey", Value))
  1536. {
  1537. PublicKeyFile = Value;
  1538. }
  1539. if (Options->FindSwitch(L"clientcert", Value))
  1540. {
  1541. TlsCertificateFile = Value;
  1542. }
  1543. if (Options->FindSwitch(PassphraseOption, Value))
  1544. {
  1545. Passphrase = Value;
  1546. }
  1547. if (Options->FindSwitch(L"timeout", Value))
  1548. {
  1549. Timeout = StrToInt(Value);
  1550. }
  1551. if (Options->FindSwitch(L"hostkey", Value) ||
  1552. Options->FindSwitch(L"certificate", Value))
  1553. {
  1554. HostKey = Value;
  1555. FOverrideCachedHostKey = true;
  1556. }
  1557. FtpPasvMode = Options->SwitchValue(L"passive", FtpPasvMode);
  1558. if (Options->FindSwitch(L"implicit"))
  1559. {
  1560. bool Enabled = Options->SwitchValue(L"implicit", true);
  1561. Ftps = Enabled ? ftpsImplicit : ftpsNone;
  1562. if (!PortNumberDefined && Enabled)
  1563. {
  1564. PortNumber = FtpsImplicitPortNumber;
  1565. }
  1566. }
  1567. // BACKWARD COMPATIBILITY with 5.5.x
  1568. if (Options->FindSwitch(L"explicitssl"))
  1569. {
  1570. bool Enabled = Options->SwitchValue(L"explicitssl", true);
  1571. Ftps = Enabled ? ftpsExplicitSsl : ftpsNone;
  1572. if (!PortNumberDefined && Enabled)
  1573. {
  1574. PortNumber = FtpPortNumber;
  1575. }
  1576. }
  1577. if (Options->FindSwitch(L"explicit") ||
  1578. // BACKWARD COMPATIBILITY with 5.5.x
  1579. Options->FindSwitch(L"explicittls"))
  1580. {
  1581. UnicodeString SwitchName =
  1582. Options->FindSwitch(L"explicit") ? L"explicit" : L"explicittls";
  1583. bool Enabled = Options->SwitchValue(SwitchName, true);
  1584. Ftps = Enabled ? ftpsExplicitTls : ftpsNone;
  1585. if (!PortNumberDefined && Enabled)
  1586. {
  1587. PortNumber = FtpPortNumber;
  1588. }
  1589. }
  1590. if (Options->FindSwitch(L"rawsettings"))
  1591. {
  1592. TStrings * RawSettings = NULL;
  1593. TOptionsStorage * OptionsStorage = NULL;
  1594. try
  1595. {
  1596. RawSettings = new TStringList();
  1597. if (Options->FindSwitch(L"rawsettings", RawSettings))
  1598. {
  1599. OptionsStorage = new TOptionsStorage(RawSettings, false);
  1600. ApplyRawSettings(OptionsStorage);
  1601. }
  1602. }
  1603. __finally
  1604. {
  1605. delete RawSettings;
  1606. delete OptionsStorage;
  1607. }
  1608. }
  1609. }
  1610. return true;
  1611. }
  1612. //---------------------------------------------------------------------
  1613. void __fastcall TSessionData::ApplyRawSettings(THierarchicalStorage * Storage)
  1614. {
  1615. bool Dummy;
  1616. DoLoad(Storage, Dummy);
  1617. }
  1618. //---------------------------------------------------------------------
  1619. void __fastcall TSessionData::ConfigureTunnel(int APortNumber)
  1620. {
  1621. FOrigHostName = HostName;
  1622. FOrigPortNumber = PortNumber;
  1623. FOrigProxyMethod = ProxyMethod;
  1624. HostName = L"127.0.0.1";
  1625. PortNumber = APortNumber;
  1626. // proxy settings is used for tunnel
  1627. ProxyMethod = ::pmNone;
  1628. }
  1629. //---------------------------------------------------------------------
  1630. void __fastcall TSessionData::RollbackTunnel()
  1631. {
  1632. HostName = FOrigHostName;
  1633. PortNumber = FOrigPortNumber;
  1634. ProxyMethod = FOrigProxyMethod;
  1635. }
  1636. //---------------------------------------------------------------------
  1637. void __fastcall TSessionData::ExpandEnvironmentVariables()
  1638. {
  1639. HostName = HostNameExpanded;
  1640. UserName = UserNameExpanded;
  1641. PublicKeyFile = ::ExpandEnvironmentVariables(PublicKeyFile);
  1642. }
  1643. //---------------------------------------------------------------------
  1644. void __fastcall TSessionData::ValidatePath(const UnicodeString Path)
  1645. {
  1646. // noop
  1647. }
  1648. //---------------------------------------------------------------------
  1649. void __fastcall TSessionData::ValidateName(const UnicodeString Name)
  1650. {
  1651. // keep consistent with MakeValidName
  1652. if (Name.LastDelimiter(L"/") > 0)
  1653. {
  1654. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, L"/")));
  1655. }
  1656. }
  1657. //---------------------------------------------------------------------
  1658. UnicodeString __fastcall TSessionData::MakeValidName(const UnicodeString & Name)
  1659. {
  1660. // keep consistent with ValidateName
  1661. return ReplaceStr(Name, L"/", L"\\");
  1662. }
  1663. //---------------------------------------------------------------------
  1664. RawByteString __fastcall TSessionData::EncryptPassword(const UnicodeString & Password, UnicodeString Key)
  1665. {
  1666. return Configuration->EncryptPassword(Password, Key);
  1667. }
  1668. //---------------------------------------------------------------------
  1669. RawByteString __fastcall TSessionData::StronglyRecryptPassword(const RawByteString & Password, UnicodeString Key)
  1670. {
  1671. return Configuration->StronglyRecryptPassword(Password, Key);
  1672. }
  1673. //---------------------------------------------------------------------
  1674. UnicodeString __fastcall TSessionData::DecryptPassword(const RawByteString & Password, UnicodeString Key)
  1675. {
  1676. UnicodeString Result;
  1677. try
  1678. {
  1679. Result = Configuration->DecryptPassword(Password, Key);
  1680. }
  1681. catch(EAbort &)
  1682. {
  1683. // silently ignore aborted prompts for master password and return empty password
  1684. }
  1685. return Result;
  1686. }
  1687. //---------------------------------------------------------------------
  1688. bool __fastcall TSessionData::GetCanLogin()
  1689. {
  1690. return !FHostName.IsEmpty();
  1691. }
  1692. //---------------------------------------------------------------------------
  1693. UnicodeString __fastcall TSessionData::GetSessionKey()
  1694. {
  1695. return FORMAT(L"%s@%s", (UserName, HostName));
  1696. }
  1697. //---------------------------------------------------------------------
  1698. UnicodeString __fastcall TSessionData::GetInternalStorageKey()
  1699. {
  1700. if (Name.IsEmpty())
  1701. {
  1702. return SessionKey;
  1703. }
  1704. else
  1705. {
  1706. return Name;
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------
  1710. UnicodeString __fastcall TSessionData::GetStorageKey()
  1711. {
  1712. return SessionName;
  1713. }
  1714. //---------------------------------------------------------------------
  1715. UnicodeString __fastcall TSessionData::FormatSiteKey(const UnicodeString & HostName, int PortNumber)
  1716. {
  1717. return FORMAT(L"%s:%d", (HostName, PortNumber));
  1718. }
  1719. //---------------------------------------------------------------------
  1720. UnicodeString __fastcall TSessionData::GetSiteKey()
  1721. {
  1722. return FormatSiteKey(HostNameExpanded, PortNumber);
  1723. }
  1724. //---------------------------------------------------------------------
  1725. void __fastcall TSessionData::SetHostName(UnicodeString value)
  1726. {
  1727. if (FHostName != value)
  1728. {
  1729. // HostName is key for password encryption
  1730. UnicodeString XPassword = Password;
  1731. // This is now hardly used as hostname is parsed directly on login dialog.
  1732. // But can be used when importing sites from PuTTY, as it allows same format too.
  1733. int P = value.LastDelimiter(L"@");
  1734. if (P > 0)
  1735. {
  1736. UserName = value.SubString(1, P - 1);
  1737. value = value.SubString(P + 1, value.Length() - P);
  1738. }
  1739. FHostName = value;
  1740. Modify();
  1741. Password = XPassword;
  1742. Shred(XPassword);
  1743. }
  1744. }
  1745. //---------------------------------------------------------------------
  1746. UnicodeString __fastcall TSessionData::GetHostNameExpanded()
  1747. {
  1748. return ::ExpandEnvironmentVariables(HostName);
  1749. }
  1750. //---------------------------------------------------------------------
  1751. void __fastcall TSessionData::SetPortNumber(int value)
  1752. {
  1753. SET_SESSION_PROPERTY(PortNumber);
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. void __fastcall TSessionData::SetShell(UnicodeString value)
  1757. {
  1758. SET_SESSION_PROPERTY(Shell);
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. void __fastcall TSessionData::SetSftpServer(UnicodeString value)
  1762. {
  1763. SET_SESSION_PROPERTY(SftpServer);
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. void __fastcall TSessionData::SetClearAliases(bool value)
  1767. {
  1768. SET_SESSION_PROPERTY(ClearAliases);
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. void __fastcall TSessionData::SetListingCommand(UnicodeString value)
  1772. {
  1773. SET_SESSION_PROPERTY(ListingCommand);
  1774. }
  1775. //---------------------------------------------------------------------------
  1776. void __fastcall TSessionData::SetIgnoreLsWarnings(bool value)
  1777. {
  1778. SET_SESSION_PROPERTY(IgnoreLsWarnings);
  1779. }
  1780. //---------------------------------------------------------------------------
  1781. void __fastcall TSessionData::SetUnsetNationalVars(bool value)
  1782. {
  1783. SET_SESSION_PROPERTY(UnsetNationalVars);
  1784. }
  1785. //---------------------------------------------------------------------
  1786. void __fastcall TSessionData::SetUserName(UnicodeString value)
  1787. {
  1788. // Avoid password recryption (what may popup master password prompt)
  1789. if (FUserName != value)
  1790. {
  1791. // UserName is key for password encryption
  1792. UnicodeString XPassword = Password;
  1793. SET_SESSION_PROPERTY(UserName);
  1794. Password = XPassword;
  1795. Shred(XPassword);
  1796. }
  1797. }
  1798. //---------------------------------------------------------------------
  1799. UnicodeString __fastcall TSessionData::GetUserNameExpanded()
  1800. {
  1801. return ::ExpandEnvironmentVariables(UserName);
  1802. }
  1803. //---------------------------------------------------------------------
  1804. void __fastcall TSessionData::SetPassword(UnicodeString avalue)
  1805. {
  1806. RawByteString value = EncryptPassword(avalue, UserName+HostName);
  1807. SET_SESSION_PROPERTY(Password);
  1808. }
  1809. //---------------------------------------------------------------------
  1810. UnicodeString __fastcall TSessionData::GetPassword() const
  1811. {
  1812. return DecryptPassword(FPassword, UserName+HostName);
  1813. }
  1814. //---------------------------------------------------------------------
  1815. void __fastcall TSessionData::SetPingInterval(int value)
  1816. {
  1817. SET_SESSION_PROPERTY(PingInterval);
  1818. }
  1819. //---------------------------------------------------------------------
  1820. void __fastcall TSessionData::SetTryAgent(bool value)
  1821. {
  1822. SET_SESSION_PROPERTY(TryAgent);
  1823. }
  1824. //---------------------------------------------------------------------
  1825. void __fastcall TSessionData::SetAgentFwd(bool value)
  1826. {
  1827. SET_SESSION_PROPERTY(AgentFwd);
  1828. }
  1829. //---------------------------------------------------------------------
  1830. void __fastcall TSessionData::SetAuthTIS(bool value)
  1831. {
  1832. SET_SESSION_PROPERTY(AuthTIS);
  1833. }
  1834. //---------------------------------------------------------------------
  1835. void __fastcall TSessionData::SetAuthKI(bool value)
  1836. {
  1837. SET_SESSION_PROPERTY(AuthKI);
  1838. }
  1839. //---------------------------------------------------------------------
  1840. void __fastcall TSessionData::SetAuthKIPassword(bool value)
  1841. {
  1842. SET_SESSION_PROPERTY(AuthKIPassword);
  1843. }
  1844. //---------------------------------------------------------------------
  1845. void __fastcall TSessionData::SetAuthGSSAPI(bool value)
  1846. {
  1847. SET_SESSION_PROPERTY(AuthGSSAPI);
  1848. }
  1849. //---------------------------------------------------------------------
  1850. void __fastcall TSessionData::SetGSSAPIFwdTGT(bool value)
  1851. {
  1852. SET_SESSION_PROPERTY(GSSAPIFwdTGT);
  1853. }
  1854. //---------------------------------------------------------------------
  1855. void __fastcall TSessionData::SetGSSAPIServerRealm(UnicodeString value)
  1856. {
  1857. SET_SESSION_PROPERTY(GSSAPIServerRealm);
  1858. }
  1859. //---------------------------------------------------------------------
  1860. void __fastcall TSessionData::SetChangeUsername(bool value)
  1861. {
  1862. SET_SESSION_PROPERTY(ChangeUsername);
  1863. }
  1864. //---------------------------------------------------------------------
  1865. void __fastcall TSessionData::SetCompression(bool value)
  1866. {
  1867. SET_SESSION_PROPERTY(Compression);
  1868. }
  1869. //---------------------------------------------------------------------
  1870. void __fastcall TSessionData::SetSshProt(TSshProt value)
  1871. {
  1872. SET_SESSION_PROPERTY(SshProt);
  1873. }
  1874. //---------------------------------------------------------------------
  1875. void __fastcall TSessionData::SetSsh2DES(bool value)
  1876. {
  1877. SET_SESSION_PROPERTY(Ssh2DES);
  1878. }
  1879. //---------------------------------------------------------------------
  1880. void __fastcall TSessionData::SetSshNoUserAuth(bool value)
  1881. {
  1882. SET_SESSION_PROPERTY(SshNoUserAuth);
  1883. }
  1884. //---------------------------------------------------------------------
  1885. UnicodeString __fastcall TSessionData::GetSshProtStr()
  1886. {
  1887. return SshProtList[FSshProt];
  1888. }
  1889. //---------------------------------------------------------------------
  1890. bool __fastcall TSessionData::GetUsesSsh()
  1891. {
  1892. return IsSshProtocol(FSProtocol);
  1893. }
  1894. //---------------------------------------------------------------------
  1895. void __fastcall TSessionData::SetCipher(int Index, TCipher value)
  1896. {
  1897. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  1898. SET_SESSION_PROPERTY(Ciphers[Index]);
  1899. }
  1900. //---------------------------------------------------------------------
  1901. TCipher __fastcall TSessionData::GetCipher(int Index) const
  1902. {
  1903. DebugAssert(Index >= 0 && Index < CIPHER_COUNT);
  1904. return FCiphers[Index];
  1905. }
  1906. //---------------------------------------------------------------------
  1907. void __fastcall TSessionData::SetCipherList(UnicodeString value)
  1908. {
  1909. bool Used[CIPHER_COUNT];
  1910. for (int C = 0; C < CIPHER_COUNT; C++) Used[C] = false;
  1911. UnicodeString CipherStr;
  1912. int Index = 0;
  1913. while (!value.IsEmpty() && (Index < CIPHER_COUNT))
  1914. {
  1915. CipherStr = CutToChar(value, L',', true);
  1916. for (int C = 0; C < CIPHER_COUNT; C++)
  1917. {
  1918. if (!CipherStr.CompareIC(CipherNames[C]))
  1919. {
  1920. Cipher[Index] = (TCipher)C;
  1921. Used[C] = true;
  1922. Index++;
  1923. break;
  1924. }
  1925. }
  1926. }
  1927. for (int C = 0; C < CIPHER_COUNT && Index < CIPHER_COUNT; C++)
  1928. {
  1929. if (!Used[DefaultCipherList[C]]) Cipher[Index++] = DefaultCipherList[C];
  1930. }
  1931. }
  1932. //---------------------------------------------------------------------
  1933. UnicodeString __fastcall TSessionData::GetCipherList() const
  1934. {
  1935. UnicodeString Result;
  1936. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  1937. {
  1938. Result += UnicodeString(Index ? L"," : L"") + CipherNames[Cipher[Index]];
  1939. }
  1940. return Result;
  1941. }
  1942. //---------------------------------------------------------------------
  1943. void __fastcall TSessionData::SetKex(int Index, TKex value)
  1944. {
  1945. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  1946. SET_SESSION_PROPERTY(Kex[Index]);
  1947. }
  1948. //---------------------------------------------------------------------
  1949. TKex __fastcall TSessionData::GetKex(int Index) const
  1950. {
  1951. DebugAssert(Index >= 0 && Index < KEX_COUNT);
  1952. return FKex[Index];
  1953. }
  1954. //---------------------------------------------------------------------
  1955. void __fastcall TSessionData::SetKexList(UnicodeString value)
  1956. {
  1957. bool Used[KEX_COUNT];
  1958. for (int K = 0; K < KEX_COUNT; K++) Used[K] = false;
  1959. UnicodeString KexStr;
  1960. int Index = 0;
  1961. while (!value.IsEmpty() && (Index < KEX_COUNT))
  1962. {
  1963. KexStr = CutToChar(value, L',', true);
  1964. for (int K = 0; K < KEX_COUNT; K++)
  1965. {
  1966. if (!KexStr.CompareIC(KexNames[K]))
  1967. {
  1968. Kex[Index] = (TKex)K;
  1969. Used[K] = true;
  1970. Index++;
  1971. break;
  1972. }
  1973. }
  1974. }
  1975. for (int K = 0; K < KEX_COUNT && Index < KEX_COUNT; K++)
  1976. {
  1977. if (!Used[DefaultKexList[K]]) Kex[Index++] = DefaultKexList[K];
  1978. }
  1979. }
  1980. //---------------------------------------------------------------------
  1981. UnicodeString __fastcall TSessionData::GetKexList() const
  1982. {
  1983. UnicodeString Result;
  1984. for (int Index = 0; Index < KEX_COUNT; Index++)
  1985. {
  1986. Result += UnicodeString(Index ? L"," : L"") + KexNames[Kex[Index]];
  1987. }
  1988. return Result;
  1989. }
  1990. //---------------------------------------------------------------------
  1991. void __fastcall TSessionData::SetPublicKeyFile(UnicodeString value)
  1992. {
  1993. if (FPublicKeyFile != value)
  1994. {
  1995. // PublicKeyFile is key for Passphrase encryption
  1996. UnicodeString XPassphrase = Passphrase;
  1997. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1998. FPublicKeyFile = StripPathQuotes(value);
  1999. Modify();
  2000. Passphrase = XPassphrase;
  2001. Shred(XPassphrase);
  2002. }
  2003. }
  2004. //---------------------------------------------------------------------
  2005. void __fastcall TSessionData::SetPassphrase(UnicodeString avalue)
  2006. {
  2007. RawByteString value = EncryptPassword(avalue, PublicKeyFile);
  2008. SET_SESSION_PROPERTY(Passphrase);
  2009. }
  2010. //---------------------------------------------------------------------
  2011. UnicodeString __fastcall TSessionData::GetPassphrase() const
  2012. {
  2013. return DecryptPassword(FPassphrase, PublicKeyFile);
  2014. }
  2015. //---------------------------------------------------------------------
  2016. void __fastcall TSessionData::SetReturnVar(UnicodeString value)
  2017. {
  2018. SET_SESSION_PROPERTY(ReturnVar);
  2019. }
  2020. //---------------------------------------------------------------------------
  2021. void __fastcall TSessionData::SetLookupUserGroups(TAutoSwitch value)
  2022. {
  2023. SET_SESSION_PROPERTY(LookupUserGroups);
  2024. }
  2025. //---------------------------------------------------------------------------
  2026. void __fastcall TSessionData::SetEOLType(TEOLType value)
  2027. {
  2028. SET_SESSION_PROPERTY(EOLType);
  2029. }
  2030. //---------------------------------------------------------------------------
  2031. void __fastcall TSessionData::SetTrimVMSVersions(bool value)
  2032. {
  2033. SET_SESSION_PROPERTY(TrimVMSVersions);
  2034. }
  2035. //---------------------------------------------------------------------------
  2036. TDateTime __fastcall TSessionData::GetTimeoutDT()
  2037. {
  2038. return SecToDateTime(Timeout);
  2039. }
  2040. //---------------------------------------------------------------------------
  2041. void __fastcall TSessionData::SetTimeout(int value)
  2042. {
  2043. SET_SESSION_PROPERTY(Timeout);
  2044. }
  2045. //---------------------------------------------------------------------------
  2046. void __fastcall TSessionData::SetFSProtocol(TFSProtocol value)
  2047. {
  2048. SET_SESSION_PROPERTY(FSProtocol);
  2049. }
  2050. //---------------------------------------------------------------------
  2051. UnicodeString __fastcall TSessionData::GetFSProtocolStr()
  2052. {
  2053. DebugAssert(FSProtocol >= 0 && FSProtocol < FSPROTOCOL_COUNT);
  2054. return FSProtocolNames[FSProtocol];
  2055. }
  2056. //---------------------------------------------------------------------------
  2057. void __fastcall TSessionData::SetDetectReturnVar(bool value)
  2058. {
  2059. if (value != DetectReturnVar)
  2060. {
  2061. ReturnVar = value ? L"" : L"$?";
  2062. }
  2063. }
  2064. //---------------------------------------------------------------------------
  2065. bool __fastcall TSessionData::GetDetectReturnVar()
  2066. {
  2067. return ReturnVar.IsEmpty();
  2068. }
  2069. //---------------------------------------------------------------------------
  2070. void __fastcall TSessionData::SetDefaultShell(bool value)
  2071. {
  2072. if (value != DefaultShell)
  2073. {
  2074. Shell = value ? L"" : L"/bin/bash";
  2075. }
  2076. }
  2077. //---------------------------------------------------------------------------
  2078. bool __fastcall TSessionData::GetDefaultShell()
  2079. {
  2080. return Shell.IsEmpty();
  2081. }
  2082. //---------------------------------------------------------------------------
  2083. void __fastcall TSessionData::SetPuttyProtocol(UnicodeString value)
  2084. {
  2085. SET_SESSION_PROPERTY(PuttyProtocol);
  2086. }
  2087. //---------------------------------------------------------------------
  2088. UnicodeString __fastcall TSessionData::GetNormalizedPuttyProtocol() const
  2089. {
  2090. return DefaultStr(PuttyProtocol, PuttySshProtocol);
  2091. }
  2092. //---------------------------------------------------------------------
  2093. void __fastcall TSessionData::SetPingIntervalDT(TDateTime value)
  2094. {
  2095. unsigned short hour, min, sec, msec;
  2096. value.DecodeTime(&hour, &min, &sec, &msec);
  2097. PingInterval = ((int)hour)*SecsPerHour + ((int)min)*SecsPerMin + sec;
  2098. }
  2099. //---------------------------------------------------------------------------
  2100. TDateTime __fastcall TSessionData::GetPingIntervalDT()
  2101. {
  2102. return SecToDateTime(PingInterval);
  2103. }
  2104. //---------------------------------------------------------------------------
  2105. void __fastcall TSessionData::SetPingType(TPingType value)
  2106. {
  2107. SET_SESSION_PROPERTY(PingType);
  2108. }
  2109. //---------------------------------------------------------------------------
  2110. void __fastcall TSessionData::SetAddressFamily(TAddressFamily value)
  2111. {
  2112. SET_SESSION_PROPERTY(AddressFamily);
  2113. }
  2114. //---------------------------------------------------------------------------
  2115. void __fastcall TSessionData::SetRekeyData(UnicodeString value)
  2116. {
  2117. SET_SESSION_PROPERTY(RekeyData);
  2118. }
  2119. //---------------------------------------------------------------------------
  2120. void __fastcall TSessionData::SetRekeyTime(unsigned int value)
  2121. {
  2122. SET_SESSION_PROPERTY(RekeyTime);
  2123. }
  2124. //---------------------------------------------------------------------
  2125. UnicodeString __fastcall TSessionData::GetDefaultSessionName()
  2126. {
  2127. if (!HostName.IsEmpty() && !UserName.IsEmpty())
  2128. {
  2129. // If we ever choose to include port number,
  2130. // we have to escape IPv6 literals in HostName
  2131. return FORMAT(L"%s@%s", (UserName, HostName));
  2132. }
  2133. else if (!HostName.IsEmpty())
  2134. {
  2135. return HostName;
  2136. }
  2137. else
  2138. {
  2139. return L"session";
  2140. }
  2141. }
  2142. //---------------------------------------------------------------------
  2143. UnicodeString __fastcall TSessionData::GetNameWithoutHiddenPrefix()
  2144. {
  2145. UnicodeString Result = Name;
  2146. if (Hidden)
  2147. {
  2148. Result = Result.SubString(TNamedObjectList::HiddenPrefix.Length() + 1, Result.Length() - TNamedObjectList::HiddenPrefix.Length());
  2149. }
  2150. return Result;
  2151. }
  2152. //---------------------------------------------------------------------
  2153. bool __fastcall TSessionData::HasSessionName()
  2154. {
  2155. return (!GetNameWithoutHiddenPrefix().IsEmpty() && (Name != DefaultName) && !IsWorkspace);
  2156. }
  2157. //---------------------------------------------------------------------
  2158. UnicodeString __fastcall TSessionData::GetSessionName()
  2159. {
  2160. UnicodeString Result;
  2161. if (HasSessionName())
  2162. {
  2163. Result = GetNameWithoutHiddenPrefix();
  2164. }
  2165. else
  2166. {
  2167. Result = DefaultSessionName;
  2168. }
  2169. return Result;
  2170. }
  2171. //---------------------------------------------------------------------
  2172. bool __fastcall TSessionData::IsSecure()
  2173. {
  2174. bool Result;
  2175. switch (FSProtocol)
  2176. {
  2177. case fsSCPonly:
  2178. case fsSFTP:
  2179. case fsSFTPonly:
  2180. Result = true;
  2181. break;
  2182. case fsFTP:
  2183. case fsWebDAV:
  2184. Result = (Ftps != ftpsNone);
  2185. break;
  2186. default:
  2187. DebugFail();
  2188. break;
  2189. }
  2190. return Result;
  2191. }
  2192. //---------------------------------------------------------------------
  2193. UnicodeString __fastcall TSessionData::GetProtocolUrl()
  2194. {
  2195. UnicodeString Url;
  2196. switch (FSProtocol)
  2197. {
  2198. case fsSCPonly:
  2199. Url = ScpProtocol;
  2200. break;
  2201. default:
  2202. DebugFail();
  2203. // fallback
  2204. case fsSFTP:
  2205. case fsSFTPonly:
  2206. Url = SftpProtocol;
  2207. break;
  2208. case fsFTP:
  2209. if (Ftps == ftpsImplicit)
  2210. {
  2211. Url = FtpsProtocol;
  2212. }
  2213. else if ((Ftps == ftpsExplicitTls) || (Ftps == ftpsExplicitSsl))
  2214. {
  2215. Url = FtpesProtocol;
  2216. }
  2217. else
  2218. {
  2219. Url = FtpProtocol;
  2220. }
  2221. break;
  2222. case fsWebDAV:
  2223. if (Ftps == ftpsImplicit)
  2224. {
  2225. Url = WebDAVSProtocol;
  2226. }
  2227. else
  2228. {
  2229. Url = WebDAVProtocol;
  2230. }
  2231. break;
  2232. }
  2233. Url += ProtocolSeparator;
  2234. return Url;
  2235. }
  2236. //---------------------------------------------------------------------
  2237. static bool __fastcall IsIPv6Literal(const UnicodeString & HostName)
  2238. {
  2239. bool Result = (HostName.Pos(L":") > 0);
  2240. if (Result)
  2241. {
  2242. for (int Index = 1; Result && (Index <= HostName.Length()); Index++)
  2243. {
  2244. wchar_t C = HostName[Index];
  2245. Result = IsHex(C) || (C == L':');
  2246. }
  2247. }
  2248. return Result;
  2249. }
  2250. //---------------------------------------------------------------------
  2251. UnicodeString __fastcall TSessionData::GenerateSessionUrl(unsigned int Flags)
  2252. {
  2253. UnicodeString Url;
  2254. if (FLAGSET(Flags, sufSpecific))
  2255. {
  2256. Url += WinSCPProtocolPrefix;
  2257. }
  2258. Url += GetProtocolUrl();
  2259. if (FLAGSET(Flags, sufUserName) && !UserNameExpanded.IsEmpty())
  2260. {
  2261. Url += EncodeUrlString(UserNameExpanded);
  2262. if (FLAGSET(Flags, sufPassword) && !Password.IsEmpty())
  2263. {
  2264. Url += L":" + EncodeUrlString(Password);
  2265. }
  2266. if (FLAGSET(Flags, sufHostKey) && !HostKey.IsEmpty())
  2267. {
  2268. Url +=
  2269. UnicodeString(UrlParamSeparator) + UrlHostKeyParamName +
  2270. UnicodeString(UrlParamValueSeparator) + NormalizeFingerprint(HostKey);
  2271. }
  2272. Url += L"@";
  2273. }
  2274. DebugAssert(!HostNameExpanded.IsEmpty());
  2275. if (IsIPv6Literal(HostNameExpanded))
  2276. {
  2277. Url += L"[" + HostNameExpanded + L"]";
  2278. }
  2279. else
  2280. {
  2281. Url += EncodeUrlString(HostNameExpanded);
  2282. }
  2283. if (PortNumber != DefaultPort(FSProtocol, Ftps))
  2284. {
  2285. Url += L":" + IntToStr(PortNumber);
  2286. }
  2287. Url += L"/";
  2288. return Url;
  2289. }
  2290. //---------------------------------------------------------------------
  2291. void __fastcall TSessionData::AddSwitch(UnicodeString & Result, const UnicodeString & Switch)
  2292. {
  2293. Result += FORMAT(L" -%s", (Switch));
  2294. }
  2295. //---------------------------------------------------------------------
  2296. void __fastcall TSessionData::AddSwitchValue(UnicodeString & Result, const UnicodeString & Name, const UnicodeString & Value)
  2297. {
  2298. AddSwitch(Result, FORMAT(L"%s=%s", (Name, Value)));
  2299. }
  2300. //---------------------------------------------------------------------
  2301. void __fastcall TSessionData::AddSwitch(UnicodeString & Result, const UnicodeString & Name, const UnicodeString & Value)
  2302. {
  2303. AddSwitchValue(Result, Name, FORMAT("\"%s\"", (EscapeParam(Value))));
  2304. }
  2305. //---------------------------------------------------------------------
  2306. void __fastcall TSessionData::AddSwitch(UnicodeString & Result, const UnicodeString & Name, int Value)
  2307. {
  2308. AddSwitchValue(Result, Name, IntToStr(Value));
  2309. }
  2310. //---------------------------------------------------------------------
  2311. void __fastcall TSessionData::LookupLastFingerprint()
  2312. {
  2313. UnicodeString FingerprintType;
  2314. if (IsSshProtocol(FSProtocol))
  2315. {
  2316. FingerprintType = SshFingerprintType;
  2317. }
  2318. else if (Ftps != ftpsNone)
  2319. {
  2320. FingerprintType = TlsFingerprintType;
  2321. }
  2322. if (!FingerprintType.IsEmpty())
  2323. {
  2324. HostKey = Configuration->LastFingerprint(SiteKey, FingerprintType);
  2325. }
  2326. }
  2327. //---------------------------------------------------------------------
  2328. UnicodeString __fastcall TSessionData::GenerateOpenCommandArgs()
  2329. {
  2330. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  2331. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  2332. SessionData->Assign(this);
  2333. UnicodeString Result = SessionData->GenerateSessionUrl(sufOpen);
  2334. // Before we reset the FSProtocol
  2335. bool AUsesSsh = SessionData->UsesSsh;
  2336. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  2337. if (SessionData->FSProtocol != fsSFTPonly)
  2338. {
  2339. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  2340. }
  2341. SessionData->HostName = FactoryDefaults->HostName;
  2342. SessionData->PortNumber = FactoryDefaults->PortNumber;
  2343. SessionData->UserName = FactoryDefaults->UserName;
  2344. SessionData->Password = FactoryDefaults->Password;
  2345. SessionData->CopyNonCoreData(FactoryDefaults.get());
  2346. SessionData->Ftps = FactoryDefaults->Ftps;
  2347. if (SessionData->HostKey != FactoryDefaults->HostKey)
  2348. {
  2349. UnicodeString SwitchName = AUsesSsh ? L"hostkey" : L"certificate";
  2350. AddSwitch(Result, SwitchName, SessionData->HostKey);
  2351. SessionData->HostKey = FactoryDefaults->HostKey;
  2352. }
  2353. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  2354. {
  2355. AddSwitch(Result, L"privatekey", SessionData->PublicKeyFile);
  2356. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  2357. }
  2358. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  2359. {
  2360. AddSwitch(Result, L"clientcert", SessionData->TlsCertificateFile);
  2361. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  2362. }
  2363. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  2364. {
  2365. AddSwitch(Result, PassphraseOption, SessionData->Passphrase);
  2366. SessionData->Passphrase = FactoryDefaults->Passphrase;
  2367. }
  2368. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  2369. {
  2370. AddSwitch(Result, L"passive", SessionData->FtpPasvMode ? 1 : 0);
  2371. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  2372. }
  2373. if (SessionData->Timeout != FactoryDefaults->Timeout)
  2374. {
  2375. AddSwitch(Result, L"timeout", SessionData->Timeout);
  2376. SessionData->Timeout = FactoryDefaults->Timeout;
  2377. }
  2378. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get()));
  2379. if (RawSettings->Count > 0)
  2380. {
  2381. AddSwitch(Result, L"rawsettings");
  2382. for (int Index = 0; Index < RawSettings->Count; Index++)
  2383. {
  2384. UnicodeString Name = RawSettings->Names[Index];
  2385. UnicodeString Value = RawSettings->ValueFromIndex[Index];
  2386. // Do not quote if it is all-numeric
  2387. if (IntToStr(StrToIntDef(Value, -1)) != Value)
  2388. {
  2389. Value = FORMAT(L"\"%s\"", (EscapeParam(Value)));
  2390. }
  2391. Result += FORMAT(L" %s=%s", (Name, Value));
  2392. }
  2393. }
  2394. return Result;
  2395. }
  2396. //---------------------------------------------------------------------
  2397. void __fastcall TSessionData::AddAssemblyProperty(
  2398. UnicodeString & Result, TAssemblyLanguage Language,
  2399. const UnicodeString & Name, const UnicodeString & Type,
  2400. const UnicodeString & Member)
  2401. {
  2402. UnicodeString PropertyCode;
  2403. switch (Language)
  2404. {
  2405. case alCSharp:
  2406. PropertyCode = L" %s = %s.%s,\n";
  2407. break;
  2408. case alVBNET:
  2409. PropertyCode = L" .%s = %s.%s\n";
  2410. break;
  2411. case alPowerShell:
  2412. PropertyCode = L"$sessionOptions.%s = [WinSCP.%s]::%s\n";
  2413. break;
  2414. }
  2415. Result += FORMAT(PropertyCode, (Name, Type, Member));
  2416. }
  2417. //---------------------------------------------------------------------
  2418. UnicodeString __fastcall TSessionData::AssemblyString(TAssemblyLanguage Language, UnicodeString S)
  2419. {
  2420. switch (Language)
  2421. {
  2422. case alCSharp:
  2423. if (S.Pos(L"\\") > 0)
  2424. {
  2425. S = FORMAT(L"@\"%s\"", (ReplaceStr(S, L"\"", L"\"\"")));
  2426. }
  2427. else
  2428. {
  2429. S = FORMAT(L"\"%s\"", (ReplaceStr(S, L"\"", L"\\\"")));
  2430. }
  2431. break;
  2432. case alVBNET:
  2433. S = FORMAT(L"\"%s\"", (ReplaceStr(S, L"\"", L"\"\"")));
  2434. break;
  2435. case alPowerShell:
  2436. S = FORMAT(L"\"%s\"", (ReplaceStr(S, L"\"", L"`\"")));
  2437. break;
  2438. default:
  2439. DebugFail();
  2440. break;
  2441. }
  2442. return S;
  2443. }
  2444. //---------------------------------------------------------------------
  2445. void __fastcall TSessionData::AddAssemblyPropertyRaw(
  2446. UnicodeString & Result, TAssemblyLanguage Language,
  2447. const UnicodeString & Name, const UnicodeString & Value)
  2448. {
  2449. UnicodeString PropertyCode;
  2450. switch (Language)
  2451. {
  2452. case alCSharp:
  2453. PropertyCode = L" %s = %s,\n";
  2454. break;
  2455. case alVBNET:
  2456. PropertyCode = L" .%s = %s\n";
  2457. break;
  2458. case alPowerShell:
  2459. PropertyCode = L"$sessionOptions.%s = %s\n";
  2460. break;
  2461. }
  2462. Result += FORMAT(PropertyCode, (Name, Value));
  2463. }
  2464. //---------------------------------------------------------------------
  2465. void __fastcall TSessionData::AddAssemblyProperty(
  2466. UnicodeString & Result, TAssemblyLanguage Language,
  2467. const UnicodeString & Name, UnicodeString Value)
  2468. {
  2469. AddAssemblyPropertyRaw(Result, Language, Name, AssemblyString(Language, Value));
  2470. }
  2471. //---------------------------------------------------------------------
  2472. void __fastcall TSessionData::AddAssemblyProperty(
  2473. UnicodeString & Result, TAssemblyLanguage Language,
  2474. const UnicodeString & Name, int Value)
  2475. {
  2476. AddAssemblyPropertyRaw(Result, Language, Name, IntToStr(Value));
  2477. }
  2478. //---------------------------------------------------------------------
  2479. void __fastcall TSessionData::AddAssemblyProperty(
  2480. UnicodeString & Result, TAssemblyLanguage Language,
  2481. const UnicodeString & Name, bool Value)
  2482. {
  2483. UnicodeString PropertyValue;
  2484. switch (Language)
  2485. {
  2486. case alCSharp:
  2487. PropertyValue = (Value ? L"true" : L"false");
  2488. break;
  2489. case alVBNET:
  2490. PropertyValue = (Value ? L"True" : L"False");
  2491. break;
  2492. case alPowerShell:
  2493. PropertyValue = (Value ? L"$True" : L"$False");
  2494. break;
  2495. }
  2496. AddAssemblyPropertyRaw(Result, Language, Name, PropertyValue);
  2497. }
  2498. //---------------------------------------------------------------------
  2499. UnicodeString __fastcall TSessionData::GenerateAssemblyCode(
  2500. TAssemblyLanguage Language)
  2501. {
  2502. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  2503. std::unique_ptr<TSessionData> SessionData(Clone());
  2504. UnicodeString Result;
  2505. UnicodeString SessionOptionsPreamble;
  2506. switch (Language)
  2507. {
  2508. case alCSharp:
  2509. SessionOptionsPreamble =
  2510. L"// %s\n"
  2511. "SessionOptions sessionOptions = new SessionOptions\n"
  2512. "{\n";
  2513. break;
  2514. case alVBNET:
  2515. SessionOptionsPreamble =
  2516. L"' %s\n"
  2517. "Dim mySessionOptions As New SessionOptions\n"
  2518. "With mySessionOptions\n";
  2519. break;
  2520. case alPowerShell:
  2521. SessionOptionsPreamble =
  2522. FORMAT(L"# %s\n", (LoadStr(CODE_PS_ADD_TYPE))) +
  2523. L"Add-Type -Path \"WinSCPnet.dll\"\n"
  2524. "\n"
  2525. "# %s\n"
  2526. "$sessionOptions = New-Object WinSCP.SessionOptions\n";
  2527. break;
  2528. default:
  2529. DebugFail();
  2530. break;
  2531. }
  2532. Result = FORMAT(SessionOptionsPreamble, (LoadStr(CODE_SESSION_OPTIONS)));
  2533. UnicodeString ProtocolMember;
  2534. switch (SessionData->FSProtocol)
  2535. {
  2536. case fsSCPonly:
  2537. ProtocolMember = "Scp";
  2538. break;
  2539. default:
  2540. DebugFail();
  2541. // fallback
  2542. case fsSFTP:
  2543. case fsSFTPonly:
  2544. ProtocolMember = "Sftp";
  2545. break;
  2546. case fsFTP:
  2547. ProtocolMember = "Ftp";
  2548. break;
  2549. case fsWebDAV:
  2550. ProtocolMember = "Webdav";
  2551. break;
  2552. }
  2553. // Before we reset the FSProtocol
  2554. bool AUsesSsh = SessionData->UsesSsh;
  2555. // Protocol is set unconditionally, we want even the default SFTP
  2556. AddAssemblyProperty(Result, Language, L"Protocol", L"Protocol", ProtocolMember);
  2557. // SFTP-only is not reflected by the protocol prefix, we have to use rawsettings for that
  2558. if (SessionData->FSProtocol != fsSFTPonly)
  2559. {
  2560. SessionData->FSProtocol = FactoryDefaults->FSProtocol;
  2561. }
  2562. if (SessionData->HostName != FactoryDefaults->HostName)
  2563. {
  2564. AddAssemblyProperty(Result, Language, L"HostName", HostName);
  2565. SessionData->HostName = FactoryDefaults->HostName;
  2566. }
  2567. if (SessionData->PortNumber != FactoryDefaults->PortNumber)
  2568. {
  2569. AddAssemblyProperty(Result, Language, L"PortNumber", PortNumber);
  2570. SessionData->PortNumber = FactoryDefaults->PortNumber;
  2571. }
  2572. if (SessionData->UserName != FactoryDefaults->UserName)
  2573. {
  2574. AddAssemblyProperty(Result, Language, L"UserName", UserName);
  2575. SessionData->UserName = FactoryDefaults->UserName;
  2576. }
  2577. if (SessionData->Password != FactoryDefaults->Password)
  2578. {
  2579. AddAssemblyProperty(Result, Language, L"Password", Password);
  2580. SessionData->Password = FactoryDefaults->Password;
  2581. }
  2582. SessionData->CopyNonCoreData(FactoryDefaults.get());
  2583. if (SessionData->Ftps != FactoryDefaults->Ftps)
  2584. {
  2585. // SessionData->FSProtocol is reset already
  2586. switch (FSProtocol)
  2587. {
  2588. case fsFTP:
  2589. {
  2590. UnicodeString FtpSecureMember;
  2591. switch (SessionData->Ftps)
  2592. {
  2593. case ftpsNone:
  2594. // noop
  2595. break;
  2596. case ftpsImplicit:
  2597. FtpSecureMember = L"Implicit";
  2598. break;
  2599. case ftpsExplicitTls:
  2600. case ftpsExplicitSsl:
  2601. FtpSecureMember = L"Explicit";
  2602. break;
  2603. default:
  2604. DebugFail();
  2605. break;
  2606. }
  2607. AddAssemblyProperty(Result, Language, L"FtpSecure", L"FtpSecure", FtpSecureMember);
  2608. }
  2609. break;
  2610. case fsWebDAV:
  2611. AddAssemblyProperty(Result, Language, L"WebdavSecure", (SessionData->Ftps != ftpsNone));
  2612. break;
  2613. default:
  2614. DebugFail();
  2615. break;
  2616. }
  2617. SessionData->Ftps = FactoryDefaults->Ftps;
  2618. }
  2619. if (SessionData->HostKey != FactoryDefaults->HostKey)
  2620. {
  2621. UnicodeString PropertyName = AUsesSsh ? L"SshHostKeyFingerprint" : L"TlsHostCertificateFingerprint";
  2622. AddAssemblyProperty(Result, Language, PropertyName, SessionData->HostKey);
  2623. SessionData->HostKey = FactoryDefaults->HostKey;
  2624. }
  2625. if (SessionData->PublicKeyFile != FactoryDefaults->PublicKeyFile)
  2626. {
  2627. AddAssemblyProperty(Result, Language, L"SshPrivateKeyPath", SessionData->PublicKeyFile);
  2628. SessionData->PublicKeyFile = FactoryDefaults->PublicKeyFile;
  2629. }
  2630. if (SessionData->TlsCertificateFile != FactoryDefaults->TlsCertificateFile)
  2631. {
  2632. AddAssemblyProperty(Result, Language, L"TlsClientCertificatePath", SessionData->TlsCertificateFile);
  2633. SessionData->TlsCertificateFile = FactoryDefaults->TlsCertificateFile;
  2634. }
  2635. if (SessionData->Passphrase != FactoryDefaults->Passphrase)
  2636. {
  2637. AddAssemblyProperty(Result, Language, L"PrivateKeyPassphrase", SessionData->Passphrase);
  2638. SessionData->Passphrase = FactoryDefaults->Passphrase;
  2639. }
  2640. if (SessionData->FtpPasvMode != FactoryDefaults->FtpPasvMode)
  2641. {
  2642. AddAssemblyProperty(Result, Language, L"FtpMode", L"FtpMode", (SessionData->FtpPasvMode ? L"Passive" : L"Active"));
  2643. SessionData->FtpPasvMode = FactoryDefaults->FtpPasvMode;
  2644. }
  2645. if (SessionData->Timeout != FactoryDefaults->Timeout)
  2646. {
  2647. AddAssemblyProperty(Result, Language, L"TimeoutInMilliseconds", SessionData->Timeout);
  2648. SessionData->Timeout = FactoryDefaults->Timeout;
  2649. }
  2650. switch (Language)
  2651. {
  2652. case alCSharp:
  2653. Result += L"};\n";
  2654. break;
  2655. case alVBNET:
  2656. // noop
  2657. // Ending With only after AddRawSettings
  2658. break;
  2659. }
  2660. std::unique_ptr<TStrings> RawSettings(SessionData->SaveToOptions(FactoryDefaults.get()));
  2661. if (RawSettings->Count > 0)
  2662. {
  2663. Result += L"\n";
  2664. for (int Index = 0; Index < RawSettings->Count; Index++)
  2665. {
  2666. UnicodeString Name = RawSettings->Names[Index];
  2667. UnicodeString Value = RawSettings->ValueFromIndex[Index];
  2668. UnicodeString SettingsCode;
  2669. switch (Language)
  2670. {
  2671. case alCSharp:
  2672. SettingsCode = L"sessionOptions.AddRawSettings(\"%s\", %s);\n";
  2673. break;
  2674. case alVBNET:
  2675. SettingsCode = L" .AddRawSettings(\"%s\", %s)\n";
  2676. break;
  2677. case alPowerShell:
  2678. SettingsCode = L"$sessionOptions.AddRawSettings(\"%s\", %s)\n";
  2679. break;
  2680. }
  2681. Result += FORMAT(SettingsCode, (Name, AssemblyString(Language, Value)));
  2682. }
  2683. }
  2684. UnicodeString SessionCode;
  2685. switch (Language)
  2686. {
  2687. case alCSharp:
  2688. SessionCode =
  2689. L"\n"
  2690. "using (Session session = new Session())\n"
  2691. "{\n"
  2692. " // %s\n"
  2693. " session.Open(sessionOptions);\n"
  2694. "\n"
  2695. " // %s\n"
  2696. "}\n";
  2697. break;
  2698. case alVBNET:
  2699. SessionCode =
  2700. L"End With\n"
  2701. "\n"
  2702. "Using mySession As Session = New Session\n"
  2703. " ' %s\n"
  2704. " mySession.Open(mySessionOptions)\n"
  2705. "\n"
  2706. " ' %s\n"
  2707. "End Using\n";
  2708. break;
  2709. case alPowerShell:
  2710. SessionCode =
  2711. L"\n"
  2712. "$session = New-Object WinSCP.Session\n"
  2713. "\n"
  2714. "try\n"
  2715. "{\n"
  2716. " # %s\n"
  2717. " $session.Open($sessionOptions)\n"
  2718. "\n"
  2719. " # %s\n"
  2720. "}\n"
  2721. "finally\n"
  2722. "{\n"
  2723. " $session.Dispose()\n"
  2724. "}\n";
  2725. break;
  2726. }
  2727. Result += FORMAT(SessionCode, (LoadStr(CODE_CONNECT), LoadStr(CODE_YOUR_CODE)));
  2728. return Result;
  2729. }
  2730. //---------------------------------------------------------------------
  2731. void __fastcall TSessionData::SetTimeDifference(TDateTime value)
  2732. {
  2733. SET_SESSION_PROPERTY(TimeDifference);
  2734. }
  2735. //---------------------------------------------------------------------
  2736. void __fastcall TSessionData::SetTimeDifferenceAuto(bool value)
  2737. {
  2738. SET_SESSION_PROPERTY(TimeDifferenceAuto);
  2739. }
  2740. //---------------------------------------------------------------------
  2741. void __fastcall TSessionData::SetLocalDirectory(UnicodeString value)
  2742. {
  2743. SET_SESSION_PROPERTY(LocalDirectory);
  2744. }
  2745. //---------------------------------------------------------------------
  2746. void __fastcall TSessionData::SetRemoteDirectory(UnicodeString value)
  2747. {
  2748. SET_SESSION_PROPERTY(RemoteDirectory);
  2749. }
  2750. //---------------------------------------------------------------------
  2751. void __fastcall TSessionData::SetSynchronizeBrowsing(bool value)
  2752. {
  2753. SET_SESSION_PROPERTY(SynchronizeBrowsing);
  2754. }
  2755. //---------------------------------------------------------------------
  2756. void __fastcall TSessionData::SetUpdateDirectories(bool value)
  2757. {
  2758. SET_SESSION_PROPERTY(UpdateDirectories);
  2759. }
  2760. //---------------------------------------------------------------------
  2761. void __fastcall TSessionData::SetCacheDirectories(bool value)
  2762. {
  2763. SET_SESSION_PROPERTY(CacheDirectories);
  2764. }
  2765. //---------------------------------------------------------------------
  2766. void __fastcall TSessionData::SetCacheDirectoryChanges(bool value)
  2767. {
  2768. SET_SESSION_PROPERTY(CacheDirectoryChanges);
  2769. }
  2770. //---------------------------------------------------------------------
  2771. void __fastcall TSessionData::SetPreserveDirectoryChanges(bool value)
  2772. {
  2773. SET_SESSION_PROPERTY(PreserveDirectoryChanges);
  2774. }
  2775. //---------------------------------------------------------------------
  2776. void __fastcall TSessionData::SetResolveSymlinks(bool value)
  2777. {
  2778. SET_SESSION_PROPERTY(ResolveSymlinks);
  2779. }
  2780. //---------------------------------------------------------------------------
  2781. void __fastcall TSessionData::SetDSTMode(TDSTMode value)
  2782. {
  2783. SET_SESSION_PROPERTY(DSTMode);
  2784. }
  2785. //---------------------------------------------------------------------------
  2786. void __fastcall TSessionData::SetDeleteToRecycleBin(bool value)
  2787. {
  2788. SET_SESSION_PROPERTY(DeleteToRecycleBin);
  2789. }
  2790. //---------------------------------------------------------------------------
  2791. void __fastcall TSessionData::SetOverwrittenToRecycleBin(bool value)
  2792. {
  2793. SET_SESSION_PROPERTY(OverwrittenToRecycleBin);
  2794. }
  2795. //---------------------------------------------------------------------------
  2796. void __fastcall TSessionData::SetRecycleBinPath(UnicodeString value)
  2797. {
  2798. SET_SESSION_PROPERTY(RecycleBinPath);
  2799. }
  2800. //---------------------------------------------------------------------------
  2801. void __fastcall TSessionData::SetPostLoginCommands(UnicodeString value)
  2802. {
  2803. SET_SESSION_PROPERTY(PostLoginCommands);
  2804. }
  2805. //---------------------------------------------------------------------
  2806. void __fastcall TSessionData::SetLockInHome(bool value)
  2807. {
  2808. SET_SESSION_PROPERTY(LockInHome);
  2809. }
  2810. //---------------------------------------------------------------------
  2811. void __fastcall TSessionData::SetSpecial(bool value)
  2812. {
  2813. SET_SESSION_PROPERTY(Special);
  2814. }
  2815. //---------------------------------------------------------------------------
  2816. void __fastcall TSessionData::SetScp1Compatibility(bool value)
  2817. {
  2818. SET_SESSION_PROPERTY(Scp1Compatibility);
  2819. }
  2820. //---------------------------------------------------------------------
  2821. void __fastcall TSessionData::SetTcpNoDelay(bool value)
  2822. {
  2823. SET_SESSION_PROPERTY(TcpNoDelay);
  2824. }
  2825. //---------------------------------------------------------------------
  2826. void __fastcall TSessionData::SetSendBuf(int value)
  2827. {
  2828. SET_SESSION_PROPERTY(SendBuf);
  2829. }
  2830. //---------------------------------------------------------------------
  2831. void __fastcall TSessionData::SetSshSimple(bool value)
  2832. {
  2833. SET_SESSION_PROPERTY(SshSimple);
  2834. }
  2835. //---------------------------------------------------------------------
  2836. void __fastcall TSessionData::SetProxyMethod(TProxyMethod value)
  2837. {
  2838. SET_SESSION_PROPERTY(ProxyMethod);
  2839. }
  2840. //---------------------------------------------------------------------
  2841. void __fastcall TSessionData::SetProxyHost(UnicodeString value)
  2842. {
  2843. SET_SESSION_PROPERTY(ProxyHost);
  2844. }
  2845. //---------------------------------------------------------------------
  2846. void __fastcall TSessionData::SetProxyPort(int value)
  2847. {
  2848. SET_SESSION_PROPERTY(ProxyPort);
  2849. }
  2850. //---------------------------------------------------------------------
  2851. void __fastcall TSessionData::SetProxyUsername(UnicodeString value)
  2852. {
  2853. SET_SESSION_PROPERTY(ProxyUsername);
  2854. }
  2855. //---------------------------------------------------------------------
  2856. void __fastcall TSessionData::SetProxyPassword(UnicodeString avalue)
  2857. {
  2858. RawByteString value = EncryptPassword(avalue, ProxyUsername+ProxyHost);
  2859. SET_SESSION_PROPERTY(ProxyPassword);
  2860. }
  2861. //---------------------------------------------------------------------
  2862. UnicodeString __fastcall TSessionData::GetProxyPassword() const
  2863. {
  2864. return DecryptPassword(FProxyPassword, ProxyUsername+ProxyHost);
  2865. }
  2866. //---------------------------------------------------------------------
  2867. void __fastcall TSessionData::SetProxyTelnetCommand(UnicodeString value)
  2868. {
  2869. SET_SESSION_PROPERTY(ProxyTelnetCommand);
  2870. }
  2871. //---------------------------------------------------------------------
  2872. void __fastcall TSessionData::SetProxyLocalCommand(UnicodeString value)
  2873. {
  2874. SET_SESSION_PROPERTY(ProxyLocalCommand);
  2875. }
  2876. //---------------------------------------------------------------------
  2877. void __fastcall TSessionData::SetProxyDNS(TAutoSwitch value)
  2878. {
  2879. SET_SESSION_PROPERTY(ProxyDNS);
  2880. }
  2881. //---------------------------------------------------------------------
  2882. void __fastcall TSessionData::SetProxyLocalhost(bool value)
  2883. {
  2884. SET_SESSION_PROPERTY(ProxyLocalhost);
  2885. }
  2886. //---------------------------------------------------------------------
  2887. void __fastcall TSessionData::SetFtpProxyLogonType(int value)
  2888. {
  2889. SET_SESSION_PROPERTY(FtpProxyLogonType);
  2890. }
  2891. //---------------------------------------------------------------------
  2892. void __fastcall TSessionData::SetBug(TSshBug Bug, TAutoSwitch value)
  2893. {
  2894. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  2895. SET_SESSION_PROPERTY(Bugs[Bug]);
  2896. }
  2897. //---------------------------------------------------------------------
  2898. TAutoSwitch __fastcall TSessionData::GetBug(TSshBug Bug) const
  2899. {
  2900. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FBugs));
  2901. return FBugs[Bug];
  2902. }
  2903. //---------------------------------------------------------------------
  2904. void __fastcall TSessionData::SetCustomParam1(UnicodeString value)
  2905. {
  2906. SET_SESSION_PROPERTY(CustomParam1);
  2907. }
  2908. //---------------------------------------------------------------------
  2909. void __fastcall TSessionData::SetCustomParam2(UnicodeString value)
  2910. {
  2911. SET_SESSION_PROPERTY(CustomParam2);
  2912. }
  2913. //---------------------------------------------------------------------
  2914. void __fastcall TSessionData::SetSFTPDownloadQueue(int value)
  2915. {
  2916. SET_SESSION_PROPERTY(SFTPDownloadQueue);
  2917. }
  2918. //---------------------------------------------------------------------
  2919. void __fastcall TSessionData::SetSFTPUploadQueue(int value)
  2920. {
  2921. SET_SESSION_PROPERTY(SFTPUploadQueue);
  2922. }
  2923. //---------------------------------------------------------------------
  2924. void __fastcall TSessionData::SetSFTPListingQueue(int value)
  2925. {
  2926. SET_SESSION_PROPERTY(SFTPListingQueue);
  2927. }
  2928. //---------------------------------------------------------------------
  2929. void __fastcall TSessionData::SetSFTPMaxVersion(int value)
  2930. {
  2931. SET_SESSION_PROPERTY(SFTPMaxVersion);
  2932. }
  2933. //---------------------------------------------------------------------
  2934. void __fastcall TSessionData::SetSFTPMaxPacketSize(unsigned long value)
  2935. {
  2936. SET_SESSION_PROPERTY(SFTPMaxPacketSize);
  2937. }
  2938. //---------------------------------------------------------------------
  2939. void __fastcall TSessionData::SetSFTPBug(TSftpBug Bug, TAutoSwitch value)
  2940. {
  2941. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  2942. SET_SESSION_PROPERTY(SFTPBugs[Bug]);
  2943. }
  2944. //---------------------------------------------------------------------
  2945. TAutoSwitch __fastcall TSessionData::GetSFTPBug(TSftpBug Bug) const
  2946. {
  2947. DebugAssert(Bug >= 0 && static_cast<unsigned int>(Bug) < LENOF(FSFTPBugs));
  2948. return FSFTPBugs[Bug];
  2949. }
  2950. //---------------------------------------------------------------------
  2951. void __fastcall TSessionData::SetSCPLsFullTime(TAutoSwitch value)
  2952. {
  2953. SET_SESSION_PROPERTY(SCPLsFullTime);
  2954. }
  2955. //---------------------------------------------------------------------------
  2956. void __fastcall TSessionData::SetColor(int value)
  2957. {
  2958. SET_SESSION_PROPERTY(Color);
  2959. }
  2960. //---------------------------------------------------------------------------
  2961. void __fastcall TSessionData::SetTunnel(bool value)
  2962. {
  2963. SET_SESSION_PROPERTY(Tunnel);
  2964. }
  2965. //---------------------------------------------------------------------
  2966. void __fastcall TSessionData::SetTunnelHostName(UnicodeString value)
  2967. {
  2968. if (FTunnelHostName != value)
  2969. {
  2970. // HostName is key for password encryption
  2971. UnicodeString XTunnelPassword = TunnelPassword;
  2972. int P = value.LastDelimiter(L"@");
  2973. if (P > 0)
  2974. {
  2975. TunnelUserName = value.SubString(1, P - 1);
  2976. value = value.SubString(P + 1, value.Length() - P);
  2977. }
  2978. FTunnelHostName = value;
  2979. Modify();
  2980. TunnelPassword = XTunnelPassword;
  2981. Shred(XTunnelPassword);
  2982. }
  2983. }
  2984. //---------------------------------------------------------------------
  2985. void __fastcall TSessionData::SetTunnelPortNumber(int value)
  2986. {
  2987. SET_SESSION_PROPERTY(TunnelPortNumber);
  2988. }
  2989. //---------------------------------------------------------------------
  2990. void __fastcall TSessionData::SetTunnelUserName(UnicodeString value)
  2991. {
  2992. // Avoid password recryption (what may popup master password prompt)
  2993. if (FTunnelUserName != value)
  2994. {
  2995. // TunnelUserName is key for password encryption
  2996. UnicodeString XTunnelPassword = TunnelPassword;
  2997. SET_SESSION_PROPERTY(TunnelUserName);
  2998. TunnelPassword = XTunnelPassword;
  2999. Shred(XTunnelPassword);
  3000. }
  3001. }
  3002. //---------------------------------------------------------------------
  3003. void __fastcall TSessionData::SetTunnelPassword(UnicodeString avalue)
  3004. {
  3005. RawByteString value = EncryptPassword(avalue, TunnelUserName+TunnelHostName);
  3006. SET_SESSION_PROPERTY(TunnelPassword);
  3007. }
  3008. //---------------------------------------------------------------------
  3009. UnicodeString __fastcall TSessionData::GetTunnelPassword() const
  3010. {
  3011. return DecryptPassword(FTunnelPassword, TunnelUserName+TunnelHostName);
  3012. }
  3013. //---------------------------------------------------------------------
  3014. void __fastcall TSessionData::SetTunnelPublicKeyFile(UnicodeString value)
  3015. {
  3016. if (FTunnelPublicKeyFile != value)
  3017. {
  3018. // StripPathQuotes should not be needed as we do not feed quotes anymore
  3019. FTunnelPublicKeyFile = StripPathQuotes(value);
  3020. Modify();
  3021. }
  3022. }
  3023. //---------------------------------------------------------------------
  3024. void __fastcall TSessionData::SetTunnelLocalPortNumber(int value)
  3025. {
  3026. SET_SESSION_PROPERTY(TunnelLocalPortNumber);
  3027. }
  3028. //---------------------------------------------------------------------
  3029. bool __fastcall TSessionData::GetTunnelAutoassignLocalPortNumber()
  3030. {
  3031. return (FTunnelLocalPortNumber <= 0);
  3032. }
  3033. //---------------------------------------------------------------------
  3034. void __fastcall TSessionData::SetTunnelPortFwd(UnicodeString value)
  3035. {
  3036. SET_SESSION_PROPERTY(TunnelPortFwd);
  3037. }
  3038. //---------------------------------------------------------------------
  3039. void __fastcall TSessionData::SetTunnelHostKey(UnicodeString value)
  3040. {
  3041. SET_SESSION_PROPERTY(TunnelHostKey);
  3042. }
  3043. //---------------------------------------------------------------------
  3044. void __fastcall TSessionData::SetFtpPasvMode(bool value)
  3045. {
  3046. SET_SESSION_PROPERTY(FtpPasvMode);
  3047. }
  3048. //---------------------------------------------------------------------
  3049. void __fastcall TSessionData::SetFtpForcePasvIp(TAutoSwitch value)
  3050. {
  3051. SET_SESSION_PROPERTY(FtpForcePasvIp);
  3052. }
  3053. //---------------------------------------------------------------------
  3054. void __fastcall TSessionData::SetFtpUseMlsd(TAutoSwitch value)
  3055. {
  3056. SET_SESSION_PROPERTY(FtpUseMlsd);
  3057. }
  3058. //---------------------------------------------------------------------
  3059. void __fastcall TSessionData::SetFtpAccount(UnicodeString value)
  3060. {
  3061. SET_SESSION_PROPERTY(FtpAccount);
  3062. }
  3063. //---------------------------------------------------------------------
  3064. void __fastcall TSessionData::SetFtpPingInterval(int value)
  3065. {
  3066. SET_SESSION_PROPERTY(FtpPingInterval);
  3067. }
  3068. //---------------------------------------------------------------------------
  3069. TDateTime __fastcall TSessionData::GetFtpPingIntervalDT()
  3070. {
  3071. return SecToDateTime(FtpPingInterval);
  3072. }
  3073. //---------------------------------------------------------------------------
  3074. void __fastcall TSessionData::SetFtpPingType(TPingType value)
  3075. {
  3076. SET_SESSION_PROPERTY(FtpPingType);
  3077. }
  3078. //---------------------------------------------------------------------------
  3079. void __fastcall TSessionData::SetFtpTransferActiveImmediately(TAutoSwitch value)
  3080. {
  3081. SET_SESSION_PROPERTY(FtpTransferActiveImmediately);
  3082. }
  3083. //---------------------------------------------------------------------------
  3084. void __fastcall TSessionData::SetFtps(TFtps value)
  3085. {
  3086. SET_SESSION_PROPERTY(Ftps);
  3087. }
  3088. //---------------------------------------------------------------------------
  3089. void __fastcall TSessionData::SetMinTlsVersion(TTlsVersion value)
  3090. {
  3091. SET_SESSION_PROPERTY(MinTlsVersion);
  3092. }
  3093. //---------------------------------------------------------------------------
  3094. void __fastcall TSessionData::SetMaxTlsVersion(TTlsVersion value)
  3095. {
  3096. SET_SESSION_PROPERTY(MaxTlsVersion);
  3097. }
  3098. //---------------------------------------------------------------------
  3099. void __fastcall TSessionData::SetFtpListAll(TAutoSwitch value)
  3100. {
  3101. SET_SESSION_PROPERTY(FtpListAll);
  3102. }
  3103. //---------------------------------------------------------------------
  3104. void __fastcall TSessionData::SetFtpHost(TAutoSwitch value)
  3105. {
  3106. SET_SESSION_PROPERTY(FtpHost);
  3107. }
  3108. //---------------------------------------------------------------------
  3109. void __fastcall TSessionData::SetSslSessionReuse(bool value)
  3110. {
  3111. SET_SESSION_PROPERTY(SslSessionReuse);
  3112. }
  3113. //---------------------------------------------------------------------
  3114. void __fastcall TSessionData::SetTlsCertificateFile(UnicodeString value)
  3115. {
  3116. SET_SESSION_PROPERTY(TlsCertificateFile);
  3117. }
  3118. //---------------------------------------------------------------------
  3119. void __fastcall TSessionData::SetNotUtf(TAutoSwitch value)
  3120. {
  3121. SET_SESSION_PROPERTY(NotUtf);
  3122. }
  3123. //---------------------------------------------------------------------
  3124. void __fastcall TSessionData::SetIsWorkspace(bool value)
  3125. {
  3126. SET_SESSION_PROPERTY(IsWorkspace);
  3127. }
  3128. //---------------------------------------------------------------------
  3129. void __fastcall TSessionData::SetLink(UnicodeString value)
  3130. {
  3131. SET_SESSION_PROPERTY(Link);
  3132. }
  3133. //---------------------------------------------------------------------
  3134. void __fastcall TSessionData::SetHostKey(UnicodeString value)
  3135. {
  3136. SET_SESSION_PROPERTY(HostKey);
  3137. }
  3138. //---------------------------------------------------------------------
  3139. void __fastcall TSessionData::SetNote(UnicodeString value)
  3140. {
  3141. SET_SESSION_PROPERTY(Note);
  3142. }
  3143. //---------------------------------------------------------------------
  3144. UnicodeString __fastcall TSessionData::GetInfoTip()
  3145. {
  3146. if (UsesSsh)
  3147. {
  3148. return FMTLOAD(SESSION_INFO_TIP2,
  3149. (HostName, UserName,
  3150. (PublicKeyFile.IsEmpty() ? LoadStr(NO_STR) : LoadStr(YES_STR)),
  3151. FSProtocolStr));
  3152. }
  3153. else
  3154. {
  3155. return FMTLOAD(SESSION_INFO_TIP_NO_SSH,
  3156. (HostName, UserName, FSProtocolStr));
  3157. }
  3158. }
  3159. //---------------------------------------------------------------------
  3160. UnicodeString __fastcall TSessionData::ExtractLocalName(const UnicodeString & Name)
  3161. {
  3162. UnicodeString Result = Name;
  3163. int P = Result.LastDelimiter(L"/");
  3164. if (P > 0)
  3165. {
  3166. Result.Delete(1, P);
  3167. }
  3168. return Result;
  3169. }
  3170. //---------------------------------------------------------------------
  3171. UnicodeString __fastcall TSessionData::GetLocalName()
  3172. {
  3173. UnicodeString Result;
  3174. if (HasSessionName())
  3175. {
  3176. Result = ExtractLocalName(Name);
  3177. }
  3178. else
  3179. {
  3180. Result = DefaultSessionName;
  3181. }
  3182. return Result;
  3183. }
  3184. //---------------------------------------------------------------------
  3185. UnicodeString __fastcall TSessionData::ExtractFolderName(const UnicodeString & Name)
  3186. {
  3187. UnicodeString Result;
  3188. int P = Name.LastDelimiter(L"/");
  3189. if (P > 0)
  3190. {
  3191. Result = Name.SubString(1, P - 1);
  3192. }
  3193. return Result;
  3194. }
  3195. //---------------------------------------------------------------------
  3196. UnicodeString __fastcall TSessionData::GetFolderName()
  3197. {
  3198. UnicodeString Result;
  3199. if (HasSessionName() || IsWorkspace)
  3200. {
  3201. Result = ExtractFolderName(Name);
  3202. }
  3203. return Result;
  3204. }
  3205. //---------------------------------------------------------------------
  3206. UnicodeString __fastcall TSessionData::ComposePath(
  3207. const UnicodeString & Path, const UnicodeString & Name)
  3208. {
  3209. return UnixIncludeTrailingBackslash(Path) + Name;
  3210. }
  3211. //=== TStoredSessionList ----------------------------------------------
  3212. __fastcall TStoredSessionList::TStoredSessionList(bool aReadOnly):
  3213. TNamedObjectList(), FReadOnly(aReadOnly)
  3214. {
  3215. DebugAssert(Configuration);
  3216. FDefaultSettings = new TSessionData(DefaultName);
  3217. }
  3218. //---------------------------------------------------------------------
  3219. __fastcall TStoredSessionList::~TStoredSessionList()
  3220. {
  3221. DebugAssert(Configuration);
  3222. delete FDefaultSettings;
  3223. }
  3224. //---------------------------------------------------------------------
  3225. void __fastcall TStoredSessionList::Load(THierarchicalStorage * Storage,
  3226. bool AsModified, bool UseDefaults)
  3227. {
  3228. TStringList *SubKeys = new TStringList();
  3229. TList * Loaded = new TList;
  3230. try
  3231. {
  3232. DebugAssert(AutoSort);
  3233. AutoSort = false;
  3234. bool WasEmpty = (Count == 0);
  3235. Storage->GetSubKeyNames(SubKeys);
  3236. for (int Index = 0; Index < SubKeys->Count; Index++)
  3237. {
  3238. UnicodeString SessionName = SubKeys->Strings[Index];
  3239. bool ValidName = true;
  3240. try
  3241. {
  3242. TSessionData::ValidatePath(SessionName);
  3243. }
  3244. catch(...)
  3245. {
  3246. ValidName = false;
  3247. }
  3248. if (ValidName)
  3249. {
  3250. TSessionData * SessionData;
  3251. if (SessionName == FDefaultSettings->Name)
  3252. {
  3253. SessionData = FDefaultSettings;
  3254. }
  3255. else
  3256. {
  3257. // if the list was empty before loading, do not waste time trying to
  3258. // find existing sites to overwrite (we rely on underlying storage
  3259. // to secure uniqueness of the key names)
  3260. if (WasEmpty)
  3261. {
  3262. SessionData = NULL;
  3263. }
  3264. else
  3265. {
  3266. SessionData = (TSessionData*)FindByName(SessionName);
  3267. }
  3268. }
  3269. if ((SessionData != FDefaultSettings) || !UseDefaults)
  3270. {
  3271. if (SessionData == NULL)
  3272. {
  3273. SessionData = new TSessionData(L"");
  3274. if (UseDefaults)
  3275. {
  3276. SessionData->CopyData(DefaultSettings);
  3277. }
  3278. SessionData->Name = SessionName;
  3279. Add(SessionData);
  3280. }
  3281. Loaded->Add(SessionData);
  3282. SessionData->Load(Storage);
  3283. if (AsModified)
  3284. {
  3285. SessionData->Modified = true;
  3286. }
  3287. }
  3288. }
  3289. }
  3290. if (!AsModified)
  3291. {
  3292. for (int Index = 0; Index < TObjectList::Count; Index++)
  3293. {
  3294. if (Loaded->IndexOf(Items[Index]) < 0)
  3295. {
  3296. Delete(Index);
  3297. Index--;
  3298. }
  3299. }
  3300. }
  3301. }
  3302. __finally
  3303. {
  3304. AutoSort = true;
  3305. AlphaSort();
  3306. delete SubKeys;
  3307. delete Loaded;
  3308. }
  3309. }
  3310. //---------------------------------------------------------------------
  3311. void __fastcall TStoredSessionList::Load()
  3312. {
  3313. bool SessionList = true;
  3314. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  3315. try
  3316. {
  3317. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, False))
  3318. Load(Storage);
  3319. }
  3320. __finally
  3321. {
  3322. delete Storage;
  3323. }
  3324. }
  3325. //---------------------------------------------------------------------
  3326. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  3327. TSessionData * Data, bool All, bool RecryptPasswordOnly,
  3328. TSessionData * FactoryDefaults)
  3329. {
  3330. if (All || Data->Modified)
  3331. {
  3332. if (RecryptPasswordOnly)
  3333. {
  3334. Data->SaveRecryptedPasswords(Storage);
  3335. }
  3336. else
  3337. {
  3338. Data->Save(Storage, false, FactoryDefaults);
  3339. }
  3340. }
  3341. }
  3342. //---------------------------------------------------------------------
  3343. void __fastcall TStoredSessionList::DoSave(THierarchicalStorage * Storage,
  3344. bool All, bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  3345. {
  3346. TSessionData * FactoryDefaults = new TSessionData(L"");
  3347. try
  3348. {
  3349. DoSave(Storage, FDefaultSettings, All, RecryptPasswordOnly, FactoryDefaults);
  3350. for (int Index = 0; Index < CountIncludingHidden; Index++)
  3351. {
  3352. TSessionData * SessionData = (TSessionData *)Items[Index];
  3353. try
  3354. {
  3355. DoSave(Storage, SessionData, All, RecryptPasswordOnly, FactoryDefaults);
  3356. }
  3357. catch (Exception & E)
  3358. {
  3359. UnicodeString Message;
  3360. if (RecryptPasswordOnly && DebugAlwaysTrue(RecryptPasswordErrors != NULL) &&
  3361. ExceptionMessage(&E, Message))
  3362. {
  3363. RecryptPasswordErrors->Add(FORMAT("%s: %s", (SessionData->SessionName, Message)));
  3364. }
  3365. else
  3366. {
  3367. throw;
  3368. }
  3369. }
  3370. }
  3371. }
  3372. __finally
  3373. {
  3374. delete FactoryDefaults;
  3375. }
  3376. }
  3377. //---------------------------------------------------------------------
  3378. void __fastcall TStoredSessionList::Save(THierarchicalStorage * Storage, bool All)
  3379. {
  3380. DoSave(Storage, All, false, NULL);
  3381. }
  3382. //---------------------------------------------------------------------
  3383. void __fastcall TStoredSessionList::DoSave(bool All, bool Explicit,
  3384. bool RecryptPasswordOnly, TStrings * RecryptPasswordErrors)
  3385. {
  3386. bool SessionList = true;
  3387. THierarchicalStorage * Storage = Configuration->CreateScpStorage(SessionList);
  3388. try
  3389. {
  3390. Storage->AccessMode = smReadWrite;
  3391. Storage->Explicit = Explicit;
  3392. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  3393. {
  3394. DoSave(Storage, All, RecryptPasswordOnly, RecryptPasswordErrors);
  3395. }
  3396. }
  3397. __finally
  3398. {
  3399. delete Storage;
  3400. }
  3401. Saved();
  3402. }
  3403. //---------------------------------------------------------------------
  3404. void __fastcall TStoredSessionList::Save(bool All, bool Explicit)
  3405. {
  3406. DoSave(All, Explicit, false, NULL);
  3407. }
  3408. //---------------------------------------------------------------------
  3409. void __fastcall TStoredSessionList::RecryptPasswords(TStrings * RecryptPasswordErrors)
  3410. {
  3411. DoSave(true, true, true, RecryptPasswordErrors);
  3412. }
  3413. //---------------------------------------------------------------------
  3414. void __fastcall TStoredSessionList::Saved()
  3415. {
  3416. FDefaultSettings->Modified = false;
  3417. for (int Index = 0; Index < CountIncludingHidden; Index++)
  3418. {
  3419. ((TSessionData *)Items[Index])->Modified = false;
  3420. }
  3421. }
  3422. //---------------------------------------------------------------------
  3423. void __fastcall TStoredSessionList::ImportLevelFromFilezilla(_di_IXMLNode Node, const UnicodeString & Path)
  3424. {
  3425. for (int Index = 0; Index < Node->ChildNodes->Count; Index++)
  3426. {
  3427. _di_IXMLNode ChildNode = Node->ChildNodes->Get(Index);
  3428. if (ChildNode->NodeName == L"Server")
  3429. {
  3430. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  3431. SessionData->CopyData(DefaultSettings);
  3432. SessionData->ImportFromFilezilla(ChildNode, Path);
  3433. Add(SessionData.release());
  3434. }
  3435. else if (ChildNode->NodeName == L"Folder")
  3436. {
  3437. UnicodeString Name;
  3438. for (int Index = 0; Index < ChildNode->ChildNodes->Count; Index++)
  3439. {
  3440. _di_IXMLNode PossibleTextMode = ChildNode->ChildNodes->Get(Index);
  3441. if (PossibleTextMode->NodeType == ntText)
  3442. {
  3443. UnicodeString NodeValue = PossibleTextMode->NodeValue;
  3444. AddToList(Name, NodeValue.Trim(), L" ");
  3445. }
  3446. }
  3447. Name = TSessionData::MakeValidName(Name).Trim();
  3448. ImportLevelFromFilezilla(ChildNode, TSessionData::ComposePath(Path, Name));
  3449. }
  3450. }
  3451. }
  3452. //---------------------------------------------------------------------
  3453. void __fastcall TStoredSessionList::ImportFromFilezilla(const UnicodeString FileName)
  3454. {
  3455. const _di_IXMLDocument Document = interface_cast<Xmlintf::IXMLDocument>(new TXMLDocument(NULL));
  3456. Document->LoadFromFile(FileName);
  3457. _di_IXMLNode FileZilla3Node = Document->ChildNodes->FindNode(L"FileZilla3");
  3458. if (FileZilla3Node != NULL)
  3459. {
  3460. _di_IXMLNode ServersNode = FileZilla3Node->ChildNodes->FindNode(L"Servers");
  3461. if (ServersNode != NULL)
  3462. {
  3463. ImportLevelFromFilezilla(ServersNode, L"");
  3464. }
  3465. }
  3466. }
  3467. //---------------------------------------------------------------------
  3468. void __fastcall TStoredSessionList::Export(const UnicodeString FileName)
  3469. {
  3470. THierarchicalStorage * Storage = new TIniFileStorage(FileName);
  3471. try
  3472. {
  3473. Storage->AccessMode = smReadWrite;
  3474. if (Storage->OpenSubKey(Configuration->StoredSessionsSubKey, true))
  3475. {
  3476. Save(Storage, true);
  3477. }
  3478. }
  3479. __finally
  3480. {
  3481. delete Storage;
  3482. }
  3483. }
  3484. //---------------------------------------------------------------------
  3485. void __fastcall TStoredSessionList::SelectAll(bool Select)
  3486. {
  3487. for (int Index = 0; Index < Count; Index++)
  3488. Sessions[Index]->Selected = Select;
  3489. }
  3490. //---------------------------------------------------------------------
  3491. void __fastcall TStoredSessionList::Import(TStoredSessionList * From,
  3492. bool OnlySelected, TList * Imported)
  3493. {
  3494. for (int Index = 0; Index < From->Count; Index++)
  3495. {
  3496. if (!OnlySelected || From->Sessions[Index]->Selected)
  3497. {
  3498. TSessionData *Session = new TSessionData(L"");
  3499. Session->Assign(From->Sessions[Index]);
  3500. Session->Modified = true;
  3501. Session->MakeUniqueIn(this);
  3502. Add(Session);
  3503. if (Imported != NULL)
  3504. {
  3505. Imported->Add(Session);
  3506. }
  3507. }
  3508. }
  3509. // only modified, explicit
  3510. Save(false, true);
  3511. }
  3512. //---------------------------------------------------------------------
  3513. void __fastcall TStoredSessionList::SelectSessionsToImport
  3514. (TStoredSessionList * Dest, bool SSHOnly)
  3515. {
  3516. for (int Index = 0; Index < Count; Index++)
  3517. {
  3518. Sessions[Index]->Selected =
  3519. (!SSHOnly || (Sessions[Index]->GetNormalizedPuttyProtocol() == PuttySshProtocol)) &&
  3520. !Dest->FindByName(Sessions[Index]->Name);
  3521. }
  3522. }
  3523. //---------------------------------------------------------------------
  3524. void __fastcall TStoredSessionList::Cleanup()
  3525. {
  3526. try
  3527. {
  3528. if (Configuration->Storage == stRegistry) Clear();
  3529. TRegistryStorage * Storage = new TRegistryStorage(Configuration->RegistryStorageKey);
  3530. try
  3531. {
  3532. Storage->AccessMode = smReadWrite;
  3533. if (Storage->OpenRootKey(False))
  3534. Storage->RecursiveDeleteSubKey(Configuration->StoredSessionsSubKey);
  3535. }
  3536. __finally
  3537. {
  3538. delete Storage;
  3539. }
  3540. }
  3541. catch (Exception &E)
  3542. {
  3543. throw ExtException(&E, LoadStr(CLEANUP_SESSIONS_ERROR));
  3544. }
  3545. }
  3546. //---------------------------------------------------------------------------
  3547. void __fastcall TStoredSessionList::UpdateStaticUsage()
  3548. {
  3549. int SCP = 0;
  3550. int SFTP = 0;
  3551. int FTP = 0;
  3552. int FTPS = 0;
  3553. int WebDAV = 0;
  3554. int WebDAVS = 0;
  3555. int Password = 0;
  3556. int Advanced = 0;
  3557. int Color = 0;
  3558. int Note = 0;
  3559. int Tunnel = 0;
  3560. bool Folders = false;
  3561. bool Workspaces = false;
  3562. std::unique_ptr<TSessionData> FactoryDefaults(new TSessionData(L""));
  3563. std::unique_ptr<TStringList> DifferentAdvancedProperties(CreateSortedStringList());
  3564. for (int Index = 0; Index < Count; Index++)
  3565. {
  3566. TSessionData * Data = Sessions[Index];
  3567. if (Data->IsWorkspace)
  3568. {
  3569. Workspaces = true;
  3570. }
  3571. else
  3572. {
  3573. switch (Data->FSProtocol)
  3574. {
  3575. case fsSCPonly:
  3576. SCP++;
  3577. break;
  3578. case fsSFTP:
  3579. case fsSFTPonly:
  3580. SFTP++;
  3581. break;
  3582. case fsFTP:
  3583. if (Data->Ftps == ftpsNone)
  3584. {
  3585. FTP++;
  3586. }
  3587. else
  3588. {
  3589. FTPS++;
  3590. }
  3591. break;
  3592. case fsWebDAV:
  3593. if (Data->Ftps == ftpsNone)
  3594. {
  3595. WebDAV++;
  3596. }
  3597. else
  3598. {
  3599. WebDAVS++;
  3600. }
  3601. break;
  3602. }
  3603. if (Data->HasAnySessionPassword())
  3604. {
  3605. Password++;
  3606. }
  3607. if (Data->Color != 0)
  3608. {
  3609. Color++;
  3610. }
  3611. if (!Data->Note.IsEmpty())
  3612. {
  3613. Note++;
  3614. }
  3615. // this effectively does not take passwords (proxy + tunnel) into account,
  3616. // when master password is set, as master password handler in not set up yet
  3617. if (!Data->IsSame(FactoryDefaults.get(), true, DifferentAdvancedProperties.get()))
  3618. {
  3619. Advanced++;
  3620. }
  3621. if (Data->Tunnel)
  3622. {
  3623. Tunnel++;
  3624. }
  3625. if (!Data->FolderName.IsEmpty())
  3626. {
  3627. Folders = true;
  3628. }
  3629. }
  3630. }
  3631. Configuration->Usage->Set(L"StoredSessionsCountSCP", SCP);
  3632. Configuration->Usage->Set(L"StoredSessionsCountSFTP", SFTP);
  3633. Configuration->Usage->Set(L"StoredSessionsCountFTP", FTP);
  3634. Configuration->Usage->Set(L"StoredSessionsCountFTPS", FTPS);
  3635. Configuration->Usage->Set(L"StoredSessionsCountWebDAV", WebDAV);
  3636. Configuration->Usage->Set(L"StoredSessionsCountWebDAVS", WebDAVS);
  3637. Configuration->Usage->Set(L"StoredSessionsCountPassword", Password);
  3638. Configuration->Usage->Set(L"StoredSessionsCountColor", Color);
  3639. Configuration->Usage->Set(L"StoredSessionsCountNote", Note);
  3640. Configuration->Usage->Set(L"StoredSessionsCountAdvanced", Advanced);
  3641. DifferentAdvancedProperties->Delimiter = L',';
  3642. Configuration->Usage->Set(L"StoredSessionsAdvancedSettings", DifferentAdvancedProperties->DelimitedText);
  3643. Configuration->Usage->Set(L"StoredSessionsCountTunnel", Tunnel);
  3644. // actually default might be true, see below for when the default is actually used
  3645. bool CustomDefaultStoredSession = false;
  3646. try
  3647. {
  3648. // this can throw, when the default session settings have password set
  3649. // (and no other basic property, like hostname/username),
  3650. // and master password is enabled as we are called before master password
  3651. // handler is set
  3652. CustomDefaultStoredSession = !FDefaultSettings->IsSame(FactoryDefaults.get(), false);
  3653. }
  3654. catch (...)
  3655. {
  3656. }
  3657. Configuration->Usage->Set(L"UsingDefaultStoredSession", CustomDefaultStoredSession);
  3658. Configuration->Usage->Set(L"UsingStoredSessionsFolders", Folders);
  3659. Configuration->Usage->Set(L"UsingWorkspaces", Workspaces);
  3660. }
  3661. //---------------------------------------------------------------------------
  3662. TSessionData * __fastcall TStoredSessionList::FindSame(TSessionData * Data)
  3663. {
  3664. TSessionData * Result;
  3665. if (Data->Hidden || Data->Name.IsEmpty() || Data->IsWorkspace)
  3666. {
  3667. Result = NULL;
  3668. }
  3669. else
  3670. {
  3671. Result = dynamic_cast<TSessionData *>(FindByName(Data->Name));
  3672. }
  3673. return Result;
  3674. }
  3675. //---------------------------------------------------------------------------
  3676. int __fastcall TStoredSessionList::IndexOf(TSessionData * Data)
  3677. {
  3678. for (int Index = 0; Index < Count; Index++)
  3679. if (Data == Sessions[Index]) return Index;
  3680. return -1;
  3681. }
  3682. //---------------------------------------------------------------------------
  3683. TSessionData * __fastcall TStoredSessionList::NewSession(
  3684. UnicodeString SessionName, TSessionData * Session)
  3685. {
  3686. TSessionData * DuplicateSession = (TSessionData*)FindByName(SessionName);
  3687. if (!DuplicateSession)
  3688. {
  3689. DuplicateSession = new TSessionData(L"");
  3690. DuplicateSession->Assign(Session);
  3691. DuplicateSession->Name = SessionName;
  3692. // make sure, that new stored session is saved to registry
  3693. DuplicateSession->Modified = true;
  3694. Add(DuplicateSession);
  3695. }
  3696. else
  3697. {
  3698. DuplicateSession->Assign(Session);
  3699. DuplicateSession->Name = SessionName;
  3700. DuplicateSession->Modified = true;
  3701. }
  3702. // list was saved here before to default storage, but it would not allow
  3703. // to work with special lists (export/import) not using default storage
  3704. return DuplicateSession;
  3705. }
  3706. //---------------------------------------------------------------------------
  3707. void __fastcall TStoredSessionList::SetDefaultSettings(TSessionData * value)
  3708. {
  3709. DebugAssert(FDefaultSettings);
  3710. if (FDefaultSettings != value)
  3711. {
  3712. FDefaultSettings->Assign(value);
  3713. // make sure default settings are saved
  3714. FDefaultSettings->Modified = true;
  3715. FDefaultSettings->Name = DefaultName;
  3716. if (!FReadOnly)
  3717. {
  3718. // only modified, explicit
  3719. Save(false, true);
  3720. }
  3721. }
  3722. }
  3723. //---------------------------------------------------------------------------
  3724. void __fastcall TStoredSessionList::ImportHostKeys(const UnicodeString TargetKey,
  3725. const UnicodeString SourceKey, TStoredSessionList * Sessions,
  3726. bool OnlySelected)
  3727. {
  3728. TRegistryStorage * SourceStorage = NULL;
  3729. TRegistryStorage * TargetStorage = NULL;
  3730. TStringList * KeyList = NULL;
  3731. try
  3732. {
  3733. SourceStorage = new TRegistryStorage(SourceKey);
  3734. TargetStorage = new TRegistryStorage(TargetKey);
  3735. TargetStorage->AccessMode = smReadWrite;
  3736. KeyList = new TStringList();
  3737. if (SourceStorage->OpenRootKey(false) &&
  3738. TargetStorage->OpenRootKey(true))
  3739. {
  3740. SourceStorage->GetValueNames(KeyList);
  3741. TSessionData * Session;
  3742. UnicodeString HostKeyName;
  3743. DebugAssert(Sessions != NULL);
  3744. for (int Index = 0; Index < Sessions->Count; Index++)
  3745. {
  3746. Session = Sessions->Sessions[Index];
  3747. if (!OnlySelected || Session->Selected)
  3748. {
  3749. HostKeyName = PuttyMungeStr(FORMAT(L"@%d:%s", (Session->PortNumber, Session->HostNameExpanded)));
  3750. UnicodeString KeyName;
  3751. for (int KeyIndex = 0; KeyIndex < KeyList->Count; KeyIndex++)
  3752. {
  3753. KeyName = KeyList->Strings[KeyIndex];
  3754. int P = KeyName.Pos(HostKeyName);
  3755. if ((P > 0) && (P == KeyName.Length() - HostKeyName.Length() + 1))
  3756. {
  3757. TargetStorage->WriteStringRaw(KeyName,
  3758. SourceStorage->ReadStringRaw(KeyName, L""));
  3759. }
  3760. }
  3761. }
  3762. }
  3763. }
  3764. }
  3765. __finally
  3766. {
  3767. delete SourceStorage;
  3768. delete TargetStorage;
  3769. delete KeyList;
  3770. }
  3771. }
  3772. //---------------------------------------------------------------------------
  3773. bool __fastcall TStoredSessionList::IsFolderOrWorkspace(
  3774. const UnicodeString & Name, bool Workspace)
  3775. {
  3776. bool Result = false;
  3777. TSessionData * FirstData = NULL;
  3778. if (!Name.IsEmpty())
  3779. {
  3780. for (int Index = 0; !Result && (Index < Count); Index++)
  3781. {
  3782. Result = Sessions[Index]->IsInFolderOrWorkspace(Name);
  3783. if (Result)
  3784. {
  3785. FirstData = Sessions[Index];
  3786. }
  3787. }
  3788. }
  3789. return
  3790. Result &&
  3791. DebugAlwaysTrue(FirstData != NULL) &&
  3792. (FirstData->IsWorkspace == Workspace);
  3793. }
  3794. //---------------------------------------------------------------------------
  3795. bool __fastcall TStoredSessionList::IsFolder(const UnicodeString & Name)
  3796. {
  3797. return IsFolderOrWorkspace(Name, false);
  3798. }
  3799. //---------------------------------------------------------------------------
  3800. bool __fastcall TStoredSessionList::IsWorkspace(const UnicodeString & Name)
  3801. {
  3802. return IsFolderOrWorkspace(Name, true);
  3803. }
  3804. //---------------------------------------------------------------------------
  3805. TSessionData * __fastcall TStoredSessionList::CheckIsInFolderOrWorkspaceAndResolve(
  3806. TSessionData * Data, const UnicodeString & Name)
  3807. {
  3808. if (Data->IsInFolderOrWorkspace(Name))
  3809. {
  3810. Data = ResolveWorkspaceData(Data);
  3811. if ((Data != NULL) && Data->CanLogin &&
  3812. DebugAlwaysTrue(Data->Link.IsEmpty()))
  3813. {
  3814. return Data;
  3815. }
  3816. }
  3817. return NULL;
  3818. }
  3819. //---------------------------------------------------------------------------
  3820. void __fastcall TStoredSessionList::GetFolderOrWorkspace(const UnicodeString & Name, TList * List)
  3821. {
  3822. for (int Index = 0; (Index < Count); Index++)
  3823. {
  3824. TSessionData * RawData = Sessions[Index];
  3825. TSessionData * Data =
  3826. CheckIsInFolderOrWorkspaceAndResolve(RawData, Name);
  3827. if (Data != NULL)
  3828. {
  3829. TSessionData * Data2 = new TSessionData(L"");
  3830. Data2->Assign(Data);
  3831. if (!RawData->Link.IsEmpty() && (DebugAlwaysTrue(Data != RawData)) &&
  3832. // BACKWARD COMPATIBILITY
  3833. // When loading pre-5.6.4 workspace, that does not have state saved,
  3834. // do not overwrite the site "state" defaults
  3835. // with (empty) workspace state
  3836. RawData->HasStateData())
  3837. {
  3838. Data2->CopyStateData(RawData);
  3839. }
  3840. List->Add(Data2);
  3841. }
  3842. }
  3843. }
  3844. //---------------------------------------------------------------------------
  3845. TStrings * __fastcall TStoredSessionList::GetFolderOrWorkspaceList(
  3846. const UnicodeString & Name)
  3847. {
  3848. std::unique_ptr<TStringList> Result(new TStringList());
  3849. for (int Index = 0; (Index < Count); Index++)
  3850. {
  3851. TSessionData * Data =
  3852. CheckIsInFolderOrWorkspaceAndResolve(Sessions[Index], Name);
  3853. if (Data != NULL)
  3854. {
  3855. Result->Add(Data->SessionName);
  3856. }
  3857. }
  3858. return Result.release();
  3859. }
  3860. //---------------------------------------------------------------------------
  3861. TStrings * __fastcall TStoredSessionList::GetWorkspaces()
  3862. {
  3863. std::unique_ptr<TStringList> Result(CreateSortedStringList());
  3864. for (int Index = 0; (Index < Count); Index++)
  3865. {
  3866. TSessionData * Data = Sessions[Index];
  3867. if (Data->IsWorkspace)
  3868. {
  3869. Result->Add(Data->FolderName);
  3870. }
  3871. }
  3872. return Result.release();
  3873. }
  3874. //---------------------------------------------------------------------------
  3875. void __fastcall TStoredSessionList::NewWorkspace(
  3876. UnicodeString Name, TList * DataList)
  3877. {
  3878. for (int Index = 0; (Index < Count); Index++)
  3879. {
  3880. TSessionData * Data = Sessions[Index];
  3881. if (Data->IsInFolderOrWorkspace(Name))
  3882. {
  3883. Data->Remove();
  3884. Remove(Data);
  3885. Index--;
  3886. }
  3887. }
  3888. for (int Index = 0; (Index < DataList->Count); Index++)
  3889. {
  3890. TSessionData * Data = static_cast<TSessionData *>(DataList->Items[Index]);
  3891. TSessionData * Data2 = new TSessionData(L"");
  3892. Data2->Assign(Data);
  3893. Data2->Name = TSessionData::ComposePath(Name, Data->Name);
  3894. // make sure, that new stored session is saved to registry
  3895. Data2->Modified = true;
  3896. Add(Data2);
  3897. }
  3898. }
  3899. //---------------------------------------------------------------------------
  3900. bool __fastcall TStoredSessionList::HasAnyWorkspace()
  3901. {
  3902. bool Result = false;
  3903. for (int Index = 0; !Result && (Index < Count); Index++)
  3904. {
  3905. TSessionData * Data = Sessions[Index];
  3906. Result = Data->IsWorkspace;
  3907. }
  3908. return Result;
  3909. }
  3910. //---------------------------------------------------------------------------
  3911. TSessionData * __fastcall TStoredSessionList::ParseUrl(UnicodeString Url,
  3912. TOptions * Options, bool & DefaultsOnly, UnicodeString * FileName,
  3913. bool * AProtocolDefined, UnicodeString * MaskedUrl)
  3914. {
  3915. TSessionData * Data = new TSessionData(L"");
  3916. try
  3917. {
  3918. Data->ParseUrl(Url, Options, this, DefaultsOnly, FileName, AProtocolDefined, MaskedUrl);
  3919. }
  3920. catch(...)
  3921. {
  3922. delete Data;
  3923. throw;
  3924. }
  3925. return Data;
  3926. }
  3927. //---------------------------------------------------------------------
  3928. bool __fastcall TStoredSessionList::IsUrl(UnicodeString Url)
  3929. {
  3930. bool DefaultsOnly;
  3931. bool ProtocolDefined = false;
  3932. std::unique_ptr<TSessionData> ParsedData(ParseUrl(Url, NULL, DefaultsOnly, NULL, &ProtocolDefined));
  3933. bool Result = ProtocolDefined;
  3934. return Result;
  3935. }
  3936. //---------------------------------------------------------------------
  3937. TSessionData * __fastcall TStoredSessionList::ResolveWorkspaceData(TSessionData * Data)
  3938. {
  3939. if (!Data->Link.IsEmpty())
  3940. {
  3941. Data = dynamic_cast<TSessionData *>(FindByName(Data->Link));
  3942. if (Data != NULL)
  3943. {
  3944. Data = ResolveWorkspaceData(Data);
  3945. }
  3946. }
  3947. return Data;
  3948. }
  3949. //---------------------------------------------------------------------
  3950. TSessionData * __fastcall TStoredSessionList::SaveWorkspaceData(TSessionData * Data)
  3951. {
  3952. std::unique_ptr<TSessionData> Result(new TSessionData(L""));
  3953. TSessionData * SameData = StoredSessions->FindSame(Data);
  3954. if (SameData != NULL)
  3955. {
  3956. Result->CopyStateData(Data);
  3957. Result->Link = Data->Name;
  3958. }
  3959. else
  3960. {
  3961. Result->Assign(Data);
  3962. }
  3963. Result->IsWorkspace = true;
  3964. return Result.release();
  3965. }
  3966. //---------------------------------------------------------------------
  3967. bool __fastcall TStoredSessionList::CanLogin(TSessionData * Data)
  3968. {
  3969. Data = ResolveWorkspaceData(Data);
  3970. return (Data != NULL) && Data->CanLogin;
  3971. }
  3972. //---------------------------------------------------------------------
  3973. UnicodeString GetExpandedLogFileName(UnicodeString LogFileName, TSessionData * SessionData)
  3974. {
  3975. // StripPathQuotes should not be needed as we do not feed quotes anymore
  3976. UnicodeString ANewFileName = StripPathQuotes(ExpandEnvironmentVariables(LogFileName));
  3977. TDateTime N = Now();
  3978. for (int Index = 1; Index < ANewFileName.Length(); Index++)
  3979. {
  3980. if (ANewFileName[Index] == L'!')
  3981. {
  3982. UnicodeString Replacement;
  3983. // keep consistent with TFileCustomCommand::PatternReplacement
  3984. switch (tolower(ANewFileName[Index + 1]))
  3985. {
  3986. case L'y':
  3987. Replacement = FormatDateTime(L"yyyy", N);
  3988. break;
  3989. case L'm':
  3990. Replacement = FormatDateTime(L"mm", N);
  3991. break;
  3992. case L'd':
  3993. Replacement = FormatDateTime(L"dd", N);
  3994. break;
  3995. case L't':
  3996. Replacement = FormatDateTime(L"hhnnss", N);
  3997. break;
  3998. case 'p':
  3999. Replacement = IntToStr(static_cast<int>(GetCurrentProcessId()));
  4000. break;
  4001. case L'@':
  4002. if (SessionData != NULL)
  4003. {
  4004. Replacement = MakeValidFileName(SessionData->HostNameExpanded);
  4005. }
  4006. else
  4007. {
  4008. Replacement = L"nohost";
  4009. }
  4010. break;
  4011. case L's':
  4012. if (SessionData != NULL)
  4013. {
  4014. Replacement = MakeValidFileName(SessionData->SessionName);
  4015. }
  4016. else
  4017. {
  4018. Replacement = L"nosession";
  4019. }
  4020. break;
  4021. case L'!':
  4022. Replacement = L"!";
  4023. break;
  4024. default:
  4025. Replacement = UnicodeString(L"!") + ANewFileName[Index + 1];
  4026. break;
  4027. }
  4028. ANewFileName.Delete(Index, 2);
  4029. ANewFileName.Insert(Replacement, Index);
  4030. Index += Replacement.Length() - 1;
  4031. }
  4032. }
  4033. return ANewFileName;
  4034. }
  4035. //---------------------------------------------------------------------
  4036. bool __fastcall IsSshProtocol(TFSProtocol FSProtocol)
  4037. {
  4038. return
  4039. (FSProtocol == fsSFTPonly) || (FSProtocol == fsSFTP) ||
  4040. (FSProtocol == fsSCPonly);
  4041. }
  4042. //---------------------------------------------------------------------------
  4043. int __fastcall DefaultPort(TFSProtocol FSProtocol, TFtps Ftps)
  4044. {
  4045. int Result;
  4046. switch (FSProtocol)
  4047. {
  4048. case fsFTP:
  4049. if (Ftps == ftpsImplicit)
  4050. {
  4051. Result = FtpsImplicitPortNumber;
  4052. }
  4053. else
  4054. {
  4055. Result = FtpPortNumber;
  4056. }
  4057. break;
  4058. case fsWebDAV:
  4059. if (Ftps == ftpsNone)
  4060. {
  4061. Result = HTTPPortNumber;
  4062. }
  4063. else
  4064. {
  4065. Result = HTTPSPortNumber;
  4066. }
  4067. break;
  4068. default:
  4069. if (IsSshProtocol(FSProtocol))
  4070. {
  4071. Result = SshPortNumber;
  4072. }
  4073. else
  4074. {
  4075. DebugFail();
  4076. Result = -1;
  4077. }
  4078. break;
  4079. }
  4080. return Result;
  4081. }
  4082. //---------------------------------------------------------------------