Skip to content

Commit aa73596

Browse files
committed
feat: export mysql_authentication_dialog_ask callback from loginform unit
1 parent c2acc8f commit aa73596

3 files changed

Lines changed: 68 additions & 63 deletions

File tree

‎heidisql.lpr‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
SysUtils, Dialogs,
1111
Forms, printer4lazarus, datetimectrls, LCLTranslator, Translations,
1212
{ you can add units after this }
13-
main, apphelpers, dbconnection, generic_types, crashdialog, FileInfo
13+
main, apphelpers, dbconnection, generic_types, crashdialog, FileInfo, loginform
1414
{$if defined(LINUX) and (defined(LCLQt5) or defined(LCLQt6))}
1515
, platformtheme
1616
{$endif};
1717

1818
{$R *.res}
1919

20+
// Export the callback from unit loginform, so that libmysqlclient's "dialog"
21+
// authentication plugin can find it in the executable's export table.
22+
// FPC only allows exports in the main program, not in units.
23+
exports
24+
mysql_authentication_dialog_ask;
25+
2026
var
2127
AppLanguage: String;
2228
WasDarkMode, IsDarkMode, FirstRun: Boolean;

‎source/dbconnection.pas‎

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,17 +1017,6 @@ TInterbaseQuery = class(TDBQuery)
10171017
procedure SQLite_CollationNeededCallback(userData:Pointer; ppDb:Psqlite3; eTextRep:integer; zName:PAnsiChar); cdecl;
10181018
function SQLite_Collation(userData: Pointer; lenA: Integer; strA: PAnsiChar; lenB: Integer; strB: PAnsiChar): Integer; cdecl;
10191019

1020-
function mysql_authentication_dialog_ask(
1021-
Handle: PMYSQL;
1022-
_type: Integer;
1023-
prompt: PAnsiChar;
1024-
buf: PAnsiChar;
1025-
buf_len: Integer
1026-
): PAnsiChar; cdecl;
1027-
1028-
{exports
1029-
mysql_authentication_dialog_ask;}
1030-
10311020

10321021
{$I const.inc}
10331022

@@ -12343,53 +12332,4 @@ function SQLite_Collation(userData: Pointer; lenA: Integer; strA: PAnsiChar; len
1234312332
end;
1234412333

1234512334

12346-
function mysql_authentication_dialog_ask;
12347-
var
12348-
Dialog: TfrmLogin;
12349-
begin
12350-
{
12351-
From client_plugin.h:
12352-
The C function with the name "mysql_authentication_dialog_ask", if exists,
12353-
will be used by the "dialog" client authentication plugin when user
12354-
input is needed. This function should be of mysql_authentication_dialog_ask_t
12355-
type. If the function does not exists, a built-in implementation will be
12356-
used.
12357-
@param mysql mysql
12358-
@param type type of the input
12359-
1 - normal string input
12360-
2 - password string
12361-
@param prompt prompt
12362-
@param buf a buffer to store the use input
12363-
@param buf_len the length of the buffer
12364-
@retval a pointer to the user input string.
12365-
It may be equal to 'buf' or to 'mysql->password'.
12366-
In all other cases it is assumed to be an allocated
12367-
string, and the "dialog" plugin will free() it.
12368-
Test suite:
12369-
INSTALL PLUGIN three_attempts SONAME 'dialog.dll';
12370-
CREATE USER test_dialog IDENTIFIED VIA three_attempts USING 'SECRET';
12371-
}
12372-
Dialog := TfrmLogin.Create(nil);
12373-
Dialog.lblPrompt.Caption := String(prompt);
12374-
Dialog.editUsername.Width := Dialog.editUsername.Width + (Dialog.editUsername.Left - Dialog.lblUsername.Left);
12375-
Dialog.editPassword.Width := Dialog.editUsername.Width;
12376-
Dialog.lblUsername.Visible := False;
12377-
Dialog.lblPassword.Visible := False;
12378-
Dialog.editUsername.Left := Dialog.lblUsername.Left;
12379-
Dialog.editPassword.Left := Dialog.lblPassword.Left;
12380-
Dialog.editUsername.Top := Dialog.lblPrompt.Top + Dialog.lblPrompt.Height + 15;
12381-
Dialog.editPassword.Top := Dialog.editUsername.Top;
12382-
Dialog.editUsername.Visible := _type=1;
12383-
Dialog.editPassword.Visible := _type=2;
12384-
Dialog.ShowModal;
12385-
case _type of
12386-
1: Result := PAnsiChar(AnsiString(Dialog.editUsername.Text));
12387-
2: Result := PAnsiChar(AnsiString(Dialog.editPassword.Text));
12388-
else raise EDbError.CreateFmt(_('Unsupported type (%d) in %s.'), [_type, 'mysql_authentication_dialog_ask']);
12389-
end;
12390-
Dialog.Free;
12391-
end;
12392-
12393-
12394-
1239512335
end.

‎source/loginform.pas‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface
66

77
uses
88
SysUtils, Variants, Classes, Graphics, Controls, Forms,
9-
Dialogs, StdCtrls, ExtCtrls;
9+
Dialogs, StdCtrls, ExtCtrls, dbstructures.mysql;
1010

1111
type
1212
TfrmLogin = class(TForm)
@@ -27,9 +27,20 @@ TfrmLogin = class(TForm)
2727
end;
2828

2929

30+
// Callback for libmysqlclient's "dialog" authentication plugin, exported from
31+
// heidisql.lpr. Shows this login form when the server asks for user input.
32+
function mysql_authentication_dialog_ask(
33+
Handle: PMYSQL;
34+
_type: Integer;
35+
prompt: PAnsiChar;
36+
buf: PAnsiChar;
37+
buf_len: Integer
38+
): PAnsiChar; cdecl;
39+
40+
3041
implementation
3142

32-
uses apphelpers, main;
43+
uses apphelpers, main, dbstructures;
3344

3445
{$R *.lfm}
3546
{$I const.inc}
@@ -54,4 +65,52 @@ procedure TfrmLogin.FormShow(Sender: TObject);
5465
editUsername.TrySetFocus;
5566
end;
5667

68+
69+
function mysql_authentication_dialog_ask;
70+
var
71+
Dialog: TfrmLogin;
72+
begin
73+
{
74+
From client_plugin.h:
75+
The C function with the name "mysql_authentication_dialog_ask", if exists,
76+
will be used by the "dialog" client authentication plugin when user
77+
input is needed. This function should be of mysql_authentication_dialog_ask_t
78+
type. If the function does not exists, a built-in implementation will be
79+
used.
80+
@param mysql mysql
81+
@param type type of the input
82+
1 - normal string input
83+
2 - password string
84+
@param prompt prompt
85+
@param buf a buffer to store the use input
86+
@param buf_len the length of the buffer
87+
@retval a pointer to the user input string.
88+
It may be equal to 'buf' or to 'mysql->password'.
89+
In all other cases it is assumed to be an allocated
90+
string, and the "dialog" plugin will free() it.
91+
Test suite:
92+
INSTALL PLUGIN three_attempts SONAME 'dialog.dll';
93+
CREATE USER test_dialog IDENTIFIED VIA three_attempts USING 'SECRET';
94+
}
95+
Dialog := TfrmLogin.Create(nil);
96+
Dialog.lblPrompt.Caption := String(prompt);
97+
Dialog.editUsername.Width := Dialog.editUsername.Width + (Dialog.editUsername.Left - Dialog.lblUsername.Left);
98+
Dialog.editPassword.Width := Dialog.editUsername.Width;
99+
Dialog.lblUsername.Visible := False;
100+
Dialog.lblPassword.Visible := False;
101+
Dialog.editUsername.Left := Dialog.lblUsername.Left;
102+
Dialog.editPassword.Left := Dialog.lblPassword.Left;
103+
Dialog.editUsername.Top := Dialog.lblPrompt.Top + Dialog.lblPrompt.Height + 15;
104+
Dialog.editPassword.Top := Dialog.editUsername.Top;
105+
Dialog.editUsername.Visible := _type=1;
106+
Dialog.editPassword.Visible := _type=2;
107+
Dialog.ShowModal;
108+
case _type of
109+
1: Result := PAnsiChar(AnsiString(Dialog.editUsername.Text));
110+
2: Result := PAnsiChar(AnsiString(Dialog.editPassword.Text));
111+
else raise EDbError.CreateFmt(_('Unsupported type (%d) in %s.'), [_type, 'mysql_authentication_dialog_ask']);
112+
end;
113+
Dialog.Free;
114+
end;
115+
57116
end.

0 commit comments

Comments
 (0)