SessionData.cpp 174 KB

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