|
| 1 | +/************************************************************************************ |
| 2 | + Copyright (C) 2014,2015 MariaDB Corporation AB |
| 3 | + |
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Library General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2 of the License, or (at your option) any later version. |
| 8 | + |
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Library General Public License for more details. |
| 13 | + |
| 14 | + You should have received a copy of the GNU Library General Public |
| 15 | + License along with this library; if not see <http://www.gnu.org/licenses> |
| 16 | + or write to the Free Software Foundation, Inc., |
| 17 | + 51 Franklin St., Fifth Floor, Boston, MA 02110, USA |
| 18 | +*************************************************************************************/ |
| 19 | +#include <my_global.h> |
| 20 | +#include <mysql.h> |
| 21 | +#include <mysql/client_plugin.h> |
| 22 | +#include <string.h> |
| 23 | +#include <memory.h> |
| 24 | +#include <errmsg.h> |
| 25 | + |
| 26 | + |
| 27 | +/* function prototypes */ |
| 28 | +static int auth_old_password(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql); |
| 29 | + |
| 30 | +typedef struct st_mysql_client_plugin_AUTHENTICATION auth_plugin_t; |
| 31 | + |
| 32 | +typedef struct { |
| 33 | + int (*read_packet)(struct st_plugin_vio *vio, uchar **buf); |
| 34 | + int (*write_packet)(struct st_plugin_vio *vio, const uchar *pkt, size_t pkt_len); |
| 35 | + void (*info)(struct st_plugin_vio *vio, struct st_plugin_vio_info *info); |
| 36 | + /* -= end of MYSQL_PLUGIN_VIO =- */ |
| 37 | + MYSQL *mysql; |
| 38 | + auth_plugin_t *plugin; /**< what plugin we're under */ |
| 39 | + const char *db; |
| 40 | + struct { |
| 41 | + uchar *pkt; /**< pointer into NET::buff */ |
| 42 | + uint pkt_len; |
| 43 | + } cached_server_reply; |
| 44 | + uint packets_read, packets_written; /**< counters for send/received packets */ |
| 45 | + my_bool mysql_change_user; /**< if it's mysql_change_user() */ |
| 46 | + int last_read_packet_len; /**< the length of the last *read* packet */ |
| 47 | +} MCPVIO_EXT; |
| 48 | + |
| 49 | +#ifndef HAVE_OLDPASSWORD_DYNAMIC |
| 50 | +struct st_mysql_client_plugin_AUTHENTICATION auth_old_password_plugin= |
| 51 | +#else |
| 52 | +struct st_mysql_client_plugin_AUTHENTICATION _mysql_client_plugin_declaration_ = |
| 53 | +#endif |
| 54 | +{ |
| 55 | + MYSQL_CLIENT_AUTHENTICATION_PLUGIN, |
| 56 | + MYSQL_CLIENT_AUTHENTICATION_PLUGIN_INTERFACE_VERSION, |
| 57 | + "old_password", |
| 58 | + "Sergei Golubchik, R.J. Silk, Georg Richter", |
| 59 | + "Old (pre 4.1) authentication plugin", |
| 60 | + {1,0,0}, |
| 61 | + "LGPL", |
| 62 | + NULL, |
| 63 | + NULL, |
| 64 | + auth_old_password |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + client authentication plugin that does old MySQL authentication |
| 69 | + using an 8-byte (4.0-) scramble |
| 70 | +*/ |
| 71 | + |
| 72 | +static int auth_old_password(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql) |
| 73 | +{ |
| 74 | + uchar *pkt; |
| 75 | + int pkt_len; |
| 76 | + |
| 77 | + if (((MCPVIO_EXT *)vio)->mysql_change_user) |
| 78 | + { |
| 79 | + /* |
| 80 | + in mysql_change_user() the client sends the first packet. |
| 81 | + we use the old scramble. |
| 82 | + */ |
| 83 | + pkt= (uchar*)mysql->scramble_buff; |
| 84 | + pkt_len= SCRAMBLE_LENGTH_323 + 1; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + /* read the scramble */ |
| 89 | + if ((pkt_len= vio->read_packet(vio, &pkt)) < 0) |
| 90 | + return CR_ERROR; |
| 91 | + |
| 92 | + if (pkt_len != SCRAMBLE_LENGTH_323 + 1 && |
| 93 | + pkt_len != SCRAMBLE_LENGTH + 1) |
| 94 | + return CR_SERVER_HANDSHAKE_ERR; |
| 95 | + |
| 96 | + /* save it in MYSQL */ |
| 97 | + memcpy(mysql->scramble_buff, pkt, pkt_len); |
| 98 | + mysql->scramble_buff[pkt_len] = 0; |
| 99 | + } |
| 100 | + |
| 101 | + if (mysql->passwd[0]) |
| 102 | + { |
| 103 | + char scrambled[SCRAMBLE_LENGTH_323 + 1]; |
| 104 | + scramble_323(scrambled, (char*)pkt, mysql->passwd); |
| 105 | + if (vio->write_packet(vio, (uchar*)scrambled, SCRAMBLE_LENGTH_323 + 1)) |
| 106 | + return CR_ERROR; |
| 107 | + } |
| 108 | + else |
| 109 | + if (vio->write_packet(vio, 0, 0)) /* no password */ |
| 110 | + return CR_ERROR; |
| 111 | + |
| 112 | + return CR_OK; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | + |
0 commit comments