SessionData.cpp 135 KB

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