SessionData.cpp 138 KB

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