SessionData.cpp 177 KB

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