SessionData.cpp 165 KB

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