Authenticate.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. return Result;
  170. }
  171. //---------------------------------------------------------------------------
  172. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo)
  173. {
  174. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  175. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  176. Result->Parent = FPromptParent;
  177. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  178. Result->Top = Current;
  179. Result->Left = FPromptLeft;
  180. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  181. return Result;
  182. }
  183. //---------------------------------------------------------------------------
  184. TList * __fastcall TAuthenticateForm::GeneratePrompt(UnicodeString Instructions,
  185. TStrings * Prompts)
  186. {
  187. while (FPromptParent->ControlCount > 0)
  188. {
  189. delete FPromptParent->Controls[0];
  190. }
  191. TList * Result = new TList;
  192. int Current = FPromptTop;
  193. if (!Instructions.IsEmpty())
  194. {
  195. TLabel * Label = GenerateLabel(Current, Instructions);
  196. Current += Label->Height + FPromptsGap;
  197. }
  198. for (int Index = 0; Index < Prompts->Count; Index++)
  199. {
  200. if (Index > 0)
  201. {
  202. Current += FPromptEditGap;
  203. }
  204. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  205. Current += Label->Height + FPromptEditGap;
  206. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  207. TCustomEdit * Edit = GenerateEdit(Current, Echo);
  208. Result->Add(Edit);
  209. Label->FocusControl = Edit;
  210. Current += Edit->Height;
  211. }
  212. FPromptParent->ClientHeight = Current;
  213. return Result;
  214. }
  215. //---------------------------------------------------------------------------
  216. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  217. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  218. bool StoredCredentialsTried)
  219. {
  220. bool Result;
  221. TList * Edits = GeneratePrompt(Instructions, Prompts);
  222. try
  223. {
  224. bool ShowSessionRememberPasswordPanel = false;
  225. bool ShowSavePasswordPanel = false;
  226. TSessionData * Data = NULL;
  227. if (IsPasswordPrompt(Kind, Prompts) && StoredCredentialsTried)
  228. {
  229. Data = StoredSessions->FindSame(FSessionData);
  230. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  231. }
  232. // do not offer to remember password,
  233. // if we are offering to save the password to stored site
  234. if (!ShowSavePasswordPanel &&
  235. (Prompts->Count == 1) &&
  236. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  237. ALWAYS_TRUE(IsPasswordOrPassphrasePrompt(Kind, Prompts)))
  238. {
  239. ShowSessionRememberPasswordPanel = true;
  240. }
  241. SavePasswordCheck->Checked = false;
  242. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  243. SessionRememberPasswordCheck->Checked = false;
  244. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  245. if (PasswordPanel->AutoSize)
  246. {
  247. PasswordPanel->AutoSize = false;
  248. PasswordPanel->AutoSize = true;
  249. }
  250. PasswordPanel->Realign();
  251. DebugAssert(Results->Count == Edits->Count);
  252. for (int Index = 0; Index < Edits->Count; Index++)
  253. {
  254. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  255. Edit->Text = Results->Strings[Index];
  256. }
  257. Result = Execute(Name, PasswordPanel,
  258. ((Edits->Count > 0) ?
  259. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  260. static_cast<TWinControl *>(PasswordOKButton)),
  261. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  262. if (Result)
  263. {
  264. for (int Index = 0; Index < Edits->Count; Index++)
  265. {
  266. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  267. Results->Strings[Index] = Edit->Text;
  268. Prompts->Objects[Index] = (TObject *)
  269. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  270. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  271. }
  272. if (SavePasswordCheck->Checked)
  273. {
  274. DebugAssert(Data != NULL);
  275. DebugAssert(Results->Count >= 1);
  276. FSessionData->Password = Results->Strings[0];
  277. Data->Password = Results->Strings[0];
  278. // modified only, explicit
  279. StoredSessions->Save(false, true);
  280. }
  281. }
  282. }
  283. __finally
  284. {
  285. delete Edits;
  286. }
  287. return Result;
  288. }
  289. //---------------------------------------------------------------------------
  290. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  291. bool & NeverShowAgain, int Options)
  292. {
  293. BannerMemo->Lines->Text = Banner;
  294. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  295. NeverShowAgainCheck->Checked = NeverShowAgain;
  296. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  297. BannerCloseButton, BannerCloseButton, false, true, false);
  298. if (Result)
  299. {
  300. NeverShowAgain = NeverShowAgainCheck->Checked;
  301. }
  302. }
  303. //---------------------------------------------------------------------------
  304. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  305. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  306. bool FixHeight, bool Zoom, bool ForceLog)
  307. {
  308. TModalResult DefaultButtonResult;
  309. TAlign Align = Panel->Align;
  310. try
  311. {
  312. // If not visible yet, the animation was not even initialized yet
  313. if (Visible)
  314. {
  315. FFrameAnimation.Stop();
  316. }
  317. DebugAssert(FStatus.IsEmpty());
  318. FStatus = Status;
  319. DefaultButton->Default = true;
  320. CancelButton->Cancel = true;
  321. DefaultButtonResult = DefaultResult(this);
  322. if (Zoom)
  323. {
  324. Panel->Align = alClient;
  325. }
  326. if (ForceLog || Visible)
  327. {
  328. if (ClientHeight < Panel->Height)
  329. {
  330. ClientHeight = Panel->Height;
  331. }
  332. // Panel being hidden gets not realigned automatically, even if it
  333. // has Align property set
  334. Panel->Top = ClientHeight - Panel->Height;
  335. Panel->Show();
  336. TCursor PrevCursor = Screen->Cursor;
  337. try
  338. {
  339. if (Zoom)
  340. {
  341. TopPanel->Hide();
  342. }
  343. else
  344. {
  345. if (LogView->Items->Count > 0)
  346. {
  347. TListItem * Item = LogView->ItemFocused;
  348. if (Item == NULL)
  349. {
  350. Item = LogView->Items->Item[LogView->Items->Count - 1];
  351. }
  352. Item->MakeVisible(false);
  353. }
  354. }
  355. Screen->Cursor = crDefault;
  356. if (!Visible)
  357. {
  358. DebugAssert(ForceLog);
  359. ShowAsModal();
  360. }
  361. ActiveControl = FocusControl;
  362. ModalResult = mrNone;
  363. AdjustControls();
  364. do
  365. {
  366. Application->HandleMessage();
  367. }
  368. while (!Application->Terminated && (ModalResult == mrNone));
  369. }
  370. __finally
  371. {
  372. Panel->Hide();
  373. Screen->Cursor = PrevCursor;
  374. if (Zoom)
  375. {
  376. TopPanel->Show();
  377. }
  378. Repaint();
  379. }
  380. }
  381. else
  382. {
  383. int PrevHeight = ClientHeight;
  384. int PrevMinHeight = Constraints->MinHeight;
  385. int PrevMaxHeight = Constraints->MaxHeight;
  386. try
  387. {
  388. Constraints->MinHeight = 0;
  389. ClientHeight = Panel->Height;
  390. if (FixHeight)
  391. {
  392. Constraints->MinHeight = Height;
  393. Constraints->MaxHeight = Height;
  394. }
  395. TopPanel->Hide();
  396. Panel->Show();
  397. FFocusControl = FocusControl;
  398. ShowModal();
  399. }
  400. __finally
  401. {
  402. FFocusControl = NULL;
  403. ClientHeight = PrevHeight;
  404. Constraints->MinHeight = PrevMinHeight;
  405. Constraints->MaxHeight = PrevMaxHeight;
  406. Panel->Hide();
  407. TopPanel->Show();
  408. }
  409. }
  410. }
  411. __finally
  412. {
  413. Panel->Align = Align;
  414. DefaultButton->Default = false;
  415. CancelButton->Cancel = false;
  416. FStatus = L"";
  417. AdjustControls();
  418. FFrameAnimation.Start();
  419. }
  420. bool Result = (ModalResult == DefaultButtonResult);
  421. if (!Result)
  422. {
  423. // This is not nice as it may ultimately route to
  424. // TTerminalThread::Cancel() and throw fatal exception,
  425. // what actually means that any PromptUser call during authentication never
  426. // return false and their fall back/alternative code never occurs.
  427. // It probably needs fixing.
  428. DoCancel();
  429. }
  430. return Result;
  431. }
  432. //---------------------------------------------------------------------------
  433. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  434. {
  435. FormHelp(this);
  436. }
  437. //---------------------------------------------------------------------------
  438. void __fastcall TAuthenticateForm::AdjustLogView()
  439. {
  440. ListView_SetColumnWidth(LogView->Handle, 0, LVSCW_AUTOSIZE);
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  444. {
  445. AdjustLogView();
  446. }
  447. //---------------------------------------------------------------------------