Authenticate.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. ClearLog();
  41. }
  42. //---------------------------------------------------------------------------
  43. __fastcall TAuthenticateForm::~TAuthenticateForm()
  44. {
  45. ReleaseAsModal(this, FShowAsModalStorage);
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TAuthenticateForm::ShowAsModal()
  49. {
  50. ::ShowAsModal(this, FShowAsModalStorage);
  51. }
  52. //---------------------------------------------------------------------------
  53. void __fastcall TAuthenticateForm::HideAsModal()
  54. {
  55. ::HideAsModal(this, FShowAsModalStorage);
  56. }
  57. //---------------------------------------------------------------------------
  58. void __fastcall TAuthenticateForm::WMNCCreate(TWMNCCreate & Message)
  59. {
  60. // bypass TForm::WMNCCreate to prevent disabling "resize"
  61. // (which is done for bsDialog, see comments in CreateParams)
  62. DefaultHandler(&Message);
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TAuthenticateForm::DoCancel()
  66. {
  67. if (FOnCancel != NULL)
  68. {
  69. FOnCancel(this);
  70. }
  71. }
  72. //---------------------------------------------------------------------------
  73. void __fastcall TAuthenticateForm::Dispatch(void * AMessage)
  74. {
  75. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  76. if (Message.Msg == WM_NCCREATE)
  77. {
  78. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  79. }
  80. else if (Message.Msg == WM_CLOSE)
  81. {
  82. DoCancel();
  83. TForm::Dispatch(AMessage);
  84. }
  85. else if (Message.Msg == WM_MANAGES_CAPTION)
  86. {
  87. // caption managed in TAuthenticateForm::AdjustControls()
  88. Message.Result = 1;
  89. }
  90. else
  91. {
  92. TForm::Dispatch(AMessage);
  93. }
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TAuthenticateForm::CreateParams(TCreateParams & Params)
  97. {
  98. TForm::CreateParams(Params);
  99. // Allow resizing of the window, even if this is bsDialog.
  100. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  101. // reason receive focus, if window is shown atop non-main window
  102. // (like editor)
  103. Params.Style = Params.Style | WS_THICKFRAME;
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TAuthenticateForm::FormShow(TObject * /*Sender*/)
  107. {
  108. AdjustControls();
  109. if (FFocusControl != NULL)
  110. {
  111. ActiveControl = FFocusControl;
  112. }
  113. UnicodeString AnimationName = FSessionData->IsSecure() ? L"ConnectingSecure" : L"ConnectingInsecure";
  114. FFrameAnimation.Init(AnimationPaintBox, AnimationName);
  115. FFrameAnimation.Start();
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall TAuthenticateForm::ClearLog()
  119. {
  120. // TListItems::Clear() does nothing without allocated handle
  121. LogView->HandleNeeded();
  122. LogView->Items->Clear();
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TAuthenticateForm::Log(const UnicodeString Message)
  126. {
  127. // HACK
  128. // The first call to Repaint from TFrameAnimation happens
  129. // even before the form is showing. After it is shown it takes sometime too long
  130. // before the animation is painted, so that the form is closed before the first frame even appers
  131. if (!FAnimationPainted && Showing)
  132. {
  133. AnimationPaintBox->Repaint();
  134. FAnimationPainted = true;
  135. }
  136. TListItem * Item = LogView->Items->Add();
  137. Item->Caption = Message;
  138. Item->MakeVisible(false);
  139. AdjustLogView();
  140. LogView->Repaint();
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TAuthenticateForm::AdjustControls()
  144. {
  145. UnicodeString ACaption;
  146. if (FStatus.IsEmpty())
  147. {
  148. ACaption = FSessionData->SessionName;
  149. }
  150. else
  151. {
  152. ACaption = FORMAT(L"%s - %s", (FStatus, FSessionData->SessionName));
  153. }
  154. Caption = FormatFormCaption(this, ACaption);
  155. }
  156. //---------------------------------------------------------------------------
  157. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  158. {
  159. TLabel * Result = new TLabel(this);
  160. Result->Parent = FPromptParent;
  161. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  162. Result->WordWrap = true;
  163. Result->AutoSize = false;
  164. Result->Top = Current;
  165. Result->Left = FPromptLeft;
  166. Result->Caption = Caption;
  167. int Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  168. Result->Width = Width;
  169. Result->AutoSize = true;
  170. return Result;
  171. }
  172. //---------------------------------------------------------------------------
  173. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo)
  174. {
  175. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  176. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  177. Result->Parent = FPromptParent;
  178. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  179. Result->Top = Current;
  180. Result->Left = FPromptLeft;
  181. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  182. return Result;
  183. }
  184. //---------------------------------------------------------------------------
  185. TList * __fastcall TAuthenticateForm::GeneratePrompt(UnicodeString Instructions,
  186. TStrings * Prompts)
  187. {
  188. while (FPromptParent->ControlCount > 0)
  189. {
  190. delete FPromptParent->Controls[0];
  191. }
  192. TList * Result = new TList;
  193. int Current = FPromptTop;
  194. if (!Instructions.IsEmpty())
  195. {
  196. TLabel * Label = GenerateLabel(Current, Instructions);
  197. Current += Label->Height + FPromptsGap;
  198. }
  199. for (int Index = 0; Index < Prompts->Count; Index++)
  200. {
  201. if (Index > 0)
  202. {
  203. Current += FPromptEditGap;
  204. }
  205. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  206. Current += Label->Height + FPromptEditGap;
  207. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  208. TCustomEdit * Edit = GenerateEdit(Current, Echo);
  209. Result->Add(Edit);
  210. Label->FocusControl = Edit;
  211. Current += Edit->Height;
  212. }
  213. FPromptParent->ClientHeight = Current;
  214. return Result;
  215. }
  216. //---------------------------------------------------------------------------
  217. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  218. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  219. bool StoredCredentialsTried)
  220. {
  221. bool Result;
  222. TList * Edits = GeneratePrompt(Instructions, Prompts);
  223. try
  224. {
  225. bool ShowSessionRememberPasswordPanel = false;
  226. bool ShowSavePasswordPanel = false;
  227. TSessionData * Data = NULL;
  228. if (IsPasswordPrompt(Kind, Prompts) && StoredCredentialsTried)
  229. {
  230. Data = StoredSessions->FindSame(FSessionData);
  231. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  232. }
  233. // do not offer to remember password,
  234. // if we are offering to save the password to stored site
  235. if (!ShowSavePasswordPanel &&
  236. (Prompts->Count == 1) &&
  237. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  238. ALWAYS_TRUE(IsPasswordOrPassphrasePrompt(Kind, Prompts)))
  239. {
  240. ShowSessionRememberPasswordPanel = true;
  241. }
  242. SavePasswordCheck->Checked = false;
  243. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  244. SessionRememberPasswordCheck->Checked = false;
  245. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  246. if (PasswordPanel->AutoSize)
  247. {
  248. PasswordPanel->AutoSize = false;
  249. PasswordPanel->AutoSize = true;
  250. }
  251. PasswordPanel->Realign();
  252. DebugAssert(Results->Count == Edits->Count);
  253. for (int Index = 0; Index < Edits->Count; Index++)
  254. {
  255. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  256. Edit->Text = Results->Strings[Index];
  257. }
  258. Result = Execute(Name, PasswordPanel,
  259. ((Edits->Count > 0) ?
  260. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  261. static_cast<TWinControl *>(PasswordOKButton)),
  262. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  263. if (Result)
  264. {
  265. for (int Index = 0; Index < Edits->Count; Index++)
  266. {
  267. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  268. Results->Strings[Index] = Edit->Text;
  269. Prompts->Objects[Index] = (TObject *)
  270. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  271. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  272. }
  273. if (SavePasswordCheck->Checked)
  274. {
  275. DebugAssert(Data != NULL);
  276. DebugAssert(Results->Count >= 1);
  277. FSessionData->Password = Results->Strings[0];
  278. Data->Password = Results->Strings[0];
  279. // modified only, explicit
  280. StoredSessions->Save(false, true);
  281. }
  282. }
  283. }
  284. __finally
  285. {
  286. delete Edits;
  287. }
  288. return Result;
  289. }
  290. //---------------------------------------------------------------------------
  291. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  292. bool & NeverShowAgain, int Options)
  293. {
  294. BannerMemo->Lines->Text = Banner;
  295. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  296. NeverShowAgainCheck->Checked = NeverShowAgain;
  297. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  298. BannerCloseButton, BannerCloseButton, false, true, false);
  299. if (Result)
  300. {
  301. NeverShowAgain = NeverShowAgainCheck->Checked;
  302. }
  303. }
  304. //---------------------------------------------------------------------------
  305. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  306. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  307. bool FixHeight, bool Zoom, bool ForceLog)
  308. {
  309. TModalResult DefaultButtonResult;
  310. TAlign Align = Panel->Align;
  311. try
  312. {
  313. // If not visible yet, the animation was not even initialized yet
  314. if (Visible)
  315. {
  316. FFrameAnimation.Stop();
  317. }
  318. DebugAssert(FStatus.IsEmpty());
  319. FStatus = Status;
  320. DefaultButton->Default = true;
  321. CancelButton->Cancel = true;
  322. DefaultButtonResult = DefaultResult(this);
  323. if (Zoom)
  324. {
  325. Panel->Align = alClient;
  326. }
  327. if (ForceLog || Visible)
  328. {
  329. if (ClientHeight < Panel->Height)
  330. {
  331. ClientHeight = Panel->Height;
  332. }
  333. // Panel being hidden gets not realigned automatically, even if it
  334. // has Align property set
  335. Panel->Top = ClientHeight - Panel->Height;
  336. Panel->Show();
  337. TCursor PrevCursor = Screen->Cursor;
  338. try
  339. {
  340. if (Zoom)
  341. {
  342. TopPanel->Hide();
  343. }
  344. else
  345. {
  346. if (LogView->Items->Count > 0)
  347. {
  348. TListItem * Item = LogView->ItemFocused;
  349. if (Item == NULL)
  350. {
  351. Item = LogView->Items->Item[LogView->Items->Count - 1];
  352. }
  353. Item->MakeVisible(false);
  354. }
  355. }
  356. Screen->Cursor = crDefault;
  357. if (!Visible)
  358. {
  359. DebugAssert(ForceLog);
  360. ShowAsModal();
  361. }
  362. ActiveControl = FocusControl;
  363. ModalResult = mrNone;
  364. AdjustControls();
  365. do
  366. {
  367. Application->HandleMessage();
  368. }
  369. while (!Application->Terminated && (ModalResult == mrNone));
  370. }
  371. __finally
  372. {
  373. Panel->Hide();
  374. Screen->Cursor = PrevCursor;
  375. if (Zoom)
  376. {
  377. TopPanel->Show();
  378. }
  379. Repaint();
  380. }
  381. }
  382. else
  383. {
  384. int PrevHeight = ClientHeight;
  385. int PrevMinHeight = Constraints->MinHeight;
  386. int PrevMaxHeight = Constraints->MaxHeight;
  387. try
  388. {
  389. Constraints->MinHeight = 0;
  390. ClientHeight = Panel->Height;
  391. if (FixHeight)
  392. {
  393. Constraints->MinHeight = Height;
  394. Constraints->MaxHeight = Height;
  395. }
  396. TopPanel->Hide();
  397. Panel->Show();
  398. FFocusControl = FocusControl;
  399. ShowModal();
  400. }
  401. __finally
  402. {
  403. FFocusControl = NULL;
  404. ClientHeight = PrevHeight;
  405. Constraints->MinHeight = PrevMinHeight;
  406. Constraints->MaxHeight = PrevMaxHeight;
  407. Panel->Hide();
  408. TopPanel->Show();
  409. }
  410. }
  411. }
  412. __finally
  413. {
  414. Panel->Align = Align;
  415. DefaultButton->Default = false;
  416. CancelButton->Cancel = false;
  417. FStatus = L"";
  418. AdjustControls();
  419. FFrameAnimation.Start();
  420. }
  421. bool Result = (ModalResult == DefaultButtonResult);
  422. if (!Result)
  423. {
  424. // This is not nice as it may ultimately route to
  425. // TTerminalThread::Cancel() and throw fatal exception,
  426. // what actually means that any PromptUser call during authentication never
  427. // return false and their fall back/alternative code never occurs.
  428. // It probably needs fixing.
  429. DoCancel();
  430. }
  431. return Result;
  432. }
  433. //---------------------------------------------------------------------------
  434. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  435. {
  436. FormHelp(this);
  437. }
  438. //---------------------------------------------------------------------------
  439. void __fastcall TAuthenticateForm::AdjustLogView()
  440. {
  441. ListView_SetColumnWidth(LogView->Handle, 0, LVSCW_AUTOSIZE);
  442. }
  443. //---------------------------------------------------------------------------
  444. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  445. {
  446. AdjustLogView();
  447. }
  448. //---------------------------------------------------------------------------