SessionData.cpp 171 KB

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