SessionData.cpp 135 KB

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