1
0

Authenticate.cpp 19 KB

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