SessionData.cpp 169 KB

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