PuttyIntf.cpp 37 KB

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