|
| 1 | +/* Copyright (C) 2015 Vladislav Vaintroub, Georg Richter and Monty Program Ab |
| 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 | + |
| 17 | +/** |
| 18 | + @file |
| 19 | +
|
| 20 | + auth_pipd authentication plugin. |
| 21 | +
|
| 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. |
| 25 | +*/ |
| 26 | + |
| 27 | + |
| 28 | +#include <mysql/plugin_auth.h> |
| 29 | +#include <string.h> |
| 30 | +#include <lmcons.h> |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 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 |
| 43 | +*/ |
| 44 | + |
| 45 | +static int pipe_auth(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info) |
| 46 | +{ |
| 47 | + unsigned char *pkt; |
| 48 | + PTOKEN_USER pTokenUser= NULL; |
| 49 | + HANDLE hToken; |
| 50 | + 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; |
| 58 | + |
| 59 | + /* no user name yet ? read the client handshake packet with the user name */ |
| 60 | + if (info->user_name == 0) |
| 61 | + { |
| 62 | + if (vio->read_packet(vio, &pkt) < 0) |
| 63 | + return CR_ERROR; |
| 64 | + } |
| 65 | + |
| 66 | + info->password_used= PASSWORD_USED_NO_MENTION; |
| 67 | + |
| 68 | + vio->info(vio, &vio_info); |
| 69 | + if (vio_info.protocol != MYSQL_VIO_PIPE) |
| 70 | + return CR_ERROR; |
| 71 | + |
| 72 | + /* get the UID of the client process */ |
| 73 | + if (!ImpersonateNamedPipeClient(vio_info.handle)) |
| 74 | + 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; |
| 92 | + |
| 93 | + Ret= strcmp(username, info->user_name) ? CR_ERROR : CR_OK; |
| 94 | +end: |
| 95 | + if (pTokenUser) |
| 96 | + LocalFree(pTokenUser); |
| 97 | + RevertToSelf(); |
| 98 | + /* now it's simple as that */ |
| 99 | + return Ret; |
| 100 | +} |
| 101 | + |
| 102 | +static struct st_mysql_auth pipe_auth_handler= |
| 103 | +{ |
| 104 | + MYSQL_AUTHENTICATION_INTERFACE_VERSION, |
| 105 | + 0, |
| 106 | + pipe_auth |
| 107 | +}; |
| 108 | + |
| 109 | +maria_declare_plugin(socket_auth) |
| 110 | +{ |
| 111 | + MYSQL_AUTHENTICATION_PLUGIN, |
| 112 | + &pipe_auth_handler, |
| 113 | + "windows_pipe", |
| 114 | + "Vladislav Vaintroub, Georg Richter", |
| 115 | + "Windows named pipe based authentication", |
| 116 | + PLUGIN_LICENSE_GPL, |
| 117 | + NULL, |
| 118 | + NULL, |
| 119 | + 0x0100, |
| 120 | + NULL, |
| 121 | + NULL, |
| 122 | + "1.0", |
| 123 | + MariaDB_PLUGIN_MATURITY_STABLE |
| 124 | +} |
| 125 | +maria_declare_plugin_end; |
| 126 | + |
0 commit comments