SessionData.cpp 166 KB

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