Authenticate.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. if (Index < LogView->TopIndex)
  147. {
  148. LogView->TopIndex = Index;
  149. }
  150. else
  151. {
  152. int TotalHeight = 0;
  153. int Index2 = Index;
  154. while ((Index2 >= 0) && TotalHeight < LogView->ClientHeight)
  155. {
  156. TotalHeight += LogItemHeight(Index2);
  157. Index2--;
  158. }
  159. // Index2 is the last item above the first fully visible,
  160. // were the Index on the very bottom.
  161. if (LogView->TopIndex <= Index2 + 1)
  162. {
  163. LogView->TopIndex = Index2 + 2;
  164. }
  165. }
  166. }
  167. //---------------------------------------------------------------------------
  168. void __fastcall TAuthenticateForm::AdjustControls()
  169. {
  170. UnicodeString ACaption;
  171. if (FStatus.IsEmpty())
  172. {
  173. ACaption = FSessionData->SessionName;
  174. }
  175. else
  176. {
  177. ACaption = FORMAT(L"%s - %s", (FStatus, FSessionData->SessionName));
  178. }
  179. Caption = FormatFormCaption(this, ACaption);
  180. }
  181. //---------------------------------------------------------------------------
  182. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  183. {
  184. TLabel * Result = new TLabel(this);
  185. Result->Parent = FPromptParent;
  186. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  187. Result->WordWrap = true;
  188. Result->AutoSize = false;
  189. Result->Top = Current;
  190. Result->Left = FPromptLeft;
  191. Result->Caption = Caption;
  192. int Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  193. Result->Width = Width;
  194. return Result;
  195. }
  196. //---------------------------------------------------------------------------
  197. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo)
  198. {
  199. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  200. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  201. Result->Parent = FPromptParent;
  202. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  203. Result->Top = Current;
  204. Result->Left = FPromptLeft;
  205. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  206. return Result;
  207. }
  208. //---------------------------------------------------------------------------
  209. TList * __fastcall TAuthenticateForm::GeneratePrompt(UnicodeString Instructions,
  210. TStrings * Prompts)
  211. {
  212. while (FPromptParent->ControlCount > 0)
  213. {
  214. delete FPromptParent->Controls[0];
  215. }
  216. TList * Result = new TList;
  217. int Current = FPromptTop;
  218. if (!Instructions.IsEmpty())
  219. {
  220. TLabel * Label = GenerateLabel(Current, Instructions);
  221. Current += Label->Height + FPromptsGap;
  222. }
  223. for (int Index = 0; Index < Prompts->Count; Index++)
  224. {
  225. if (Index > 0)
  226. {
  227. Current += FPromptEditGap;
  228. }
  229. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  230. Current += Label->Height + FPromptEditGap;
  231. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  232. TCustomEdit * Edit = GenerateEdit(Current, Echo);
  233. Result->Add(Edit);
  234. Label->FocusControl = Edit;
  235. Current += Edit->Height;
  236. }
  237. FPromptParent->ClientHeight = Current;
  238. return Result;
  239. }
  240. //---------------------------------------------------------------------------
  241. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  242. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  243. bool StoredCredentialsTried)
  244. {
  245. bool Result;
  246. TList * Edits = GeneratePrompt(Instructions, Prompts);
  247. try
  248. {
  249. bool ShowSessionRememberPasswordPanel = false;
  250. bool ShowSavePasswordPanel = false;
  251. TSessionData * Data = NULL;
  252. if (IsPasswordPrompt(Kind, Prompts) && StoredCredentialsTried)
  253. {
  254. Data = StoredSessions->FindSame(FSessionData);
  255. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  256. }
  257. // do not offer to remember password,
  258. // if we are offering to save the password to stored site
  259. if (!ShowSavePasswordPanel &&
  260. (Prompts->Count == 1) &&
  261. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  262. DebugAlwaysTrue(IsPasswordOrPassphrasePrompt(Kind, Prompts)))
  263. {
  264. ShowSessionRememberPasswordPanel = true;
  265. }
  266. SavePasswordCheck->Checked = false;
  267. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  268. SessionRememberPasswordCheck->Checked = false;
  269. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  270. if (PasswordPanel->AutoSize)
  271. {
  272. PasswordPanel->AutoSize = false;
  273. PasswordPanel->AutoSize = true;
  274. }
  275. PasswordPanel->Realign();
  276. DebugAssert(Results->Count == Edits->Count);
  277. for (int Index = 0; Index < Edits->Count; Index++)
  278. {
  279. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  280. Edit->Text = Results->Strings[Index];
  281. }
  282. Result = Execute(Name, PasswordPanel,
  283. ((Edits->Count > 0) ?
  284. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  285. static_cast<TWinControl *>(PasswordOKButton)),
  286. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  287. if (Result)
  288. {
  289. for (int Index = 0; Index < Edits->Count; Index++)
  290. {
  291. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  292. Results->Strings[Index] = Edit->Text;
  293. Prompts->Objects[Index] = (TObject *)
  294. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  295. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  296. }
  297. if (SavePasswordCheck->Checked)
  298. {
  299. DebugAssert(Data != NULL);
  300. DebugAssert(Results->Count >= 1);
  301. FSessionData->Password = Results->Strings[0];
  302. Data->Password = Results->Strings[0];
  303. // modified only, explicit
  304. StoredSessions->Save(false, true);
  305. }
  306. }
  307. }
  308. __finally
  309. {
  310. delete Edits;
  311. }
  312. return Result;
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  316. bool & NeverShowAgain, int Options)
  317. {
  318. BannerMemo->Lines->Text = Banner;
  319. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  320. NeverShowAgainCheck->Checked = NeverShowAgain;
  321. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  322. BannerCloseButton, BannerCloseButton, false, true, false);
  323. if (Result)
  324. {
  325. NeverShowAgain = NeverShowAgainCheck->Checked;
  326. }
  327. }
  328. //---------------------------------------------------------------------------
  329. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  330. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  331. bool FixHeight, bool Zoom, bool ForceLog)
  332. {
  333. TModalResult DefaultButtonResult;
  334. TAlign Align = Panel->Align;
  335. try
  336. {
  337. // If not visible yet, the animation was not even initialized yet
  338. if (Visible)
  339. {
  340. FFrameAnimation.Stop();
  341. }
  342. DebugAssert(FStatus.IsEmpty());
  343. FStatus = Status;
  344. DefaultButton->Default = true;
  345. CancelButton->Cancel = true;
  346. DefaultButtonResult = DefaultResult(this);
  347. if (Zoom)
  348. {
  349. Panel->Align = alClient;
  350. }
  351. if (ForceLog || Visible)
  352. {
  353. if (ClientHeight < Panel->Height)
  354. {
  355. ClientHeight = Panel->Height;
  356. }
  357. // Panel being hidden gets not realigned automatically, even if it
  358. // has Align property set
  359. Panel->Top = ClientHeight - Panel->Height;
  360. Panel->Show();
  361. TCursor PrevCursor = Screen->Cursor;
  362. try
  363. {
  364. if (Zoom)
  365. {
  366. TopPanel->Hide();
  367. }
  368. else
  369. {
  370. if (LogView->Items->Count > 0)
  371. {
  372. // To avoid the scrolling effect when setting TopIndex
  373. LogView->Items->BeginUpdate();
  374. try
  375. {
  376. MakeLogItemVisible(LogView->Items->Count - 1);
  377. }
  378. __finally
  379. {
  380. LogView->Items->EndUpdate();
  381. RedrawLog();
  382. }
  383. }
  384. }
  385. Screen->Cursor = crDefault;
  386. if (!Visible)
  387. {
  388. DebugAssert(ForceLog);
  389. ShowAsModal();
  390. }
  391. ActiveControl = FocusControl;
  392. ModalResult = mrNone;
  393. AdjustControls();
  394. do
  395. {
  396. Application->HandleMessage();
  397. }
  398. while (!Application->Terminated && (ModalResult == mrNone));
  399. }
  400. __finally
  401. {
  402. Panel->Hide();
  403. Screen->Cursor = PrevCursor;
  404. if (Zoom)
  405. {
  406. TopPanel->Show();
  407. }
  408. Repaint();
  409. }
  410. }
  411. else
  412. {
  413. int PrevHeight = ClientHeight;
  414. int PrevMinHeight = Constraints->MinHeight;
  415. int PrevMaxHeight = Constraints->MaxHeight;
  416. try
  417. {
  418. Constraints->MinHeight = 0;
  419. ClientHeight = Panel->Height;
  420. if (FixHeight)
  421. {
  422. Constraints->MinHeight = Height;
  423. Constraints->MaxHeight = Height;
  424. }
  425. TopPanel->Hide();
  426. Panel->Show();
  427. FFocusControl = FocusControl;
  428. ShowModal();
  429. }
  430. __finally
  431. {
  432. FFocusControl = NULL;
  433. ClientHeight = PrevHeight;
  434. Constraints->MinHeight = PrevMinHeight;
  435. Constraints->MaxHeight = PrevMaxHeight;
  436. Panel->Hide();
  437. TopPanel->Show();
  438. }
  439. }
  440. }
  441. __finally
  442. {
  443. Panel->Align = Align;
  444. DefaultButton->Default = false;
  445. CancelButton->Cancel = false;
  446. FStatus = L"";
  447. AdjustControls();
  448. FFrameAnimation.Start();
  449. }
  450. bool Result = (ModalResult == DefaultButtonResult);
  451. if (!Result)
  452. {
  453. // This is not nice as it may ultimately route to
  454. // TTerminalThread::Cancel() and throw fatal exception,
  455. // what actually means that any PromptUser call during authentication never
  456. // return false and their fall back/alternative code never occurs.
  457. // It probably needs fixing.
  458. DoCancel();
  459. }
  460. return Result;
  461. }
  462. //---------------------------------------------------------------------------
  463. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  464. {
  465. FormHelp(this);
  466. }
  467. //---------------------------------------------------------------------------
  468. int __fastcall TAuthenticateForm::LogItemHeight(int Index)
  469. {
  470. UnicodeString S = LogView->Items->Strings[Index];
  471. TRect TextRect(0, 0, LogView->ClientWidth - (2 * FHorizontalLogPadding), 0);
  472. LogView->Canvas->Font = LogView->Font;
  473. LogView->Canvas->TextRect(TextRect, S, FLogTextFormat + (TTextFormat() << tfCalcRect));
  474. int Result = TextRect.Height() + (2 * FVerticalLogPadding);
  475. return Result;
  476. }
  477. //---------------------------------------------------------------------------
  478. void __fastcall TAuthenticateForm::LogViewMeasureItem(TWinControl * /*Control*/, int Index,
  479. int & Height)
  480. {
  481. Height = LogItemHeight(Index);
  482. }
  483. //---------------------------------------------------------------------------
  484. void __fastcall TAuthenticateForm::LogViewDrawItem(TWinControl * /*Control*/, int Index,
  485. TRect & Rect, TOwnerDrawState /*State*/)
  486. {
  487. // Reset back to base colors. We do not want to render selection.
  488. // + At initial phases the canvas font will not yet reflect he desktop font.
  489. LogView->Canvas->Font = LogView->Font;
  490. LogView->Canvas->Brush->Color = LogView->Color;
  491. LogView->Canvas->FillRect(Rect);
  492. // tfVerticalCenter does not seem to center the text,
  493. // so we need to deflate the vertical size too
  494. Rect.Inflate(-FHorizontalLogPadding, -FVerticalLogPadding);
  495. UnicodeString S = LogView->Items->Strings[Index];
  496. LogView->Canvas->TextRect(Rect, S, FLogTextFormat);
  497. }
  498. //---------------------------------------------------------------------------
  499. void __fastcall TAuthenticateForm::RedrawLog()
  500. {
  501. // Redraw including the scrollbar (RDW_FRAME)
  502. RedrawWindow(LogView->Handle, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
  503. }
  504. //---------------------------------------------------------------------------
  505. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  506. {
  507. if (LogView->Showing)
  508. {
  509. // Mainly to avoid the scrolling effect when setting TopIndex
  510. LogView->Items->BeginUpdate();
  511. try
  512. {
  513. // Rebuild the list view to force Windows to resend WM_MEASUREITEM
  514. int ItemIndex = LogView->ItemIndex;
  515. int TopIndex = LogView->TopIndex;
  516. std::unique_ptr<TStringList> Items(new TStringList);
  517. Items->Assign(LogView->Items);
  518. LogView->Items->Assign(Items.get());
  519. LogView->ItemIndex = ItemIndex;
  520. LogView->TopIndex = TopIndex;
  521. }
  522. __finally
  523. {
  524. LogView->Items->EndUpdate();
  525. RedrawLog();
  526. }
  527. }
  528. }
  529. //---------------------------------------------------------------------------