Authenticate.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Authenticate.h"
  6. #include <VCLCommon.h>
  7. #include <TextsWin.h>
  8. #include <Terminal.h>
  9. #include <CoreMain.h>
  10. #include <PasTools.hpp>
  11. #include <CustomWinConfiguration.h>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. #pragma link "PasswordEdit"
  15. #ifndef NO_RESOURCES
  16. #pragma resource "*.dfm"
  17. #endif
  18. //---------------------------------------------------------------------------
  19. __fastcall TAuthenticateForm::TAuthenticateForm(TComponent * Owner)
  20. : TForm(Owner), FSessionData(NULL), FTerminal(NULL)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TAuthenticateForm::Init(TTerminal * Terminal)
  25. {
  26. FTerminal = Terminal;
  27. FSessionData = Terminal->SessionData;
  28. UseSystemSettings(this);
  29. FShowAsModalStorage = NULL;
  30. FFocusControl = NULL;
  31. UseDesktopFont(LogView);
  32. FAnimationPainted = false;
  33. FShowNoActivate = false;
  34. FPromptParent = InstructionsLabel->Parent;
  35. FPromptLeft = InstructionsLabel->Left;
  36. FPromptTop = InstructionsLabel->Top;
  37. FPromptRight = FPromptParent->ClientWidth - InstructionsLabel->Width - FPromptLeft;
  38. FPromptEditGap = PromptEdit1->Top - PromptLabel1->Top - PromptLabel1->Height;
  39. FPromptsGap = PromptLabel2->Top - PromptEdit1->Top - PromptEdit1->Height;
  40. ClientHeight = ScaleByTextHeight(this, 270);
  41. FHorizontalLogPadding = ScaleByTextHeight(this, 4);
  42. FVerticalLogPadding = ScaleByTextHeight(this, 3);
  43. FLogTextFormat << tfNoPrefix << tfWordBreak << tfVerticalCenter;
  44. ClearLog();
  45. }
  46. //---------------------------------------------------------------------------
  47. __fastcall TAuthenticateForm::~TAuthenticateForm()
  48. {
  49. ReleaseAsModal(this, FShowAsModalStorage);
  50. }
  51. //---------------------------------------------------------------------------
  52. void __fastcall TAuthenticateForm::ShowAsModal()
  53. {
  54. if (IsApplicationMinimized())
  55. {
  56. FShowNoActivate = true;
  57. }
  58. // Do not call BringToFront when minimized, so that we do not have to use the same hack as in TMessageForm::SetZOrder
  59. ::ShowAsModal(this, FShowAsModalStorage, !FShowNoActivate);
  60. HookFormActivation(this);
  61. }
  62. //---------------------------------------------------------------------------
  63. void __fastcall TAuthenticateForm::HideAsModal()
  64. {
  65. ::HideAsModal(this, FShowAsModalStorage);
  66. UnhookFormActivation(this);
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TAuthenticateForm::CMShowingChanged(TMessage & Message)
  70. {
  71. if (Showing && FShowNoActivate)
  72. {
  73. ShowFormNoActivate(this);
  74. }
  75. else
  76. {
  77. TForm::Dispatch(&Message);
  78. }
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TAuthenticateForm::WMNCCreate(TWMNCCreate & Message)
  82. {
  83. // bypass TForm::WMNCCreate to prevent disabling "resize"
  84. // (which is done for bsDialog, see comments in CreateParams)
  85. DefaultHandler(&Message);
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TAuthenticateForm::DoCancel()
  89. {
  90. if (FOnCancel != NULL)
  91. {
  92. FOnCancel(this);
  93. }
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TAuthenticateForm::Dispatch(void * AMessage)
  97. {
  98. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  99. if (Message.Msg == WM_NCCREATE)
  100. {
  101. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  102. }
  103. else if (Message.Msg == WM_CLOSE)
  104. {
  105. DoCancel();
  106. TForm::Dispatch(AMessage);
  107. }
  108. else if (Message.Msg == WM_MANAGES_CAPTION)
  109. {
  110. // caption managed in TAuthenticateForm::AdjustControls()
  111. Message.Result = 1;
  112. }
  113. else if (Message.Msg == CM_SHOWINGCHANGED)
  114. {
  115. CMShowingChanged(Message);
  116. }
  117. else
  118. {
  119. TForm::Dispatch(AMessage);
  120. }
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall TAuthenticateForm::CreateParams(TCreateParams & Params)
  124. {
  125. TForm::CreateParams(Params);
  126. // Allow resizing of the window, even if this is bsDialog.
  127. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  128. // reason receive focus, if window is shown atop non-main window
  129. // (like editor)
  130. Params.Style = Params.Style | WS_THICKFRAME;
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall TAuthenticateForm::FormShow(TObject * /*Sender*/)
  134. {
  135. AdjustControls();
  136. if (FFocusControl != NULL)
  137. {
  138. ActiveControl = FFocusControl;
  139. }
  140. UnicodeString AnimationName = FSessionData->IsSecure() ? L"ConnectingSecure" : L"ConnectingInsecure";
  141. FFrameAnimation.Init(AnimationPaintBox, AnimationName);
  142. FFrameAnimation.Start();
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TAuthenticateForm::ClearLog()
  146. {
  147. // TListItems::Clear() does nothing without allocated handle
  148. LogView->HandleNeeded();
  149. LogView->Items->Clear();
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TAuthenticateForm::Log(const UnicodeString Message)
  153. {
  154. // HACK
  155. // The first call to Repaint from TFrameAnimation happens
  156. // even before the form is showing. After it is shown it takes sometime too long
  157. // before the animation is painted, so that the form is closed before the first frame even appers
  158. if (!FAnimationPainted && Showing)
  159. {
  160. AnimationPaintBox->Repaint();
  161. FAnimationPainted = true;
  162. }
  163. int Index = LogView->Items->Add(Message);
  164. MakeLogItemVisible(Index);
  165. LogView->Repaint();
  166. }
  167. //---------------------------------------------------------------------------
  168. void __fastcall TAuthenticateForm::MakeLogItemVisible(int Index)
  169. {
  170. if (Index < LogView->TopIndex)
  171. {
  172. LogView->TopIndex = Index;
  173. }
  174. else
  175. {
  176. int TotalHeight = 0;
  177. int Index2 = Index;
  178. while ((Index2 >= 0) && TotalHeight < LogView->ClientHeight)
  179. {
  180. TotalHeight += LogItemHeight(Index2);
  181. Index2--;
  182. }
  183. // Index2 is the last item above the first fully visible,
  184. // were the Index on the very bottom.
  185. if (LogView->TopIndex <= Index2 + 1)
  186. {
  187. LogView->TopIndex = Index2 + 2;
  188. }
  189. }
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TAuthenticateForm::AdjustControls()
  193. {
  194. UnicodeString ACaption;
  195. if (FStatus.IsEmpty())
  196. {
  197. ACaption = FSessionData->SessionName;
  198. }
  199. else
  200. {
  201. ACaption = FORMAT(L"%s - %s", (FStatus, FSessionData->SessionName));
  202. }
  203. Caption = FormatFormCaption(this, ACaption);
  204. }
  205. //---------------------------------------------------------------------------
  206. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  207. {
  208. TLabel * Result = new TLabel(this);
  209. Result->Parent = FPromptParent;
  210. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  211. Result->WordWrap = true;
  212. Result->AutoSize = false;
  213. Result->Top = Current;
  214. Result->Left = FPromptLeft;
  215. Result->Caption = Caption;
  216. int Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  217. Result->Width = Width;
  218. Result->AutoSize = true;
  219. Result->AutoSize = false;
  220. Result->Width = Width;
  221. return Result;
  222. }
  223. //---------------------------------------------------------------------------
  224. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo)
  225. {
  226. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  227. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  228. Result->Parent = FPromptParent;
  229. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  230. Result->Top = Current;
  231. Result->Left = FPromptLeft;
  232. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  233. return Result;
  234. }
  235. //---------------------------------------------------------------------------
  236. TList * __fastcall TAuthenticateForm::GeneratePrompt(UnicodeString Instructions,
  237. TStrings * Prompts)
  238. {
  239. while (FPromptParent->ControlCount > 0)
  240. {
  241. delete FPromptParent->Controls[0];
  242. }
  243. TList * Result = new TList;
  244. int Current = FPromptTop;
  245. if (!Instructions.IsEmpty())
  246. {
  247. TLabel * Label = GenerateLabel(Current, Instructions);
  248. Current += Label->Height + FPromptsGap;
  249. }
  250. for (int Index = 0; Index < Prompts->Count; Index++)
  251. {
  252. if (Index > 0)
  253. {
  254. Current += FPromptEditGap;
  255. }
  256. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  257. Current += Label->Height + FPromptEditGap;
  258. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  259. TCustomEdit * Edit = GenerateEdit(Current, Echo);
  260. Result->Add(Edit);
  261. Label->FocusControl = Edit;
  262. Current += Edit->Height;
  263. }
  264. FPromptParent->ClientHeight = Current;
  265. return Result;
  266. }
  267. //---------------------------------------------------------------------------
  268. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  269. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  270. bool StoredCredentialsTried)
  271. {
  272. bool Result;
  273. TList * Edits = GeneratePrompt(Instructions, Prompts);
  274. try
  275. {
  276. bool ShowSessionRememberPasswordPanel = false;
  277. bool ShowSavePasswordPanel = false;
  278. TSessionData * Data = NULL;
  279. if (IsPasswordPrompt(Kind, Prompts) && StoredCredentialsTried)
  280. {
  281. Data = StoredSessions->FindSame(FSessionData);
  282. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  283. }
  284. // do not offer to remember password,
  285. // if we are offering to save the password to stored site
  286. if (!ShowSavePasswordPanel &&
  287. (Prompts->Count == 1) &&
  288. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  289. DebugAlwaysTrue(IsPasswordOrPassphrasePrompt(Kind, Prompts)))
  290. {
  291. ShowSessionRememberPasswordPanel = true;
  292. }
  293. SavePasswordCheck->Checked = false;
  294. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  295. SessionRememberPasswordCheck->Checked = false;
  296. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  297. if (PasswordPanel->AutoSize)
  298. {
  299. PasswordPanel->AutoSize = false;
  300. PasswordPanel->AutoSize = true;
  301. }
  302. PasswordPanel->Realign();
  303. DebugAssert(Results->Count == Edits->Count);
  304. for (int Index = 0; Index < Edits->Count; Index++)
  305. {
  306. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  307. Edit->Text = Results->Strings[Index];
  308. }
  309. Result = Execute(Name, PasswordPanel,
  310. ((Edits->Count > 0) ?
  311. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  312. static_cast<TWinControl *>(PasswordOKButton)),
  313. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  314. if (Result)
  315. {
  316. for (int Index = 0; Index < Edits->Count; Index++)
  317. {
  318. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  319. Results->Strings[Index] = Edit->Text;
  320. Prompts->Objects[Index] = (TObject *)
  321. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  322. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  323. }
  324. if (SavePasswordCheck->Checked)
  325. {
  326. DebugAssert(Data != NULL);
  327. DebugAssert(Results->Count >= 1);
  328. FSessionData->Password = Results->Strings[0];
  329. Data->Password = Results->Strings[0];
  330. // modified only, explicit
  331. StoredSessions->Save(false, true);
  332. }
  333. }
  334. }
  335. __finally
  336. {
  337. delete Edits;
  338. }
  339. return Result;
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  343. bool & NeverShowAgain, int Options)
  344. {
  345. BannerMemo->Lines->Text = Banner;
  346. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  347. NeverShowAgainCheck->Checked = NeverShowAgain;
  348. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  349. BannerCloseButton, BannerCloseButton, false, true, false);
  350. if (Result)
  351. {
  352. NeverShowAgain = NeverShowAgainCheck->Checked;
  353. }
  354. }
  355. //---------------------------------------------------------------------------
  356. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  357. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  358. bool FixHeight, bool Zoom, bool ForceLog)
  359. {
  360. TModalResult DefaultButtonResult;
  361. TAlign Align = Panel->Align;
  362. try
  363. {
  364. // If not visible yet, the animation was not even initialized yet
  365. if (Visible)
  366. {
  367. FFrameAnimation.Stop();
  368. }
  369. DebugAssert(FStatus.IsEmpty());
  370. FStatus = Status;
  371. DefaultButton->Default = true;
  372. CancelButton->Cancel = true;
  373. DefaultButtonResult = DefaultResult(this);
  374. if (Zoom)
  375. {
  376. Panel->Align = alClient;
  377. }
  378. if (ForceLog || Visible)
  379. {
  380. if (ClientHeight < Panel->Height)
  381. {
  382. ClientHeight = Panel->Height;
  383. }
  384. // Panel being hidden gets not realigned automatically, even if it
  385. // has Align property set
  386. Panel->Top = ClientHeight - Panel->Height;
  387. Panel->Show();
  388. TCursor PrevCursor = Screen->Cursor;
  389. try
  390. {
  391. if (Zoom)
  392. {
  393. TopPanel->Hide();
  394. }
  395. else
  396. {
  397. if (LogView->Items->Count > 0)
  398. {
  399. // To avoid the scrolling effect when setting TopIndex
  400. LogView->Items->BeginUpdate();
  401. try
  402. {
  403. MakeLogItemVisible(LogView->Items->Count - 1);
  404. }
  405. __finally
  406. {
  407. LogView->Items->EndUpdate();
  408. RedrawLog();
  409. }
  410. }
  411. }
  412. Screen->Cursor = crDefault;
  413. if (!Visible)
  414. {
  415. DebugAssert(ForceLog);
  416. ShowAsModal();
  417. }
  418. ActiveControl = FocusControl;
  419. ModalResult = mrNone;
  420. AdjustControls();
  421. do
  422. {
  423. Application->HandleMessage();
  424. }
  425. while (!Application->Terminated && (ModalResult == mrNone));
  426. }
  427. __finally
  428. {
  429. Panel->Hide();
  430. Screen->Cursor = PrevCursor;
  431. if (Zoom)
  432. {
  433. TopPanel->Show();
  434. }
  435. Repaint();
  436. }
  437. }
  438. else
  439. {
  440. int PrevHeight = ClientHeight;
  441. int PrevMinHeight = Constraints->MinHeight;
  442. int PrevMaxHeight = Constraints->MaxHeight;
  443. try
  444. {
  445. Constraints->MinHeight = 0;
  446. ClientHeight = Panel->Height;
  447. if (FixHeight)
  448. {
  449. Constraints->MinHeight = Height;
  450. Constraints->MaxHeight = Height;
  451. }
  452. TopPanel->Hide();
  453. Panel->Show();
  454. FFocusControl = FocusControl;
  455. ShowModal();
  456. }
  457. __finally
  458. {
  459. FFocusControl = NULL;
  460. ClientHeight = PrevHeight;
  461. Constraints->MinHeight = PrevMinHeight;
  462. Constraints->MaxHeight = PrevMaxHeight;
  463. Panel->Hide();
  464. TopPanel->Show();
  465. }
  466. }
  467. }
  468. __finally
  469. {
  470. Panel->Align = Align;
  471. DefaultButton->Default = false;
  472. CancelButton->Cancel = false;
  473. FStatus = L"";
  474. AdjustControls();
  475. FFrameAnimation.Start();
  476. }
  477. bool Result = (ModalResult == DefaultButtonResult);
  478. if (!Result)
  479. {
  480. // This is not nice as it may ultimately route to
  481. // TTerminalThread::Cancel() and throw fatal exception,
  482. // what actually means that any PromptUser call during authentication never
  483. // return false and their fall back/alternative code never occurs.
  484. // It probably needs fixing.
  485. DoCancel();
  486. }
  487. return Result;
  488. }
  489. //---------------------------------------------------------------------------
  490. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  491. {
  492. FormHelp(this);
  493. }
  494. //---------------------------------------------------------------------------
  495. int __fastcall TAuthenticateForm::LogItemHeight(int Index)
  496. {
  497. UnicodeString S = LogView->Items->Strings[Index];
  498. TRect TextRect(0, 0, LogView->ClientWidth - (2 * FHorizontalLogPadding), 0);
  499. LogView->Canvas->Font = LogView->Font;
  500. LogView->Canvas->TextRect(TextRect, S, FLogTextFormat + (TTextFormat() << tfCalcRect));
  501. int Result = TextRect.Height() + (2 * FVerticalLogPadding);
  502. return Result;
  503. }
  504. //---------------------------------------------------------------------------
  505. void __fastcall TAuthenticateForm::LogViewMeasureItem(TWinControl * /*Control*/, int Index,
  506. int & Height)
  507. {
  508. Height = LogItemHeight(Index);
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TAuthenticateForm::LogViewDrawItem(TWinControl * /*Control*/, int Index,
  512. TRect & Rect, TOwnerDrawState /*State*/)
  513. {
  514. // Reset back to base colors. We do not want to render selection.
  515. // + At initial phases the canvas font will not yet reflect he desktop font.
  516. LogView->Canvas->Font = LogView->Font;
  517. LogView->Canvas->Brush->Color = LogView->Color;
  518. LogView->Canvas->FillRect(Rect);
  519. // tfVerticalCenter does not seem to center the text,
  520. // so we need to deflate the vertical size too
  521. Rect.Inflate(-FHorizontalLogPadding, -FVerticalLogPadding);
  522. UnicodeString S = LogView->Items->Strings[Index];
  523. LogView->Canvas->TextRect(Rect, S, FLogTextFormat);
  524. }
  525. //---------------------------------------------------------------------------
  526. void __fastcall TAuthenticateForm::RedrawLog()
  527. {
  528. // Redraw including the scrollbar (RDW_FRAME)
  529. RedrawWindow(LogView->Handle, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  533. {
  534. if (LogView->Showing)
  535. {
  536. // Mainly to avoid the scrolling effect when setting TopIndex
  537. LogView->Items->BeginUpdate();
  538. try
  539. {
  540. // Rebuild the list view to force Windows to resend WM_MEASUREITEM
  541. int ItemIndex = LogView->ItemIndex;
  542. int TopIndex = LogView->TopIndex;
  543. std::unique_ptr<TStringList> Items(new TStringList);
  544. Items->Assign(LogView->Items);
  545. LogView->Items->Assign(Items.get());
  546. LogView->ItemIndex = ItemIndex;
  547. LogView->TopIndex = TopIndex;
  548. }
  549. __finally
  550. {
  551. LogView->Items->EndUpdate();
  552. RedrawLog();
  553. }
  554. }
  555. }
  556. //---------------------------------------------------------------------------