PuttyIntf.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #define PUTTY_DO_GLOBALS
  5. #include "PuttyIntf.h"
  6. #include "Interface.h"
  7. #include "SecureShell.h"
  8. #include "Exceptions.h"
  9. #include "CoreMain.h"
  10. #include "TextsCore.h"
  11. //---------------------------------------------------------------------------
  12. char sshver[50];
  13. const int platform_uses_x11_unix_by_default = TRUE;
  14. CRITICAL_SECTION noise_section;
  15. bool SaveRandomSeed;
  16. char appname_[50];
  17. const char *const appname = appname_;
  18. //---------------------------------------------------------------------------
  19. void __fastcall PuttyInitialize()
  20. {
  21. SaveRandomSeed = true;
  22. InitializeCriticalSection(&noise_section);
  23. // make sure random generator is initialised, so random_save_seed()
  24. // in destructor can proceed
  25. random_ref();
  26. flags = FLAG_VERBOSE | FLAG_SYNCAGENT; // verbose log
  27. sk_init();
  28. AnsiString VersionString = SshVersionString();
  29. assert(!VersionString.IsEmpty() && (static_cast<size_t>(VersionString.Length()) < LENOF(sshver)));
  30. strcpy(sshver, VersionString.c_str());
  31. AnsiString AppName = AppNameString();
  32. assert(!AppName.IsEmpty() && (static_cast<size_t>(AppName.Length()) < LENOF(appname_)));
  33. strcpy(appname_, AppName.c_str());
  34. }
  35. //---------------------------------------------------------------------------
  36. void __fastcall PuttyFinalize()
  37. {
  38. if (SaveRandomSeed)
  39. {
  40. random_save_seed();
  41. }
  42. random_unref();
  43. sk_cleanup();
  44. win_misc_cleanup();
  45. DeleteCriticalSection(&noise_section);
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall DontSaveRandomSeed()
  49. {
  50. SaveRandomSeed = false;
  51. }
  52. //---------------------------------------------------------------------------
  53. extern "C" char * do_select(Plug plug, SOCKET skt, int startup)
  54. {
  55. void * frontend;
  56. if (!is_ssh(plug) && !is_pfwd(plug))
  57. {
  58. // If it is not SSH/PFwd plug, then it must be Proxy plug.
  59. // Get SSH/PFwd plug which it wraps.
  60. Proxy_Socket ProxySocket = ((Proxy_Plug)plug)->proxy_socket;
  61. plug = ProxySocket->plug;
  62. }
  63. bool pfwd = is_pfwd(plug);
  64. if (pfwd)
  65. {
  66. plug = (Plug)get_pfwd_backend(plug);
  67. }
  68. frontend = get_ssh_frontend(plug);
  69. assert(frontend);
  70. TSecureShell * SecureShell = reinterpret_cast<TSecureShell*>(frontend);
  71. if (!pfwd)
  72. {
  73. SecureShell->UpdateSocket(skt, startup);
  74. }
  75. else
  76. {
  77. SecureShell->UpdatePortFwdSocket(skt, startup);
  78. }
  79. return NULL;
  80. }
  81. //---------------------------------------------------------------------------
  82. int from_backend(void * frontend, int is_stderr, const char * data, int datalen)
  83. {
  84. assert(frontend);
  85. if (is_stderr >= 0)
  86. {
  87. assert((is_stderr == 0) || (is_stderr == 1));
  88. ((TSecureShell *)frontend)->FromBackend((is_stderr == 1), reinterpret_cast<const unsigned char *>(data), datalen);
  89. }
  90. else
  91. {
  92. assert(is_stderr == -1);
  93. ((TSecureShell *)frontend)->CWrite(data, datalen);
  94. }
  95. return 0;
  96. }
  97. //---------------------------------------------------------------------------
  98. int from_backend_untrusted(void * /*frontend*/, const char * /*data*/, int /*len*/)
  99. {
  100. // currently used with authentication banner only,
  101. // for which we have own interface display_banner
  102. return 0;
  103. }
  104. //---------------------------------------------------------------------------
  105. int get_userpass_input(prompts_t * p, unsigned char * /*in*/, int /*inlen*/)
  106. {
  107. assert(p != NULL);
  108. TSecureShell * SecureShell = reinterpret_cast<TSecureShell *>(p->frontend);
  109. assert(SecureShell != NULL);
  110. int Result;
  111. TStrings * Prompts = new TStringList();
  112. TStrings * Results = new TStringList();
  113. try
  114. {
  115. for (int Index = 0; Index < int(p->n_prompts); Index++)
  116. {
  117. prompt_t * Prompt = p->prompts[Index];
  118. Prompts->AddObject(Prompt->prompt, (TObject *)(FLAGMASK(Prompt->echo, pupEcho)));
  119. Results->AddObject(L"", (TObject *)Prompt->result_len);
  120. }
  121. if (SecureShell->PromptUser(p->to_server, p->name, p->name_reqd,
  122. p->instruction, p->instr_reqd, Prompts, Results))
  123. {
  124. for (int Index = 0; Index < int(p->n_prompts); Index++)
  125. {
  126. prompt_t * Prompt = p->prompts[Index];
  127. strncpy(Prompt->result, AnsiString(Results->Strings[Index]).c_str(), Prompt->result_len);
  128. Prompt->result[Prompt->result_len - 1] = '\0';
  129. }
  130. Result = 1;
  131. }
  132. else
  133. {
  134. Result = 0;
  135. }
  136. }
  137. __finally
  138. {
  139. delete Prompts;
  140. delete Results;
  141. }
  142. return Result;
  143. }
  144. //---------------------------------------------------------------------------
  145. char * get_ttymode(void * /*frontend*/, const char * /*mode*/)
  146. {
  147. // should never happen when Config.nopty == TRUE
  148. assert(false);
  149. return NULL;
  150. }
  151. //---------------------------------------------------------------------------
  152. void logevent(void * frontend, const char * string)
  153. {
  154. // Frontend maybe NULL here
  155. if (frontend != NULL)
  156. {
  157. ((TSecureShell *)frontend)->PuttyLogEvent(string);
  158. }
  159. }
  160. //---------------------------------------------------------------------------
  161. void connection_fatal(void * frontend, char * fmt, ...)
  162. {
  163. va_list Param;
  164. char Buf[200];
  165. va_start(Param, fmt);
  166. vsnprintf(Buf, LENOF(Buf), fmt, Param); \
  167. Buf[LENOF(Buf) - 1] = '\0'; \
  168. va_end(Param);
  169. assert(frontend != NULL);
  170. ((TSecureShell *)frontend)->PuttyFatalError(Buf);
  171. }
  172. //---------------------------------------------------------------------------
  173. int verify_ssh_host_key(void * frontend, char * host, int port, char * keytype,
  174. char * keystr, char * fingerprint, void (*/*callback*/)(void * ctx, int result),
  175. void * /*ctx*/)
  176. {
  177. assert(frontend != NULL);
  178. ((TSecureShell *)frontend)->VerifyHostKey(host, port, keytype, keystr, fingerprint);
  179. // We should return 0 when key was not confirmed, we throw exception instead.
  180. return 1;
  181. }
  182. //---------------------------------------------------------------------------
  183. int askalg(void * frontend, const char * algtype, const char * algname,
  184. void (*/*callback*/)(void * ctx, int result), void * /*ctx*/)
  185. {
  186. assert(frontend != NULL);
  187. ((TSecureShell *)frontend)->AskAlg(algtype, algname);
  188. // We should return 0 when alg was not confirmed, we throw exception instead.
  189. return 1;
  190. }
  191. //---------------------------------------------------------------------------
  192. void old_keyfile_warning(void)
  193. {
  194. // no reference to TSecureShell instace available
  195. }
  196. //---------------------------------------------------------------------------
  197. void display_banner(void * frontend, const char * banner, int size)
  198. {
  199. assert(frontend);
  200. UnicodeString Banner(banner, size);
  201. ((TSecureShell *)frontend)->DisplayBanner(Banner);
  202. }
  203. //---------------------------------------------------------------------------
  204. static void SSHFatalError(const char * Format, va_list Param)
  205. {
  206. char Buf[200];
  207. vsnprintf(Buf, LENOF(Buf), Format, Param);
  208. Buf[LENOF(Buf) - 1] = '\0';
  209. // Only few calls from putty\winnet.c might be connected with specific
  210. // TSecureShell. Otherwise called only for really fatal errors
  211. // like 'out of memory' from putty\ssh.c.
  212. throw ESshFatal(NULL, Buf);
  213. }
  214. //---------------------------------------------------------------------------
  215. void fatalbox(char * fmt, ...)
  216. {
  217. va_list Param;
  218. va_start(Param, fmt);
  219. SSHFatalError(fmt, Param);
  220. va_end(Param);
  221. }
  222. //---------------------------------------------------------------------------
  223. void modalfatalbox(char * fmt, ...)
  224. {
  225. va_list Param;
  226. va_start(Param, fmt);
  227. SSHFatalError(fmt, Param);
  228. va_end(Param);
  229. }
  230. //---------------------------------------------------------------------------
  231. void cleanup_exit(int /*code*/)
  232. {
  233. throw ESshFatal(NULL, "");
  234. }
  235. //---------------------------------------------------------------------------
  236. int askappend(void * /*frontend*/, Filename /*filename*/,
  237. void (*/*callback*/)(void * ctx, int result), void * /*ctx*/)
  238. {
  239. // this is called from logging.c of putty, which is never used with WinSCP
  240. assert(false);
  241. return 0;
  242. }
  243. //---------------------------------------------------------------------------
  244. void ldisc_send(void * /*handle*/, char * /*buf*/, int len, int /*interactive*/)
  245. {
  246. // This is only here because of the calls to ldisc_send(NULL,
  247. // 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
  248. // as an ldisc. So if we get called with any real data, I want
  249. // to know about it.
  250. assert(len == 0);
  251. USEDPARAM(len);
  252. }
  253. //---------------------------------------------------------------------------
  254. void agent_schedule_callback(void (* /*callback*/)(void *, void *, int),
  255. void * /*callback_ctx*/, void * /*data*/, int /*len*/)
  256. {
  257. assert(false);
  258. }
  259. //---------------------------------------------------------------------------
  260. void notify_remote_exit(void * /*frontend*/)
  261. {
  262. // nothing
  263. }
  264. //---------------------------------------------------------------------------
  265. void update_specials_menu(void * /*frontend*/)
  266. {
  267. // nothing
  268. }
  269. //---------------------------------------------------------------------------
  270. typedef void (*timer_fn_t)(void *ctx, long now);
  271. long schedule_timer(int ticks, timer_fn_t /*fn*/, void * /*ctx*/)
  272. {
  273. return ticks + GetTickCount();
  274. }
  275. //---------------------------------------------------------------------------
  276. void expire_timer_context(void * /*ctx*/)
  277. {
  278. // nothing
  279. }
  280. //---------------------------------------------------------------------------
  281. Pinger pinger_new(Config * /*cfg*/, Backend * /*back*/, void * /*backhandle*/)
  282. {
  283. return NULL;
  284. }
  285. //---------------------------------------------------------------------------
  286. void pinger_reconfig(Pinger /*pinger*/, Config * /*oldcfg*/, Config * /*newcfg*/)
  287. {
  288. // nothing
  289. }
  290. //---------------------------------------------------------------------------
  291. void pinger_free(Pinger /*pinger*/)
  292. {
  293. // nothing
  294. }
  295. //---------------------------------------------------------------------------
  296. void set_busy_status(void * /*frontend*/, int /*status*/)
  297. {
  298. // nothing
  299. }
  300. //---------------------------------------------------------------------------
  301. void platform_get_x11_auth(struct X11Display * /*display*/, const Config * /*cfg*/)
  302. {
  303. // nothing, therefore no auth.
  304. }
  305. //---------------------------------------------------------------------------
  306. int get_remote_username(Config * cfg, char *user, size_t len)
  307. {
  308. if (*cfg->username)
  309. {
  310. strncpy(user, cfg->username, len);
  311. user[len-1] = '\0';
  312. }
  313. else
  314. {
  315. *user = '\0';
  316. }
  317. return (*user != '\0');
  318. }
  319. //---------------------------------------------------------------------------
  320. static long OpenWinSCPKey(HKEY Key, const char * SubKey, HKEY * Result, bool CanCreate)
  321. {
  322. long R;
  323. assert(Configuration != NULL);
  324. assert(Key == HKEY_CURRENT_USER);
  325. USEDPARAM(Key);
  326. UnicodeString RegKey = SubKey;
  327. int PuttyKeyLen = Configuration->PuttyRegistryStorageKey.Length();
  328. assert(RegKey.SubString(1, PuttyKeyLen) == Configuration->PuttyRegistryStorageKey);
  329. RegKey = RegKey.SubString(PuttyKeyLen + 1, RegKey.Length() - PuttyKeyLen);
  330. if (!RegKey.IsEmpty())
  331. {
  332. assert(RegKey[1] == L'\\');
  333. RegKey.Delete(1, 1);
  334. }
  335. if (RegKey.IsEmpty())
  336. {
  337. *Result = static_cast<HKEY>(NULL);
  338. R = ERROR_SUCCESS;
  339. }
  340. else
  341. {
  342. // we expect this to be called only from verify_host_key() or store_host_key()
  343. assert(RegKey == L"SshHostKeys");
  344. THierarchicalStorage * Storage = Configuration->CreateScpStorage(false);
  345. Storage->AccessMode = (CanCreate ? smReadWrite : smRead);
  346. if (Storage->OpenSubKey(RegKey, CanCreate))
  347. {
  348. *Result = reinterpret_cast<HKEY>(Storage);
  349. R = ERROR_SUCCESS;
  350. }
  351. else
  352. {
  353. delete Storage;
  354. R = ERROR_CANTOPEN;
  355. }
  356. }
  357. return R;
  358. }
  359. //---------------------------------------------------------------------------
  360. long reg_open_winscp_key(HKEY Key, const char * SubKey, HKEY * Result)
  361. {
  362. return OpenWinSCPKey(Key, SubKey, Result, false);
  363. }
  364. //---------------------------------------------------------------------------
  365. long reg_create_winscp_key(HKEY Key, const char * SubKey, HKEY * Result)
  366. {
  367. return OpenWinSCPKey(Key, SubKey, Result, true);
  368. }
  369. //---------------------------------------------------------------------------
  370. long reg_query_winscp_value_ex(HKEY Key, const char * ValueName, unsigned long * /*Reserved*/,
  371. unsigned long * Type, unsigned char * Data, unsigned long * DataSize)
  372. {
  373. long R;
  374. assert(Configuration != NULL);
  375. THierarchicalStorage * Storage = reinterpret_cast<THierarchicalStorage *>(Key);
  376. AnsiString Value;
  377. if (Storage == NULL)
  378. {
  379. if (UnicodeString(ValueName) == L"RandSeedFile")
  380. {
  381. Value = Configuration->RandomSeedFileName;
  382. R = ERROR_SUCCESS;
  383. }
  384. else
  385. {
  386. assert(false);
  387. R = ERROR_READ_FAULT;
  388. }
  389. }
  390. else
  391. {
  392. if (Storage->ValueExists(ValueName))
  393. {
  394. Value = Storage->ReadStringRaw(ValueName, L"");
  395. R = ERROR_SUCCESS;
  396. }
  397. else
  398. {
  399. R = ERROR_READ_FAULT;
  400. }
  401. }
  402. if (R == ERROR_SUCCESS)
  403. {
  404. assert(Type != NULL);
  405. *Type = REG_SZ;
  406. char * DataStr = reinterpret_cast<char *>(Data);
  407. strncpy(DataStr, Value.c_str(), *DataSize);
  408. DataStr[*DataSize - 1] = '\0';
  409. *DataSize = strlen(DataStr);
  410. }
  411. return R;
  412. }
  413. //---------------------------------------------------------------------------
  414. long reg_set_winscp_value_ex(HKEY Key, const char * ValueName, unsigned long /*Reserved*/,
  415. unsigned long Type, const unsigned char * Data, unsigned long DataSize)
  416. {
  417. assert(Configuration != NULL);
  418. assert(Type == REG_SZ);
  419. USEDPARAM(Type);
  420. THierarchicalStorage * Storage = reinterpret_cast<THierarchicalStorage *>(Key);
  421. assert(Storage != NULL);
  422. if (Storage != NULL)
  423. {
  424. UnicodeString Value(reinterpret_cast<const char*>(Data), DataSize - 1);
  425. Storage->WriteStringRaw(ValueName, Value);
  426. }
  427. return ERROR_SUCCESS;
  428. }
  429. //---------------------------------------------------------------------------
  430. long reg_close_winscp_key(HKEY Key)
  431. {
  432. assert(Configuration != NULL);
  433. THierarchicalStorage * Storage = reinterpret_cast<THierarchicalStorage *>(Key);
  434. if (Storage != NULL)
  435. {
  436. delete Storage;
  437. }
  438. return ERROR_SUCCESS;
  439. }
  440. //---------------------------------------------------------------------------
  441. TKeyType KeyType(UnicodeString FileName)
  442. {
  443. assert(ktUnopenable == SSH_KEYTYPE_UNOPENABLE);
  444. assert(ktSSHCom == SSH_KEYTYPE_SSHCOM);
  445. Filename KeyFile;
  446. ASCOPY(KeyFile.path, FileName);
  447. return (TKeyType)key_type(&KeyFile);
  448. }
  449. //---------------------------------------------------------------------------
  450. UnicodeString KeyTypeName(TKeyType KeyType)
  451. {
  452. return key_type_to_str(KeyType);
  453. }
  454. //---------------------------------------------------------------------------
  455. __int64 __fastcall ParseSize(UnicodeString SizeStr)
  456. {
  457. AnsiString AnsiSizeStr = SizeStr;
  458. return parse_blocksize(AnsiSizeStr.c_str());
  459. }
  460. //---------------------------------------------------------------------------
  461. bool __fastcall HasGSSAPI()
  462. {
  463. static int has = -1;
  464. if (has < 0)
  465. {
  466. Config cfg;
  467. memset(&cfg, 0, sizeof(cfg));
  468. ssh_gss_liblist * List = ssh_gss_setup(&cfg);
  469. try
  470. {
  471. for (int Index = 0; (has <= 0) && (Index < List->nlibraries); Index++)
  472. {
  473. ssh_gss_library * library = &List->libraries[Index];
  474. Ssh_gss_ctx ctx;
  475. memset(&ctx, 0, sizeof(ctx));
  476. has =
  477. ((library->acquire_cred(library, &ctx) == SSH_GSS_OK) &&
  478. (library->release_cred(library, &ctx) == SSH_GSS_OK)) ? 1 : 0;
  479. }
  480. }
  481. __finally
  482. {
  483. ssh_gss_cleanup(List);
  484. }
  485. if (has < 0)
  486. {
  487. has = 0;
  488. }
  489. }
  490. return (has > 0);
  491. }
  492. //---------------------------------------------------------------------------