PuttyIntf.cpp 38 KB

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