SessionData.cpp 139 KB

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