From 35a201cc8ef0c3f5b2df88d2e528aabee1048348 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Fri, 30 Apr 2021 18:47:09 +0200 Subject: Initial/Final commit --- mariadb-connector-c-v_2.3.7/plugins/auth/dialog.c | 215 ++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 mariadb-connector-c-v_2.3.7/plugins/auth/dialog.c (limited to 'mariadb-connector-c-v_2.3.7/plugins/auth/dialog.c') diff --git a/mariadb-connector-c-v_2.3.7/plugins/auth/dialog.c b/mariadb-connector-c-v_2.3.7/plugins/auth/dialog.c new file mode 100644 index 0000000..7f26ae6 --- /dev/null +++ b/mariadb-connector-c-v_2.3.7/plugins/auth/dialog.c @@ -0,0 +1,215 @@ +/************************************************************************************ + Copyright (C) 2014 MariaDB Corporation AB + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not see + or write to the Free Software Foundation, Inc., + 51 Franklin St., Fifth Floor, Boston, MA 02110, USA +*************************************************************************************/ +#ifndef _WIN32 +#define _GNU_SOURCE 1 +#endif + +#include +#include +#include +#include +#include + +#ifndef WIN32 +#include +#endif + + +/* function prototypes */ +extern char *get_tty_password(char *opt_message, char *buff, int bufflen); +static int auth_dialog_open(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql); +static int auth_dialog_init(char *unused1, + size_t unused2, + int unused3, + va_list); + +mysql_authentication_dialog_ask_t auth_dialog_func; + +mysql_declare_client_plugin(AUTHENTICATION) + "dialog", + "Sergei Golubchik, Georg Richter", + "Dialog Client Authentication Plugin", + {0,1,0}, + "LGPL", + NULL, + auth_dialog_init, + NULL, + NULL, + auth_dialog_open +mysql_end_client_plugin; + + +/* {{{ static char *auth_dialog_native_prompt */ +/* + Native dialog prompt via stdin + + SYNOPSIS + auth_dialog_native_prompt + mysql connection handle + type input type + prompt prompt + buffer Input buffer + buffer_len Input buffer length + + DESCRIPTION + + RETURNS + Input buffer +*/ +static char *auth_dialog_native_prompt(MYSQL *mysql, + int type, + const char *prompt, + char *buffer, + int buffer_len) +{ + /* display prompt */ + fprintf(stdout, "%s", prompt); + + memset(buffer, 0, buffer_len); + + /* for type 2 (password) don't display input */ + if (type != 2) + { + if (fgets(buffer, buffer_len - 1, stdin)) + { + /* remove trailing line break */ + size_t length= strlen(buffer); + if (length && buffer[length - 1] == '\n') + buffer[length - 1]= 0; + } + } + else + { + get_tty_password("", buffer, buffer_len - 1); + } + return buffer; +} +/* }}} */ + +/* {{{ static int auth_dialog_open */ +/* + opens dialog + + SYNOPSIS + vio Vio + mysql connection handle + + DESCRIPTION + reads prompt from server, waits for input and sends + input to server. + Note that first byte of prompt indicates if we have a + password which should not be echoed to stdout. + + RETURN + CR_ERROR if an error occurs + CR_OK + CR_OK_HANDSHAKE_COMPLETE +*/ +static int auth_dialog_open(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql) +{ + uchar *packet; + uchar type; + char dialog_buffer[1024]; + char *response; + int packet_length; + my_bool first_loop= TRUE; + + do { + if ((packet_length= vio->read_packet(vio, &packet)) < 0) + /* read error */ + return CR_ERROR; + + if (packet_length > 0) + { + type= *packet; + packet++; + + /* check for protocol packet */ + if (!type || type == 254) + return CR_OK_HANDSHAKE_COMPLETE; + + if ((type >> 1) == 2 && + first_loop && + mysql->passwd && mysql->passwd[0]) + response= mysql->passwd; + else + response= auth_dialog_func(mysql, type >> 1, + (const char *)packet, + dialog_buffer, 1024); + } + else + { + /* in case mysql_change_user was called the client needs + to send packet first */ + response= mysql->passwd; + } + if (!response || + vio->write_packet(vio, response, strlen(response) + 1)) + return CR_ERROR; + + first_loop= FALSE; + + } while((type & 1) != 1); + return CR_OK; +} +/* }}} */ + +/* {{{ static int auth_dialog_init */ +/* + Initialization routine + + SYNOPSIS + auth_dialog_init + unused1 + unused2 + unused3 + unused4 + + DESCRIPTION + Init function checks if the caller provides own dialog function. + The function name must be mariadb_auth_dialog or + mysql_authentication_dialog_ask. If the function cannot be found, + we will use owr own simple command line input. + + RETURN + 0 success +*/ +static int auth_dialog_init(char *unused1 __attribute__((unused)), + size_t unused2 __attribute__((unused)), + int unused3 __attribute__((unused)), + va_list unused4 __attribute__((unused))) +{ + void *func; +#ifdef WIN32 + if (!(func= GetProcAddress(GetModuleHandle(NULL), "mariadb_auth_dialog"))) + /* for MySQL users */ + func= GetProcAddress(GetModuleHandle(NULL), "mysql_authentication_dialog_ask"); +#else + if (!(func= dlsym(RTLD_DEFAULT, "mariadb_auth_dialog"))) + /* for MySQL users */ + func= dlsym(RTLD_DEFAULT, "mysql_authentication_dialog_ask"); +#endif + if (func) + auth_dialog_func= (mysql_authentication_dialog_ask_t)func; + else + auth_dialog_func= auth_dialog_native_prompt; + + return 0; +} +/* }}} */ -- cgit v1.2.3