Authenticate.cpp 14 KB

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