PuttyIntf.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "PuttyIntf.h"
  5. #include "Interface.h"
  6. #include "SecureShell.h"
  7. #include "Exceptions.h"
  8. #include "CoreMain.h"
  9. #include "TextsCore.h"
  10. #include <StrUtils.hpp>
  11. #include <Soap.EncdDecd.hpp>
  12. //---------------------------------------------------------------------------
  13. char sshver[50];
  14. extern const char commitid[] = "";
  15. const bool platform_uses_x11_unix_by_default = true;
  16. CRITICAL_SECTION putty_section;
  17. bool SaveRandomSeed;
  18. bool HadRandomSeed;
  19. char appname_[50];
  20. const char *const appname = appname_;
  21. extern const bool share_can_be_downstream = false;
  22. extern const bool share_can_be_upstream = false;
  23. THierarchicalStorage * PuttyStorage = NULL;
  24. //---------------------------------------------------------------------------
  25. extern "C"
  26. {
  27. #include <winstuff.h>
  28. }
  29. const UnicodeString OriginalPuttyRegistryStorageKey(_T(PUTTY_REG_POS));
  30. const UnicodeString KittyRegistryStorageKey(L"Software\\9bis.com\\KiTTY");
  31. const UnicodeString OriginalPuttyExecutable("putty.exe");
  32. const UnicodeString KittyExecutable("kitty.exe");
  33. const UnicodeString PuttyKeyExt(L"ppk");
  34. //---------------------------------------------------------------------------
  35. void __fastcall PuttyInitialize()
  36. {
  37. SaveRandomSeed = true;
  38. InitializeCriticalSection(&putty_section);
  39. HadRandomSeed = FileExists(ApiPath(Configuration->RandomSeedFileName));
  40. // make sure random generator is initialised, so random_save_seed()
  41. // in destructor can proceed
  42. random_ref();
  43. sk_init();
  44. AnsiString VersionString = AnsiString(SshVersionString());
  45. DebugAssert(!VersionString.IsEmpty() && (static_cast<size_t>(VersionString.Length()) < LENOF(sshver)));
  46. strcpy(sshver, VersionString.c_str());
  47. AnsiString AppName = AnsiString(AppNameString());
  48. DebugAssert(!AppName.IsEmpty() && (static_cast<size_t>(AppName.Length()) < LENOF(appname_)));
  49. strcpy(appname_, AppName.c_str());
  50. }
  51. //---------------------------------------------------------------------------
  52. static bool DeleteRandomSeedOnExit()
  53. {
  54. return !HadRandomSeed && !SaveRandomSeed;
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall PuttyFinalize()
  58. {
  59. if (SaveRandomSeed)
  60. {
  61. random_save_seed();
  62. }
  63. random_unref();
  64. // random_ref in PuttyInitialize creates the seed file. Delete it, if we didn't want to create it.
  65. if (DeleteRandomSeedOnExit())
  66. {
  67. DeleteFile(ApiPath(Configuration->RandomSeedFileName));
  68. }
  69. sk_cleanup();
  70. win_misc_cleanup();
  71. win_secur_cleanup();
  72. ec_cleanup();
  73. wingss_cleanup();
  74. DeleteCriticalSection(&putty_section);
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall DontSaveRandomSeed()
  78. {
  79. SaveRandomSeed = false;
  80. }
  81. //---------------------------------------------------------------------------
  82. bool RandomSeedExists()
  83. {
  84. return
  85. !DeleteRandomSeedOnExit() &&
  86. FileExists(ApiPath(Configuration->RandomSeedFileName));
  87. }
  88. //---------------------------------------------------------------------------
  89. TSecureShell * GetSecureShell(Plug * plug, bool & pfwd)
  90. {
  91. if (!is_ssh(plug) && !is_pfwd(plug))
  92. {
  93. // If it is not SSH/PFwd plug, then it must be Proxy plug.
  94. // Get SSH/PFwd plug which it wraps.
  95. ProxySocket * AProxySocket = get_proxy_plug_socket(plug);
  96. plug = AProxySocket->plug;
  97. }
  98. pfwd = is_pfwd(plug);
  99. Seat * seat;
  100. if (pfwd)
  101. {
  102. seat = get_pfwd_seat(plug);
  103. }
  104. else
  105. {
  106. seat = get_ssh_seat(plug);
  107. }
  108. DebugAssert(seat != NULL);
  109. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  110. return SecureShell;
  111. }
  112. //---------------------------------------------------------------------------
  113. struct callback_set * get_callback_set(Plug * plug)
  114. {
  115. bool pfwd;
  116. TSecureShell * SecureShell = GetSecureShell(plug, pfwd);
  117. return SecureShell->GetCallbackSet();
  118. }
  119. //---------------------------------------------------------------------------
  120. struct callback_set * get_seat_callback_set(Seat * seat)
  121. {
  122. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  123. return SecureShell->GetCallbackSet();
  124. }
  125. //---------------------------------------------------------------------------
  126. extern "C" const char * do_select(Plug * plug, SOCKET skt, bool enable)
  127. {
  128. bool pfwd;
  129. TSecureShell * SecureShell = GetSecureShell(plug, pfwd);
  130. if (!pfwd)
  131. {
  132. SecureShell->UpdateSocket(skt, enable);
  133. }
  134. else
  135. {
  136. SecureShell->UpdatePortFwdSocket(skt, enable);
  137. }
  138. return NULL;
  139. }
  140. //---------------------------------------------------------------------------
  141. static size_t output(Seat * seat, bool is_stderr, const void * data, size_t len)
  142. {
  143. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  144. if (static_cast<int>(static_cast<char>(is_stderr)) == -1)
  145. {
  146. SecureShell->CWrite(reinterpret_cast<const char *>(data), len);
  147. }
  148. else if (!is_stderr)
  149. {
  150. SecureShell->FromBackend(reinterpret_cast<const unsigned char *>(data), len);
  151. }
  152. else
  153. {
  154. SecureShell->AddStdError(reinterpret_cast<const char *>(data), len);
  155. }
  156. return 0;
  157. }
  158. //---------------------------------------------------------------------------
  159. static bool eof(Seat *)
  160. {
  161. return false;
  162. }
  163. //---------------------------------------------------------------------------
  164. static int get_userpass_input(Seat * seat, prompts_t * p, bufchain * DebugUsedArg(input))
  165. {
  166. DebugAssert(p != NULL);
  167. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  168. DebugAssert(SecureShell != NULL);
  169. int Result;
  170. TStrings * Prompts = new TStringList();
  171. TStrings * Results = new TStringList();
  172. try
  173. {
  174. UnicodeString Name = UTF8ToString(p->name);
  175. UnicodeString AName = Name;
  176. TPromptKind PromptKind = SecureShell->IdentifyPromptKind(AName);
  177. bool UTF8Prompt = (PromptKind != pkPassphrase);
  178. for (int Index = 0; Index < int(p->n_prompts); Index++)
  179. {
  180. prompt_t * Prompt = p->prompts[Index];
  181. UnicodeString S;
  182. if (UTF8Prompt)
  183. {
  184. S = UTF8ToString(Prompt->prompt);
  185. }
  186. else
  187. {
  188. S = UnicodeString(AnsiString(Prompt->prompt));
  189. }
  190. Prompts->AddObject(S, (TObject *)(FLAGMASK(Prompt->echo, pupEcho)));
  191. // this fails, when new passwords do not match on change password prompt,
  192. // and putty retries the prompt
  193. DebugAssert(strlen(prompt_get_result_ref(Prompt)) == 0);
  194. Results->Add(L"");
  195. }
  196. UnicodeString Instructions = UTF8ToString(p->instruction);
  197. if (SecureShell->PromptUser(p->to_server, Name, p->name_reqd,
  198. Instructions, p->instr_reqd, Prompts, Results))
  199. {
  200. for (int Index = 0; Index < int(p->n_prompts); Index++)
  201. {
  202. prompt_t * Prompt = p->prompts[Index];
  203. RawByteString S;
  204. if (UTF8Prompt)
  205. {
  206. S = RawByteString(UTF8String(Results->Strings[Index]));
  207. }
  208. else
  209. {
  210. S = RawByteString(AnsiString(Results->Strings[Index]));
  211. }
  212. prompt_set_result(Prompt, S.c_str());
  213. }
  214. Result = 1;
  215. }
  216. else
  217. {
  218. Result = 0;
  219. }
  220. }
  221. __finally
  222. {
  223. delete Prompts;
  224. delete Results;
  225. }
  226. return Result;
  227. }
  228. //---------------------------------------------------------------------------
  229. static void connection_fatal(Seat * seat, const char * message)
  230. {
  231. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  232. SecureShell->PuttyFatalError(UnicodeString(AnsiString(message)));
  233. }
  234. //---------------------------------------------------------------------------
  235. int verify_ssh_host_key(Seat * seat, const char * host, int port, const char * keytype,
  236. char * keystr, const char * DebugUsedArg(keydisp), char ** key_fingerprints,
  237. void (*DebugUsedArg(callback))(void *ctx, int result), void * DebugUsedArg(ctx))
  238. {
  239. UnicodeString FingerprintSHA256, FingerprintMD5;
  240. if (key_fingerprints[SSH_FPTYPE_SHA256] != NULL)
  241. {
  242. FingerprintSHA256 = key_fingerprints[SSH_FPTYPE_SHA256];
  243. }
  244. if (DebugAlwaysTrue(key_fingerprints[SSH_FPTYPE_MD5] != NULL))
  245. {
  246. FingerprintMD5 = key_fingerprints[SSH_FPTYPE_MD5];
  247. }
  248. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  249. SecureShell->VerifyHostKey(host, port, keytype, keystr, FingerprintSHA256, FingerprintMD5);
  250. // We should return 0 when key was not confirmed, we throw exception instead.
  251. return 1;
  252. }
  253. //---------------------------------------------------------------------------
  254. bool have_ssh_host_key(Seat * seat, const char * hostname, int port,
  255. const char * keytype)
  256. {
  257. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  258. return SecureShell->HaveHostKey(hostname, port, keytype) ? 1 : 0;
  259. }
  260. //---------------------------------------------------------------------------
  261. int confirm_weak_crypto_primitive(Seat * seat, const char * algtype, const char * algname,
  262. void (*/*callback*/)(void * ctx, int result), void * /*ctx*/)
  263. {
  264. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  265. SecureShell->AskAlg(algtype, algname);
  266. // We should return 0 when alg was not confirmed, we throw exception instead.
  267. return 1;
  268. }
  269. //---------------------------------------------------------------------------
  270. int confirm_weak_cached_hostkey(Seat *, const char * /*algname*/, const char * /*betteralgs*/,
  271. void (*/*callback*/)(void *ctx, int result), void * /*ctx*/)
  272. {
  273. return 1;
  274. }
  275. //---------------------------------------------------------------------------
  276. void old_keyfile_warning(void)
  277. {
  278. // no reference to TSecureShell instance available
  279. }
  280. //---------------------------------------------------------------------------
  281. void display_banner(Seat * seat, const char * banner, int size)
  282. {
  283. TSecureShell * SecureShell = static_cast<ScpSeat *>(seat)->SecureShell;
  284. UnicodeString Banner(UTF8String(banner, size));
  285. SecureShell->DisplayBanner(Banner);
  286. }
  287. //---------------------------------------------------------------------------
  288. static void SSHFatalError(const char * Format, va_list Param)
  289. {
  290. char Buf[200];
  291. vsnprintf(Buf, LENOF(Buf), Format, Param);
  292. Buf[LENOF(Buf) - 1] = '\0';
  293. // Only few calls from putty\winnet.c might be connected with specific
  294. // TSecureShell. Otherwise called only for really fatal errors
  295. // like 'out of memory' from putty\ssh.c.
  296. throw ESshFatal(NULL, Buf);
  297. }
  298. //---------------------------------------------------------------------------
  299. void modalfatalbox(const char * fmt, ...)
  300. {
  301. va_list Param;
  302. va_start(Param, fmt);
  303. SSHFatalError(fmt, Param);
  304. va_end(Param);
  305. }
  306. //---------------------------------------------------------------------------
  307. void nonfatal(const char * fmt, ...)
  308. {
  309. va_list Param;
  310. va_start(Param, fmt);
  311. SSHFatalError(fmt, Param);
  312. va_end(Param);
  313. }
  314. //---------------------------------------------------------------------------
  315. void ldisc_echoedit_update(Ldisc * /*handle*/)
  316. {
  317. DebugFail();
  318. }
  319. //---------------------------------------------------------------------------
  320. unsigned long schedule_timer(int ticks, timer_fn_t /*fn*/, void * /*ctx*/)
  321. {
  322. return ticks + GetTickCount();
  323. }
  324. //---------------------------------------------------------------------------
  325. void expire_timer_context(void * /*ctx*/)
  326. {
  327. // nothing
  328. }
  329. //---------------------------------------------------------------------------
  330. Pinger * pinger_new(Conf * /*conf*/, Backend * /*back*/)
  331. {
  332. return NULL;
  333. }
  334. //---------------------------------------------------------------------------
  335. void pinger_reconfig(Pinger * /*pinger*/, Conf * /*oldconf*/, Conf * /*newconf*/)
  336. {
  337. // nothing
  338. }
  339. //---------------------------------------------------------------------------
  340. void pinger_free(Pinger * /*pinger*/)
  341. {
  342. // nothing
  343. }
  344. //---------------------------------------------------------------------------
  345. void platform_get_x11_auth(struct X11Display * /*display*/, Conf * /*conf*/)
  346. {
  347. // nothing, therefore no auth.
  348. }
  349. //---------------------------------------------------------------------------
  350. // Based on PuTTY's settings.c
  351. char * get_remote_username(Conf * conf)
  352. {
  353. char * username = conf_get_str(conf, CONF_username);
  354. char * result;
  355. if (*username)
  356. {
  357. result = dupstr(username);
  358. }
  359. else
  360. {
  361. result = NULL;
  362. }
  363. return result;
  364. }
  365. //---------------------------------------------------------------------------
  366. static const SeatVtable ScpSeatVtable =
  367. {
  368. output,
  369. eof,
  370. get_userpass_input,
  371. nullseat_notify_remote_exit,
  372. connection_fatal,
  373. nullseat_update_specials_menu,
  374. nullseat_get_ttymode,
  375. nullseat_set_busy_status,
  376. verify_ssh_host_key,
  377. confirm_weak_crypto_primitive,
  378. confirm_weak_cached_hostkey,
  379. nullseat_is_always_utf8,
  380. nullseat_echoedit_update,
  381. nullseat_get_x_display,
  382. nullseat_get_windowid,
  383. nullseat_get_window_pixel_size,
  384. nullseat_stripctrl_new,
  385. nullseat_set_trust_status_vacuously,
  386. nullseat_verbose_yes,
  387. nullseat_interactive_no,
  388. nullseat_get_cursor_position,
  389. };
  390. //---------------------------------------------------------------------------
  391. ScpSeat::ScpSeat(TSecureShell * ASecureShell)
  392. {
  393. SecureShell = ASecureShell;
  394. vt = &ScpSeatVtable;
  395. }
  396. //---------------------------------------------------------------------------
  397. static std::unique_ptr<TCriticalSection> PuttyRegistrySection(TraceInitPtr(new TCriticalSection()));
  398. enum TPuttyRegistryMode { prmPass, prmRedirect, prmCollect, prmFail };
  399. static TPuttyRegistryMode PuttyRegistryMode = prmRedirect;
  400. typedef std::map<UnicodeString, unsigned long> TPuttyRegistryTypes;
  401. TPuttyRegistryTypes PuttyRegistryTypes;
  402. //---------------------------------------------------------------------------
  403. static long OpenWinSCPKey(HKEY Key, const char * SubKey, HKEY * Result, bool CanCreate)
  404. {
  405. long R;
  406. DebugAssert(Key == HKEY_CURRENT_USER);
  407. DebugUsedParam(Key);
  408. UnicodeString RegKey = SubKey;
  409. int PuttyKeyLen = OriginalPuttyRegistryStorageKey.Length();
  410. DebugAssert(RegKey.SubString(1, PuttyKeyLen) == OriginalPuttyRegistryStorageKey);
  411. RegKey = RegKey.SubString(PuttyKeyLen + 1, RegKey.Length() - PuttyKeyLen);
  412. if (!RegKey.IsEmpty())
  413. {
  414. DebugAssert(RegKey[1] == L'\\');
  415. RegKey.Delete(1, 1);
  416. }
  417. if (RegKey.IsEmpty())
  418. {
  419. *Result = static_cast<HKEY>(NULL);
  420. R = ERROR_SUCCESS;
  421. }
  422. else
  423. {
  424. // we expect this to be called only from retrieve_host_key() or store_host_key()
  425. DebugAssert(RegKey == L"SshHostKeys");
  426. DebugAssert(PuttyStorage != NULL);
  427. DebugAssert(PuttyStorage->AccessMode == (CanCreate ? smReadWrite : smRead));
  428. if (PuttyStorage->OpenSubKey(RegKey, CanCreate))
  429. {
  430. *Result = reinterpret_cast<HKEY>(PuttyStorage);
  431. R = ERROR_SUCCESS;
  432. }
  433. else
  434. {
  435. R = ERROR_CANTOPEN;
  436. }
  437. }
  438. return R;
  439. }
  440. //---------------------------------------------------------------------------
  441. long reg_open_winscp_key(HKEY Key, const char * SubKey, HKEY * Result)
  442. {
  443. if (PuttyRegistryMode == prmPass)
  444. {
  445. return RegOpenKeyA(Key, SubKey, Result);
  446. }
  447. else if (PuttyRegistryMode == prmCollect)
  448. {
  449. *Result = reinterpret_cast<HKEY>(1);
  450. return ERROR_SUCCESS;
  451. }
  452. else if (PuttyRegistryMode == prmFail)
  453. {
  454. return ERROR_CANTOPEN;
  455. }
  456. DebugAssert(PuttyRegistryMode == prmRedirect);
  457. return OpenWinSCPKey(Key, SubKey, Result, false);
  458. }
  459. //---------------------------------------------------------------------------
  460. long reg_create_winscp_key(HKEY Key, const char * SubKey, HKEY * Result)
  461. {
  462. if (PuttyRegistryMode == prmPass)
  463. {
  464. return RegCreateKeyA(Key, SubKey, Result);
  465. }
  466. else if (PuttyRegistryMode == prmCollect)
  467. {
  468. *Result = reinterpret_cast<HKEY>(1);
  469. return ERROR_SUCCESS;
  470. }
  471. DebugAssert(PuttyRegistryMode == prmRedirect);
  472. return OpenWinSCPKey(Key, SubKey, Result, true);
  473. }
  474. //---------------------------------------------------------------------------
  475. long reg_query_winscp_value_ex(HKEY Key, const char * ValueName, unsigned long * Reserved,
  476. unsigned long * Type, unsigned char * Data, unsigned long * DataSize)
  477. {
  478. if (PuttyRegistryMode == prmPass)
  479. {
  480. return RegQueryValueExA(Key, ValueName, Reserved, Type, Data, DataSize);
  481. }
  482. else if (PuttyRegistryMode == prmCollect)
  483. {
  484. return ERROR_READ_FAULT;
  485. }
  486. DebugAssert(PuttyRegistryMode == prmRedirect);
  487. long R;
  488. DebugAssert(Configuration != NULL);
  489. THierarchicalStorage * Storage = reinterpret_cast<THierarchicalStorage *>(Key);
  490. AnsiString Value;
  491. if (Storage == NULL)
  492. {
  493. if (UnicodeString(ValueName) == L"RandSeedFile")
  494. {
  495. Value = AnsiString(Configuration->RandomSeedFileName);
  496. R = ERROR_SUCCESS;
  497. }
  498. else
  499. {
  500. DebugFail();
  501. R = ERROR_READ_FAULT;
  502. }
  503. }
  504. else
  505. {
  506. if (Storage->ValueExists(ValueName))
  507. {
  508. Value = AnsiString(Storage->ReadStringRaw(ValueName, L""));
  509. R = ERROR_SUCCESS;
  510. }
  511. else
  512. {
  513. R = ERROR_READ_FAULT;
  514. }
  515. }
  516. if (R == ERROR_SUCCESS)
  517. {
  518. DebugAssert(Type != NULL);
  519. *Type = REG_SZ;
  520. char * DataStr = reinterpret_cast<char *>(Data);
  521. strncpy(DataStr, Value.c_str(), *DataSize);
  522. DataStr[*DataSize - 1] = '\0';
  523. *DataSize = strlen(DataStr);
  524. }
  525. return R;
  526. }
  527. //---------------------------------------------------------------------------
  528. long reg_set_winscp_value_ex(HKEY Key, const char * ValueName, unsigned long Reserved,
  529. unsigned long Type, const unsigned char * Data, unsigned long DataSize)
  530. {
  531. if (PuttyRegistryMode == prmPass)
  532. {
  533. return RegSetValueExA(Key, ValueName, Reserved, Type, Data, DataSize);
  534. }
  535. else if (PuttyRegistryMode == prmCollect)
  536. {
  537. PuttyRegistryTypes[ValueName] = Type;
  538. return ERROR_SUCCESS;
  539. }
  540. DebugAssert(PuttyRegistryMode == prmRedirect);
  541. DebugAssert(Type == REG_SZ);
  542. DebugUsedParam(Type);
  543. THierarchicalStorage * Storage = reinterpret_cast<THierarchicalStorage *>(Key);
  544. DebugAssert(Storage != NULL);
  545. if (Storage != NULL)
  546. {
  547. UnicodeString Value(reinterpret_cast<const char*>(Data), DataSize - 1);
  548. Storage->WriteStringRaw(ValueName, Value);
  549. }
  550. return ERROR_SUCCESS;
  551. }
  552. //---------------------------------------------------------------------------
  553. long reg_close_winscp_key(HKEY Key)
  554. {
  555. if (PuttyRegistryMode == prmPass)
  556. {
  557. return RegCloseKey(Key);
  558. }
  559. else if (PuttyRegistryMode == prmCollect)
  560. {
  561. return ERROR_SUCCESS;
  562. }
  563. DebugAssert(PuttyRegistryMode == prmRedirect);
  564. return ERROR_SUCCESS;
  565. }
  566. //---------------------------------------------------------------------------
  567. TKeyType KeyType(UnicodeString FileName)
  568. {
  569. DebugAssert(ktUnopenable == SSH_KEYTYPE_UNOPENABLE);
  570. DebugAssert(ktSSHCom == SSH_KEYTYPE_SSHCOM);
  571. DebugAssert(ktSSH2PublicOpenSSH == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH);
  572. UTF8String UtfFileName = UTF8String(FileName);
  573. Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  574. TKeyType Result = (TKeyType)key_type(KeyFile);
  575. filename_free(KeyFile);
  576. return Result;
  577. }
  578. //---------------------------------------------------------------------------
  579. bool IsKeyEncrypted(TKeyType KeyType, const UnicodeString & FileName, UnicodeString & Comment)
  580. {
  581. UTF8String UtfFileName = UTF8String(FileName);
  582. bool Result;
  583. char * CommentStr = NULL;
  584. Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  585. try
  586. {
  587. switch (KeyType)
  588. {
  589. case ktSSH2:
  590. Result = (ppk_encrypted_f(KeyFile, &CommentStr) != 0);
  591. break;
  592. case ktOpenSSHPEM:
  593. case ktOpenSSHNew:
  594. case ktSSHCom:
  595. Result = (import_encrypted(KeyFile, KeyType, &CommentStr) != NULL);
  596. break;
  597. default:
  598. DebugFail();
  599. Result = false;
  600. break;
  601. }
  602. }
  603. __finally
  604. {
  605. filename_free(KeyFile);
  606. }
  607. if (CommentStr != NULL)
  608. {
  609. Comment = UnicodeString(AnsiString(CommentStr));
  610. // ktOpenSSH has no comment, PuTTY defaults to file path
  611. if (Comment == FileName)
  612. {
  613. Comment = ExtractFileName(FileName);
  614. }
  615. sfree(CommentStr);
  616. }
  617. return Result;
  618. }
  619. //---------------------------------------------------------------------------
  620. TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const UnicodeString & Passphrase, UnicodeString & Error)
  621. {
  622. UTF8String UtfFileName = UTF8String(FileName);
  623. Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  624. struct ssh2_userkey * Ssh2Key = NULL;
  625. const char * ErrorStr = NULL;
  626. AnsiString AnsiPassphrase = Passphrase;
  627. try
  628. {
  629. switch (KeyType)
  630. {
  631. case ktSSH2:
  632. Ssh2Key = ppk_load_f(KeyFile, AnsiPassphrase.c_str(), &ErrorStr);
  633. break;
  634. case ktOpenSSHPEM:
  635. case ktOpenSSHNew:
  636. case ktSSHCom:
  637. Ssh2Key = import_ssh2(KeyFile, KeyType, AnsiPassphrase.c_str(), &ErrorStr);
  638. break;
  639. default:
  640. DebugFail();
  641. break;
  642. }
  643. }
  644. __finally
  645. {
  646. Shred(AnsiPassphrase);
  647. filename_free(KeyFile);
  648. }
  649. if (Ssh2Key == NULL)
  650. {
  651. // While theoretically we may get "unable to open key file" and
  652. // so we should check system error code,
  653. // we actully never get here unless we call KeyType previously
  654. // and handle ktUnopenable accordingly.
  655. Error = AnsiString(ErrorStr);
  656. }
  657. else if (Ssh2Key == SSH2_WRONG_PASSPHRASE)
  658. {
  659. Error = EmptyStr;
  660. Ssh2Key = NULL;
  661. }
  662. return reinterpret_cast<TPrivateKey *>(Ssh2Key);
  663. }
  664. //---------------------------------------------------------------------------
  665. TPrivateKey * LoadKey(TKeyType KeyType, const UnicodeString & FileName, const UnicodeString & Passphrase)
  666. {
  667. UnicodeString Error;
  668. TPrivateKey * Result = LoadKey(KeyType, FileName, Passphrase, Error);
  669. if (Result == NULL)
  670. {
  671. if (!Error.IsEmpty())
  672. {
  673. throw Exception(Error);
  674. }
  675. else
  676. {
  677. throw Exception(LoadStr(AUTH_TRANSL_WRONG_PASSPHRASE));
  678. }
  679. }
  680. return Result;
  681. }
  682. //---------------------------------------------------------------------------
  683. UnicodeString TestKey(TKeyType KeyType, const UnicodeString & FileName)
  684. {
  685. UnicodeString Result;
  686. TPrivateKey * Key = LoadKey(KeyType, FileName, EmptyStr, Result);
  687. if (Key != NULL)
  688. {
  689. FreeKey(Key);
  690. }
  691. return Result;
  692. }
  693. //---------------------------------------------------------------------------
  694. void ChangeKeyComment(TPrivateKey * PrivateKey, const UnicodeString & Comment)
  695. {
  696. AnsiString AnsiComment(Comment);
  697. struct ssh2_userkey * Ssh2Key = reinterpret_cast<struct ssh2_userkey *>(PrivateKey);
  698. sfree(Ssh2Key->comment);
  699. Ssh2Key->comment = dupstr(AnsiComment.c_str());
  700. }
  701. //---------------------------------------------------------------------------
  702. void SaveKey(TKeyType KeyType, const UnicodeString & FileName,
  703. const UnicodeString & Passphrase, TPrivateKey * PrivateKey)
  704. {
  705. UTF8String UtfFileName = UTF8String(FileName);
  706. Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  707. try
  708. {
  709. struct ssh2_userkey * Ssh2Key = reinterpret_cast<struct ssh2_userkey *>(PrivateKey);
  710. AnsiString AnsiPassphrase = Passphrase;
  711. char * PassphrasePtr = (AnsiPassphrase.IsEmpty() ? NULL : AnsiPassphrase.c_str());
  712. switch (KeyType)
  713. {
  714. case ktSSH2:
  715. {
  716. ppk_save_parameters Params = ppk_save_default_parameters;
  717. if (Configuration->KeyVersion != 0)
  718. {
  719. Params.fmt_version = Configuration->KeyVersion;
  720. }
  721. if (!ppk_save_f(KeyFile, Ssh2Key, PassphrasePtr, &Params))
  722. {
  723. int Error = errno;
  724. throw EOSExtException(FMTLOAD(KEY_SAVE_ERROR, (FileName)), Error);
  725. }
  726. }
  727. break;
  728. default:
  729. DebugFail();
  730. break;
  731. }
  732. }
  733. __finally
  734. {
  735. filename_free(KeyFile);
  736. }
  737. }
  738. //---------------------------------------------------------------------------
  739. void FreeKey(TPrivateKey * PrivateKey)
  740. {
  741. struct ssh2_userkey * Ssh2Key = reinterpret_cast<struct ssh2_userkey *>(PrivateKey);
  742. ssh_key_free(Ssh2Key->key);
  743. sfree(Ssh2Key->comment);
  744. sfree(Ssh2Key);
  745. }
  746. //---------------------------------------------------------------------------
  747. RawByteString LoadPublicKey(const UnicodeString & FileName, UnicodeString & Algorithm, UnicodeString & Comment)
  748. {
  749. RawByteString Result;
  750. UTF8String UtfFileName = UTF8String(FileName);
  751. Filename * KeyFile = filename_from_str(UtfFileName.c_str());
  752. try
  753. {
  754. char * AlgorithmStr = NULL;
  755. char * CommentStr = NULL;
  756. const char * ErrorStr = NULL;
  757. strbuf * PublicKeyBuf = strbuf_new();
  758. if (!ppk_loadpub_f(KeyFile, &AlgorithmStr, BinarySink_UPCAST(PublicKeyBuf), &CommentStr, &ErrorStr))
  759. {
  760. UnicodeString Error = UnicodeString(AnsiString(ErrorStr));
  761. throw Exception(Error);
  762. }
  763. Algorithm = UnicodeString(AnsiString(AlgorithmStr));
  764. sfree(AlgorithmStr);
  765. Comment = UnicodeString(AnsiString(CommentStr));
  766. sfree(CommentStr);
  767. Result = RawByteString(reinterpret_cast<char *>(PublicKeyBuf->s), PublicKeyBuf->len);
  768. strbuf_free(PublicKeyBuf);
  769. }
  770. __finally
  771. {
  772. filename_free(KeyFile);
  773. }
  774. return Result;
  775. }
  776. //---------------------------------------------------------------------------
  777. UnicodeString GetPublicKeyLine(const UnicodeString & FileName, UnicodeString & Comment)
  778. {
  779. UnicodeString Algorithm;
  780. RawByteString PublicKey = LoadPublicKey(FileName, Algorithm, Comment);
  781. UnicodeString PublicKeyBase64 = EncodeBase64(PublicKey.c_str(), PublicKey.Length());
  782. PublicKeyBase64 = ReplaceStr(PublicKeyBase64, L"\r", L"");
  783. PublicKeyBase64 = ReplaceStr(PublicKeyBase64, L"\n", L"");
  784. UnicodeString Result = FORMAT(L"%s %s %s", (Algorithm, PublicKeyBase64, Comment));
  785. return Result;
  786. }
  787. //---------------------------------------------------------------------------
  788. bool __fastcall HasGSSAPI(UnicodeString CustomPath)
  789. {
  790. static int has = -1;
  791. if (has < 0)
  792. {
  793. Conf * conf = conf_new();
  794. ssh_gss_liblist * List = NULL;
  795. try
  796. {
  797. Filename * filename = filename_from_str(UTF8String(CustomPath).c_str());
  798. conf_set_filename(conf, CONF_ssh_gss_custom, filename);
  799. filename_free(filename);
  800. List = ssh_gss_setup(conf, NULL);
  801. for (int Index = 0; (has <= 0) && (Index < List->nlibraries); Index++)
  802. {
  803. ssh_gss_library * library = &List->libraries[Index];
  804. Ssh_gss_ctx ctx;
  805. memset(&ctx, 0, sizeof(ctx));
  806. has =
  807. ((library->acquire_cred(library, &ctx, NULL) == SSH_GSS_OK) &&
  808. (library->release_cred(library, &ctx) == SSH_GSS_OK)) ? 1 : 0;
  809. }
  810. }
  811. __finally
  812. {
  813. ssh_gss_cleanup(List);
  814. conf_free(conf);
  815. }
  816. if (has < 0)
  817. {
  818. has = 0;
  819. }
  820. }
  821. return (has > 0);
  822. }
  823. //---------------------------------------------------------------------------
  824. static void __fastcall DoNormalizeFingerprint(UnicodeString & Fingerprint, UnicodeString & KeyName, UnicodeString & KeyType)
  825. {
  826. const int MaxCount = 10;
  827. const ssh_keyalg * SignKeys[MaxCount];
  828. int Count = LENOF(SignKeys);
  829. // We may use find_pubkey_alg, but it gets complicated with normalized fingerprint
  830. // as the names have different number of dashes
  831. get_hostkey_algs(&Count, SignKeys);
  832. for (int Index = 0; Index < Count; Index++)
  833. {
  834. const ssh_keyalg * SignKey = SignKeys[Index];
  835. UnicodeString Name = UnicodeString(SignKey->ssh_id);
  836. if (StartsStr(Name + L" ", Fingerprint))
  837. {
  838. UnicodeString Rest = Fingerprint.SubString(Name.Length() + 2, Fingerprint.Length() - Name.Length() - 1);
  839. int Space = Rest.Pos(L" ");
  840. // If not a number, it's an invalid input,
  841. // either something completelly wrong, or it can be OpenSSH base64 public key,
  842. // that got here from TPasteKeyHandler::Paste
  843. if (IsNumber(Rest.SubString(1, Space - 1)))
  844. {
  845. KeyName = Name;
  846. Fingerprint = Rest.SubString(Space + 1, Fingerprint.Length() - Space);
  847. Fingerprint = Base64ToUrlSafe(Fingerprint);
  848. Fingerprint = MD5ToUrlSafe(Fingerprint);
  849. KeyType = UnicodeString(SignKey->cache_id);
  850. return;
  851. }
  852. }
  853. else if (StartsStr(Name + NormalizedFingerprintSeparator, Fingerprint))
  854. {
  855. KeyType = UnicodeString(SignKey->cache_id);
  856. KeyName = Name;
  857. Fingerprint.Delete(1, Name.Length() + 1);
  858. return;
  859. }
  860. }
  861. }
  862. //---------------------------------------------------------------------------
  863. void __fastcall NormalizeFingerprint(UnicodeString & Fingerprint, UnicodeString & KeyName)
  864. {
  865. UnicodeString KeyType;
  866. DoNormalizeFingerprint(Fingerprint, KeyName, KeyType);
  867. }
  868. //---------------------------------------------------------------------------
  869. UnicodeString __fastcall KeyTypeFromFingerprint(UnicodeString Fingerprint)
  870. {
  871. UnicodeString KeyType;
  872. UnicodeString KeyName; // unused
  873. DoNormalizeFingerprint(Fingerprint, KeyName, KeyType);
  874. return KeyType;
  875. }
  876. //---------------------------------------------------------------------------
  877. UnicodeString __fastcall GetPuTTYVersion()
  878. {
  879. // "Release 0.64"
  880. // "Pre-release 0.65:2015-07-20.95501a1"
  881. // "Development snapshot 2015-12-22.51465fa"
  882. UnicodeString Result = get_putty_version();
  883. // Skip "Release", "Pre-release", "Development snapshot"
  884. int P = Result.LastDelimiter(L" ");
  885. Result.Delete(1, P);
  886. return Result;
  887. }
  888. //---------------------------------------------------------------------------
  889. UnicodeString __fastcall Sha256(const char * Data, size_t Size)
  890. {
  891. unsigned char Digest[32];
  892. hash_simple(&ssh_sha256, make_ptrlen(Data, Size), Digest);
  893. UnicodeString Result(BytesToHex(Digest, LENOF(Digest)));
  894. return Result;
  895. }
  896. //---------------------------------------------------------------------------
  897. void __fastcall DllHijackingProtection()
  898. {
  899. dll_hijacking_protection();
  900. }
  901. //---------------------------------------------------------------------------
  902. UnicodeString __fastcall ParseOpenSshPubLine(const UnicodeString & Line, const struct ssh_keyalg *& Algorithm)
  903. {
  904. UTF8String UtfLine = UTF8String(Line);
  905. char * AlgorithmName = NULL;
  906. char * CommentPtr = NULL;
  907. const char * ErrorStr = NULL;
  908. strbuf * PubBlobBuf = strbuf_new();
  909. BinarySource Source[1];
  910. BinarySource_BARE_INIT(Source, UtfLine.c_str(), UtfLine.Length());
  911. UnicodeString Result;
  912. if (!openssh_loadpub(Source, &AlgorithmName, BinarySink_UPCAST(PubBlobBuf), &CommentPtr, &ErrorStr))
  913. {
  914. throw Exception(UnicodeString(ErrorStr));
  915. }
  916. else
  917. {
  918. try
  919. {
  920. Algorithm = find_pubkey_alg(AlgorithmName);
  921. if (Algorithm == NULL)
  922. {
  923. throw Exception(FORMAT(L"Unknown public key algorithm \"%s\".", (AlgorithmName)));
  924. }
  925. ptrlen PtrLen = { PubBlobBuf->s, PubBlobBuf->len };
  926. ssh_key * Key = Algorithm->new_pub(Algorithm, PtrLen);
  927. if (Key == NULL)
  928. {
  929. throw Exception(L"Invalid public key.");
  930. }
  931. char * FmtKey = Algorithm->cache_str(Key);
  932. Result = UnicodeString(FmtKey);
  933. sfree(FmtKey);
  934. Algorithm->freekey(Key);
  935. }
  936. __finally
  937. {
  938. strbuf_free(PubBlobBuf);
  939. sfree(AlgorithmName);
  940. sfree(CommentPtr);
  941. }
  942. }
  943. return Result;
  944. }
  945. //---------------------------------------------------------------------------
  946. UnicodeString __fastcall GetKeyTypeHuman(const UnicodeString & KeyType)
  947. {
  948. UnicodeString Result;
  949. if (KeyType == ssh_dss.cache_id)
  950. {
  951. Result = L"DSA";
  952. }
  953. else if ((KeyType == ssh_rsa.cache_id) ||
  954. (KeyType == L"rsa")) // SSH1
  955. {
  956. Result = L"RSA";
  957. }
  958. else if (KeyType == ssh_ecdsa_ed25519.cache_id)
  959. {
  960. Result = L"Ed25519";
  961. }
  962. else if (KeyType == ssh_ecdsa_nistp256.cache_id)
  963. {
  964. Result = L"ECDSA/nistp256";
  965. }
  966. else if (KeyType == ssh_ecdsa_nistp384.cache_id)
  967. {
  968. Result = L"ECDSA/nistp384";
  969. }
  970. else if (KeyType == ssh_ecdsa_nistp521.cache_id)
  971. {
  972. Result = L"ECDSA/nistp521";
  973. }
  974. else
  975. {
  976. DebugFail();
  977. Result = KeyType;
  978. }
  979. return Result;
  980. }
  981. //---------------------------------------------------------------------------
  982. bool IsOpenSSH(const UnicodeString & SshImplementation)
  983. {
  984. return
  985. // e.g. "OpenSSH_5.3"
  986. (SshImplementation.Pos(L"OpenSSH") == 1) ||
  987. // Sun SSH is based on OpenSSH (suffers the same bugs)
  988. (SshImplementation.Pos(L"Sun_SSH") == 1);
  989. }
  990. //---------------------------------------------------------------------------
  991. TStrings * SshCipherList()
  992. {
  993. std::unique_ptr<TStrings> Result(new TStringList());
  994. // Same order as DefaultCipherList
  995. const ssh2_ciphers * Ciphers[] = { &ssh2_aes, &ssh2_ccp, &ssh2_blowfish, &ssh2_3des, &ssh2_arcfour, &ssh2_des };
  996. for (unsigned int Index = 0; Index < LENOF(Ciphers); Index++)
  997. {
  998. for (int Index2 = 0; Index2 < Ciphers[Index]->nciphers; Index2++)
  999. {
  1000. UnicodeString Name = UnicodeString(Ciphers[Index]->list[Index2]->ssh2_id);
  1001. Result->Add(Name);
  1002. }
  1003. }
  1004. return Result.release();
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. TStrings * SshKexList()
  1008. {
  1009. std::unique_ptr<TStrings> Result(new TStringList());
  1010. // Same order as DefaultKexList
  1011. const ssh_kexes * Kexes[] = { &ssh_ecdh_kex, &ssh_diffiehellman_gex, &ssh_diffiehellman_group14, &ssh_rsa_kex, &ssh_diffiehellman_group1 };
  1012. for (unsigned int Index = 0; Index < LENOF(Kexes); Index++)
  1013. {
  1014. for (int Index2 = 0; Index2 < Kexes[Index]->nkexes; Index2++)
  1015. {
  1016. UnicodeString Name = UnicodeString(Kexes[Index]->list[Index2]->name);
  1017. Result->Add(Name);
  1018. }
  1019. }
  1020. return Result.release();
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. TStrings * SshHostKeyList()
  1024. {
  1025. std::unique_ptr<TStrings> Result(new TStringList());
  1026. const int MaxCount = 10;
  1027. const ssh_keyalg * SignKeys[MaxCount];
  1028. int Count = LENOF(SignKeys);
  1029. get_hostkey_algs(&Count, SignKeys);
  1030. for (int Index = 0; Index < Count; Index++)
  1031. {
  1032. const ssh_keyalg * SignKey = SignKeys[Index];
  1033. UnicodeString Name = UnicodeString(SignKey->ssh_id);
  1034. Result->Add(Name);
  1035. }
  1036. return Result.release();
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. TStrings * SshMacList()
  1040. {
  1041. std::unique_ptr<TStrings> Result(new TStringList());
  1042. const struct ssh2_macalg ** Macs = NULL;
  1043. int Count = 0;
  1044. get_macs(&Count, &Macs);
  1045. for (int Index = 0; Index < Count; Index++)
  1046. {
  1047. UnicodeString Name = UnicodeString(Macs[Index]->name);
  1048. UnicodeString S = Name;
  1049. UnicodeString ETMName = UnicodeString(Macs[Index]->etm_name);
  1050. if (!ETMName.IsEmpty())
  1051. {
  1052. S = FORMAT(L"%s (%s)", (S, ETMName));
  1053. }
  1054. Result->Add(S);
  1055. }
  1056. return Result.release();
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. UnicodeString GetCipherName(const ssh_cipher * Cipher)
  1060. {
  1061. return UnicodeString(UTF8String(Cipher->vt->text_name));
  1062. }
  1063. //---------------------------------------------------------------------------
  1064. UnicodeString GetCompressorName(const ssh_compressor * Compressor)
  1065. {
  1066. UnicodeString Result;
  1067. if (Compressor != NULL)
  1068. {
  1069. Result = UnicodeString(UTF8String(Compressor->vt->name));
  1070. }
  1071. return Result;
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. UnicodeString GetDecompressorName(const ssh_decompressor * Decompressor)
  1075. {
  1076. UnicodeString Result;
  1077. if (Decompressor != NULL)
  1078. {
  1079. Result = UnicodeString(UTF8String(Decompressor->vt->name));
  1080. }
  1081. return Result;
  1082. }
  1083. //---------------------------------------------------------------------------
  1084. void WritePuttySettings(THierarchicalStorage * Storage, const UnicodeString & ASettings)
  1085. {
  1086. if (PuttyRegistryTypes.empty())
  1087. {
  1088. TGuard Guard(PuttyRegistrySection.get());
  1089. TValueRestorer<TPuttyRegistryMode> PuttyRegistryModeRestorer(PuttyRegistryMode);
  1090. PuttyRegistryMode = prmCollect;
  1091. Conf * conf = conf_new();
  1092. try
  1093. {
  1094. do_defaults(NULL, conf);
  1095. save_settings(NULL, conf);
  1096. }
  1097. __finally
  1098. {
  1099. conf_free(conf);
  1100. }
  1101. }
  1102. std::unique_ptr<TStrings> Settings(new TStringList());
  1103. UnicodeString Buf = ASettings;
  1104. UnicodeString Setting;
  1105. while (CutToken(Buf, Setting))
  1106. {
  1107. Settings->Add(Setting);
  1108. }
  1109. for (int Index = 0; Index < Settings->Count; Index++)
  1110. {
  1111. UnicodeString Name = Settings->Names[Index];
  1112. TPuttyRegistryTypes::const_iterator IType = PuttyRegistryTypes.find(Name);
  1113. if (IType != PuttyRegistryTypes.end())
  1114. {
  1115. UnicodeString Value = Settings->ValueFromIndex[Index];
  1116. int I;
  1117. if (IType->second == REG_SZ)
  1118. {
  1119. Storage->WriteStringRaw(Name, Value);
  1120. }
  1121. else if (DebugAlwaysTrue(IType->second == REG_DWORD) &&
  1122. TryStrToInt(Value, I))
  1123. {
  1124. Storage->WriteInteger(Name, I);
  1125. }
  1126. }
  1127. }
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void PuttyDefaults(Conf * conf)
  1131. {
  1132. TGuard Guard(PuttyRegistrySection.get());
  1133. TValueRestorer<TPuttyRegistryMode> PuttyRegistryModeRestorer(PuttyRegistryMode);
  1134. PuttyRegistryMode = prmFail;
  1135. do_defaults(NULL, conf);
  1136. }
  1137. //---------------------------------------------------------------------------
  1138. void SavePuttyDefaults(const UnicodeString & Name)
  1139. {
  1140. TGuard Guard(PuttyRegistrySection.get());
  1141. TValueRestorer<TPuttyRegistryMode> PuttyRegistryModeRestorer(PuttyRegistryMode);
  1142. PuttyRegistryMode = prmPass;
  1143. Conf * conf = conf_new();
  1144. try
  1145. {
  1146. PuttyDefaults(conf);
  1147. AnsiString PuttyName = PuttyStr(Name);
  1148. save_settings(PuttyName.c_str(), conf);
  1149. }
  1150. __finally
  1151. {
  1152. conf_free(conf);
  1153. }
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. //---------------------------------------------------------------------------