Authenticate.cpp 18 KB

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