SessionData.cpp 139 KB

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