Skip to content

Commit 4b31e6d

Browse files
committed
Address review comments, add unit test
1 parent c1bf5ba commit 4b31e6d

File tree

5 files changed

+55
-69
lines changed

5 files changed

+55
-69
lines changed

mysql-test/r/auth_named_pipe.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
INSTALL SONAME 'auth_named_pipe';
2+
CREATE USER USERNAME IDENTIFIED WITH named_pipe;
3+
SELECT USER(),CURRENT_USER();
4+
USER() CURRENT_USER()
5+
USERNAME@localhost USERNAME@%
6+
DROP USER USERNAME;
7+
CREATE USER nosuchuser IDENTIFIED WITH named_pipe;
8+
ERROR 28000: Access denied for user 'nosuchuser'@'localhost'
9+
DROP USER nosuchuser;
10+
UNINSTALL SONAME 'auth_named_pipe';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--loose-enable-named-pipe

mysql-test/t/auth_named_pipe.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--source include/windows.inc
2+
3+
INSTALL SONAME 'auth_named_pipe';
4+
5+
--replace_result $USERNAME USERNAME
6+
eval CREATE USER $USERNAME IDENTIFIED WITH named_pipe;
7+
# Connect using named pipe, correct username
8+
connect(pipe_con,localhost,$USERNAME,,,,,PIPE);
9+
--replace_result $USERNAME USERNAME
10+
SELECT USER(),CURRENT_USER();
11+
disconnect pipe_con;
12+
connection default;
13+
--replace_result $USERNAME USERNAME
14+
eval DROP USER $USERNAME;
15+
16+
# test invalid user name
17+
CREATE USER nosuchuser IDENTIFIED WITH named_pipe;
18+
--disable_query_log
19+
--error ER_ACCESS_DENIED_NO_PASSWORD_ERROR
20+
connect(pipe_con,localhost,nosuchuser,,,,,PIPE);
21+
--enable_query_log
22+
DROP USER nosuchuser;
23+
UNINSTALL SONAME 'auth_named_pipe';

plugin/auth_pipe/CMakeLists.txt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2-
#
3-
# This program is free software; you can redistribute it and/or
4-
# modify it under the terms of the GNU General Public License as
5-
# published by the Free Software Foundation; version 2 of the
6-
# License.
7-
#
8-
# This program is distributed in the hope that it will be useful,
9-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11-
# GNU General Public License for more details.
12-
#
13-
# You should have received a copy of the GNU General Public License
14-
# along with this program; if not, write to the Free Software
15-
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16-
171
IF(WIN32)
18-
MYSQL_ADD_PLUGIN(auth_pipe auth_pipe.c MODULE_ONLY)
2+
MYSQL_ADD_PLUGIN(auth_named_pipe auth_pipe.c)
193
ENDIF()

plugin/auth_pipe/auth_pipe.c

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,86 +17,54 @@
1717
/**
1818
@file
1919
20-
auth_pipd authentication plugin.
20+
auth_pipe authentication plugin.
2121
22-
Authentication is successful if the connection is done via a named pip and
23-
the owner of the client process matches the user name that was used when
24-
connecting to mysqld.
22+
Authentication is successful if the connection is done via a named pipe
23+
pipe peer name matches mysql user name
2524
*/
2625

27-
2826
#include <mysql/plugin_auth.h>
2927
#include <string.h>
3028
#include <lmcons.h>
3129

3230

33-
34-
35-
3631
/**
37-
perform the named pipe´based authentication
38-
39-
This authentication callback performs a named pipe based authentication -
40-
it gets the uid of the client process and considers the user authenticated
41-
if it uses username of this uid. That is - if the user is already
42-
authenticated to the OS (if she is logged in) - she can use MySQL as herself
32+
This authentication callback obtains user name using named pipe impersonation
4333
*/
44-
4534
static int pipe_auth(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
4635
{
4736
unsigned char *pkt;
48-
PTOKEN_USER pTokenUser= NULL;
49-
HANDLE hToken;
5037
MYSQL_PLUGIN_VIO_INFO vio_info;
51-
DWORD dLength= 0;
52-
int Ret= CR_ERROR;
53-
TCHAR username[UNLEN + 1];
54-
DWORD username_length= UNLEN + 1;
55-
char domainname[DNLEN + 1];
56-
DWORD domainsize=DNLEN + 1;
57-
SID_NAME_USE sidnameuse;
38+
char username[UNLEN + 1];
39+
size_t username_length;
40+
int ret;
5841

5942
/* no user name yet ? read the client handshake packet with the user name */
6043
if (info->user_name == 0)
6144
{
6245
if (vio->read_packet(vio, &pkt) < 0)
6346
return CR_ERROR;
6447
}
65-
6648
info->password_used= PASSWORD_USED_NO_MENTION;
67-
6849
vio->info(vio, &vio_info);
6950
if (vio_info.protocol != MYSQL_VIO_PIPE)
7051
return CR_ERROR;
7152

72-
/* get the UID of the client process */
53+
/* Impersonate the named pipe peer, and retrieve the user name */
7354
if (!ImpersonateNamedPipeClient(vio_info.handle))
7455
return CR_ERROR;
75-
76-
if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &hToken))
77-
goto end;
78-
79-
/* determine length of TokenUser */
80-
GetTokenInformation(hToken, TokenUser, NULL, 0, &dLength);
81-
if (!dLength)
82-
goto end;
83-
84-
if (!(pTokenUser= (PTOKEN_USER)LocalAlloc(0, dLength)))
85-
goto end;
86-
87-
if (!GetTokenInformation(hToken, TokenUser, (PVOID)pTokenUser, dLength, &dLength))
88-
goto end;
89-
90-
if (!LookupAccountSid(NULL, pTokenUser->User.Sid, username, &username_length, domainname, &domainsize, &sidnameuse))
91-
goto end;
9256

93-
Ret= strcmp(username, info->user_name) ? CR_ERROR : CR_OK;
94-
end:
95-
if (pTokenUser)
96-
LocalFree(pTokenUser);
57+
username_length= sizeof(username) - 1;
58+
ret= CR_ERROR;
59+
if (GetUserName(username, &username_length))
60+
{
61+
/* Always compare names case-insensitive on Windows.*/
62+
if (_stricmp(username, info->user_name) == 0)
63+
ret= CR_OK;
64+
}
9765
RevertToSelf();
98-
/* now it's simple as that */
99-
return Ret;
66+
67+
return ret;
10068
}
10169

10270
static struct st_mysql_auth pipe_auth_handler=
@@ -106,11 +74,11 @@ static struct st_mysql_auth pipe_auth_handler=
10674
pipe_auth
10775
};
10876

109-
maria_declare_plugin(socket_auth)
77+
maria_declare_plugin(auth_named_pipe)
11078
{
11179
MYSQL_AUTHENTICATION_PLUGIN,
11280
&pipe_auth_handler,
113-
"windows_pipe",
81+
"named_pipe",
11482
"Vladislav Vaintroub, Georg Richter",
11583
"Windows named pipe based authentication",
11684
PLUGIN_LICENSE_GPL,

0 commit comments

Comments
 (0)