PuttyIntf.cpp 37 KB

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