SessionData.cpp 174 KB

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