ImportSessions.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "ImportSessions.h"
  6. #include <Configuration.h>
  7. #include <CoreMain.h>
  8. #include <VCLCommon.h>
  9. #include <WinInterface.h>
  10. #include <TextsWin.h>
  11. #include <CoreMain.h>
  12. #include <Tools.h>
  13. #include <WinApi.h>
  14. #include <PasTools.hpp>
  15. //---------------------------------------------------------------------
  16. #pragma resource "*.dfm"
  17. //---------------------------------------------------------------------
  18. const int OpensshIndex = 2;
  19. const int KnownHostsIndex = 3;
  20. //---------------------------------------------------------------------
  21. bool __fastcall DoImportSessionsDialog(TList * Imported)
  22. {
  23. std::unique_ptr<TStrings> Errors(new TStringList());
  24. std::unique_ptr<TList> SessionListsList(new TList());
  25. UnicodeString Error;
  26. std::unique_ptr<TStoredSessionList> PuttyImportSessionList(
  27. GUIConfiguration->SelectPuttySessionsForImport(StoredSessions, Error));
  28. SessionListsList->Add(PuttyImportSessionList.get());
  29. Errors->Add(Error);
  30. std::unique_ptr<TStoredSessionList> FilezillaImportSessionList(
  31. Configuration->SelectFilezillaSessionsForImport(StoredSessions, Error));
  32. SessionListsList->Add(FilezillaImportSessionList.get());
  33. Errors->Add(Error);
  34. std::unique_ptr<TStoredSessionList> OpensshImportSessionList(
  35. Configuration->SelectOpensshSessionsForImport(StoredSessions, Error));
  36. SessionListsList->Add(OpensshImportSessionList.get());
  37. Errors->Add(Error);
  38. std::unique_ptr<TStoredSessionList> KnownHostsImportSessionList(
  39. Configuration->SelectKnownHostsSessionsForImport(StoredSessions, Error));
  40. DebugAssert(KnownHostsIndex == SessionListsList->Count);
  41. SessionListsList->Add(KnownHostsImportSessionList.get());
  42. Errors->Add(Error);
  43. DebugAssert(SessionListsList->Count == Errors->Count);
  44. std::unique_ptr<TImportSessionsDialog> ImportSessionsDialog(
  45. SafeFormCreate<TImportSessionsDialog>(Application));
  46. ImportSessionsDialog->Init(SessionListsList.get(), Errors.get());
  47. bool Result = ImportSessionsDialog->Execute();
  48. if (Result)
  49. {
  50. // Particularly when importing known_hosts, there is no feedback.
  51. TInstantOperationVisualizer Visualizer;
  52. UnicodeString PuttyHostKeysSourceKey = Configuration->PuttyRegistryStorageKey;
  53. TStoredSessionList * AKnownHostsImportSessionList =
  54. static_cast<TStoredSessionList *>(SessionListsList->Items[KnownHostsIndex]);
  55. StoredSessions->Import(PuttyImportSessionList.get(), true, Imported);
  56. TStoredSessionList::ImportHostKeys(PuttyHostKeysSourceKey, PuttyImportSessionList.get(), true);
  57. StoredSessions->Import(FilezillaImportSessionList.get(), true, Imported);
  58. // FileZilla uses PuTTY's host key store
  59. TStoredSessionList::ImportHostKeys(PuttyHostKeysSourceKey, FilezillaImportSessionList.get(), true);
  60. StoredSessions->Import(OpensshImportSessionList.get(), true, Imported);
  61. // The actual import will be done by ImportSelectedKnownHosts
  62. TStoredSessionList::SelectKnownHostsForSelectedSessions(AKnownHostsImportSessionList, OpensshImportSessionList.get());
  63. TStoredSessionList::ImportSelectedKnownHosts(AKnownHostsImportSessionList);
  64. }
  65. return Result;
  66. }
  67. //---------------------------------------------------------------------
  68. __fastcall TImportSessionsDialog::TImportSessionsDialog(TComponent * AOwner) :
  69. TForm(AOwner)
  70. {
  71. UseSystemSettings(this);
  72. // this is loaded from res string to force translation
  73. Caption = LoadStr(IMPORT_CAPTION);
  74. }
  75. //---------------------------------------------------------------------
  76. void __fastcall TImportSessionsDialog::Init(TList * SessionListsList, TStrings * Errors)
  77. {
  78. FSessionListsList = SessionListsList;
  79. FErrors = Errors;
  80. for (int Index = 0; Index < SessionListsList->Count; Index++)
  81. {
  82. if ((SourceComboBox->ItemIndex < 0) && (GetSessionList(Index)->Count > 0))
  83. {
  84. SourceComboBox->ItemIndex = Index;
  85. }
  86. }
  87. if (SourceComboBox->ItemIndex < 0)
  88. {
  89. SourceComboBox->ItemIndex = 0;
  90. }
  91. int Offset = ScaleByTextHeight(this, 8);
  92. ErrorPanel->BoundsRect =
  93. TRect(
  94. SessionListView2->BoundsRect.Left + Offset, SessionListView2->BoundsRect.Top + Offset,
  95. SessionListView2->BoundsRect.Right - Offset, SessionListView2->BoundsRect.Bottom - Offset);
  96. }
  97. //---------------------------------------------------------------------
  98. TStoredSessionList * __fastcall TImportSessionsDialog::GetSessionList(int Index)
  99. {
  100. return reinterpret_cast<TStoredSessionList *>(FSessionListsList->Items[Index]);
  101. }
  102. //---------------------------------------------------------------------
  103. void __fastcall TImportSessionsDialog::UpdateControls()
  104. {
  105. PasteButton->Visible = (SourceComboBox->ItemIndex == KnownHostsIndex);
  106. EnableControl(PasteButton, IsFormatInClipboard(CF_TEXT));
  107. EnableControl(OKButton, ListViewAnyChecked(SessionListView2));
  108. EnableControl(CheckAllButton, SessionListView2->Items->Count > 0);
  109. AutoSizeListColumnsWidth(SessionListView2);
  110. }
  111. //---------------------------------------------------------------------
  112. void __fastcall TImportSessionsDialog::ClearSelections()
  113. {
  114. for (int Index = 0; Index < SourceComboBox->Items->Count; Index++)
  115. {
  116. TStoredSessionList * SessionList = GetSessionList(Index);
  117. for (int Index = 0; Index < SessionList->Count; Index++)
  118. {
  119. SessionList->Sessions[Index]->Selected = false;
  120. }
  121. }
  122. }
  123. //---------------------------------------------------------------------
  124. TSessionData * TImportSessionsDialog::GetSessionData(TListItem * Item)
  125. {
  126. return DebugNotNull(static_cast<TSessionData *>(Item->Data));
  127. }
  128. //---------------------------------------------------------------------
  129. void __fastcall TImportSessionsDialog::SaveSelection()
  130. {
  131. for (int Index = 0; Index < SessionListView2->Items->Count; Index++)
  132. {
  133. TListItem * Item = SessionListView2->Items->Item[Index];
  134. GetSessionData(Item)->Selected = Item->Checked;
  135. }
  136. }
  137. //---------------------------------------------------------------------
  138. void __fastcall TImportSessionsDialog::LoadSessions()
  139. {
  140. TStoredSessionList * SessionList = GetSessionList(SourceComboBox->ItemIndex);
  141. SessionListView2->Items->BeginUpdate();
  142. try
  143. {
  144. SessionListView2->Items->Clear();
  145. for (int Index = 0; Index < SessionList->Count; Index++)
  146. {
  147. TSessionData * Session = SessionList->Sessions[Index];
  148. TListItem * Item = SessionListView2->Items->Add();
  149. Item->Data = Session;
  150. Item->Caption = Session->Name;
  151. Item->Checked = Session->Selected;
  152. }
  153. }
  154. __finally
  155. {
  156. SessionListView2->Items->EndUpdate();
  157. }
  158. UnicodeString Error = FErrors->Strings[SourceComboBox->ItemIndex];
  159. if ((SessionList->Count > 0) || Error.IsEmpty())
  160. {
  161. ErrorPanel->Visible = false;
  162. }
  163. else
  164. {
  165. ErrorLabel->Caption = Error;
  166. ErrorPanel->Visible = true;
  167. }
  168. UpdateControls();
  169. }
  170. //---------------------------------------------------------------------------
  171. void __fastcall TImportSessionsDialog::SessionListView2InfoTip(
  172. TObject * /*Sender*/, TListItem * Item, UnicodeString & InfoTip)
  173. {
  174. TSessionData * Data = GetSessionData(Item);
  175. if (SourceComboBox->ItemIndex == KnownHostsIndex)
  176. {
  177. UnicodeString Algs;
  178. UnicodeString HostKeys = Data->HostKey;
  179. while (!HostKeys.IsEmpty())
  180. {
  181. UnicodeString HostKey = CutToChar(HostKeys, L';', true);
  182. UnicodeString Alg = CutToChar(HostKey, L':', true);
  183. AddToList(Algs, Alg, L", ");
  184. }
  185. InfoTip = FMTLOAD(IMPORT_KNOWNHOSTS_INFO_TIP, (Data->HostName, Algs));
  186. }
  187. else
  188. {
  189. InfoTip = Data->InfoTip;
  190. }
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TImportSessionsDialog::SessionListView2MouseDown(
  194. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState /*Shift*/,
  195. int /*X*/, int /*Y*/)
  196. {
  197. UpdateControls();
  198. }
  199. //---------------------------------------------------------------------------
  200. void __fastcall TImportSessionsDialog::SessionListView2KeyUp(
  201. TObject * /*Sender*/, WORD & /*Key*/, TShiftState /*Shift*/)
  202. {
  203. UpdateControls();
  204. }
  205. //---------------------------------------------------------------------------
  206. void __fastcall TImportSessionsDialog::FormShow(TObject * /*Sender*/)
  207. {
  208. // Load only now, as earlier loading somehow breaks SessionListView2 layout on initial per-monitor DPI scaling
  209. LoadSessions();
  210. }
  211. //---------------------------------------------------------------------------
  212. void __fastcall TImportSessionsDialog::CheckAllButtonClick(TObject * /*Sender*/)
  213. {
  214. ListViewCheckAll(SessionListView2, caToggle);
  215. UpdateControls();
  216. }
  217. //---------------------------------------------------------------------------
  218. void __fastcall TImportSessionsDialog::HelpButtonClick(TObject * /*Sender*/)
  219. {
  220. FormHelp(this);
  221. }
  222. //---------------------------------------------------------------------------
  223. bool __fastcall TImportSessionsDialog::Execute()
  224. {
  225. bool Result = (ShowModal() == DefaultResult(this));
  226. if (Result)
  227. {
  228. ClearSelections();
  229. SaveSelection();
  230. if (SourceComboBox->ItemIndex == OpensshIndex)
  231. {
  232. std::unique_ptr<TStrings> ConvertedSessions(new TStringList());
  233. std::unique_ptr<TStrings> ConvertedKeyFiles(new TStringList());
  234. std::unique_ptr<TStrings> NotConvertedKeyFiles(CreateSortedStringList());
  235. for (int Index = 0; Index < SessionListView2->Items->Count; Index++)
  236. {
  237. TListItem * Item = SessionListView2->Items->Item[Index];
  238. if (Item->Checked)
  239. {
  240. TSessionData * Data = GetSessionData(Item);
  241. if (!Data->PublicKeyFile.IsEmpty() &&
  242. FileExists(ApiPath(Data->PublicKeyFile)))
  243. {
  244. UnicodeString CanonicalPath = GetCanonicalPath(Data->PublicKeyFile);
  245. // Reuses the already converted keys saved under a custom name
  246. // (when saved under the default name they would be captured by the later condition based on GetConvertedKeyFileName)
  247. int CanonicalIndex = ConvertedKeyFiles->IndexOfName(CanonicalPath);
  248. bool ConvertedSession = false;
  249. if (CanonicalIndex >= 0)
  250. {
  251. Data->PublicKeyFile = ConvertedKeyFiles->ValueFromIndex[CanonicalIndex];
  252. ConvertedSession = true;
  253. }
  254. // Prevents asking about converting the same key again, when the user refuses the conversion.
  255. else if (NotConvertedKeyFiles->IndexOf(CanonicalPath) >= 0)
  256. {
  257. // noop
  258. }
  259. else
  260. {
  261. UnicodeString ConvertedFilename = GetConvertedKeyFileName(Data->PublicKeyFile);
  262. UnicodeString FileName;
  263. if (FileExists(ApiPath(ConvertedFilename)))
  264. {
  265. FileName = ConvertedFilename;
  266. ConvertedSession = true;
  267. }
  268. else
  269. {
  270. FileName = Data->PublicKeyFile;
  271. TDateTime TimestampBefore, TimestampAfter;
  272. FileAge(FileName, TimestampBefore);
  273. try
  274. {
  275. VerifyAndConvertKey(FileName, true);
  276. FileAge(FileName, TimestampAfter);
  277. if ((Data->PublicKeyFile != FileName) ||
  278. // should never happen as cancelling the saving throws EAbort
  279. DebugAlwaysTrue(TimestampBefore != TimestampAfter))
  280. {
  281. ConvertedSession = true;
  282. }
  283. }
  284. catch (EAbort &)
  285. {
  286. NotConvertedKeyFiles->Add(CanonicalPath);
  287. }
  288. }
  289. if (ConvertedSession)
  290. {
  291. Data->PublicKeyFile = FileName;
  292. ConvertedKeyFiles->Values[CanonicalPath] = FileName;
  293. }
  294. }
  295. if (ConvertedSession)
  296. {
  297. ConvertedSessions->Add(FORMAT(L"%s (%s)", (Data->Name, Data->PublicKeyFile)));
  298. }
  299. }
  300. }
  301. }
  302. if (ConvertedSessions->Count > 0)
  303. {
  304. UnicodeString Message = MainInstructions(FMTLOAD(IMPORT_CONVERTED_KEYS, (ConvertedKeyFiles->Count, ConvertedSessions->Count)));
  305. MoreMessageDialog(Message, ConvertedSessions.get(), qtInformation, qaOK, HelpKeyword);
  306. }
  307. }
  308. }
  309. return Result;
  310. }
  311. //---------------------------------------------------------------------------
  312. void __fastcall TImportSessionsDialog::SourceComboBoxSelect(TObject * /*Sender*/)
  313. {
  314. SaveSelection();
  315. LoadSessions();
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TImportSessionsDialog::CreateHandle()
  319. {
  320. TForm::CreateHandle();
  321. if (DebugAlwaysTrue(HandleAllocated()))
  322. {
  323. HINSTANCE User32Library = LoadLibrary(L"user32.dll");
  324. AddClipboardFormatListenerProc AddClipboardFormatListener =
  325. (AddClipboardFormatListenerProc)GetProcAddress(User32Library, "AddClipboardFormatListener");
  326. if (AddClipboardFormatListener != NULL)
  327. {
  328. AddClipboardFormatListener(Handle);
  329. }
  330. }
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall TImportSessionsDialog::DestroyHandle()
  334. {
  335. if (DebugAlwaysTrue(HandleAllocated()))
  336. {
  337. HINSTANCE User32Library = LoadLibrary(L"user32.dll");
  338. RemoveClipboardFormatListenerProc RemoveClipboardFormatListener =
  339. (RemoveClipboardFormatListenerProc)GetProcAddress(User32Library, "RemoveClipboardFormatListener");
  340. if (RemoveClipboardFormatListener != NULL)
  341. {
  342. RemoveClipboardFormatListener(Handle);
  343. }
  344. }
  345. TForm::DestroyHandle();
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TImportSessionsDialog::Dispatch(void * Message)
  349. {
  350. TMessage * M = static_cast<TMessage*>(Message);
  351. switch (M->Msg)
  352. {
  353. case WM_CLIPBOARDUPDATE:
  354. UpdateControls();
  355. TForm::Dispatch(Message);
  356. break;
  357. default:
  358. TForm::Dispatch(Message);
  359. break;
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TImportSessionsDialog::PasteButtonClick(TObject * /*Sender*/)
  364. {
  365. UnicodeString Text;
  366. // Proceed even when retrieving from clipboard fails, "no host keys" error will show.
  367. TextFromClipboard(Text, false);
  368. std::unique_ptr<TStrings> Lines(new TStringList());
  369. Lines->Text = Text;
  370. SessionListView2->Items->Clear();
  371. int Index = SourceComboBox->ItemIndex;
  372. UnicodeString Error;
  373. FPastedKnownHosts.reset(Configuration->SelectKnownHostsSessionsForImport(Lines.get(), StoredSessions, Error));
  374. FSessionListsList->Items[Index] = FPastedKnownHosts.get();
  375. FErrors->Strings[Index] = Error;
  376. LoadSessions();
  377. }
  378. //---------------------------------------------------------------------------