SessionData.cpp 156 KB

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