Authenticate.cpp 18 KB

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