SessionData.cpp 164 KB

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