| 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 | |
| 17 | #include <my_global.h> |
| 18 | #include <mysql/plugin_auth.h> |
| 19 | #include <mysql/client_plugin.h> |
| 20 | #include <string.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | /********************* CLIENT SIDE ***************************************/ |
| 25 | /* |
| 26 | client plugin used for testing the plugin API |
| 27 | */ |
| 28 | #include <mysql.h> |
| 29 | |
| 30 | /** |
| 31 | The main function of the test plugin. |
| 32 | |
| 33 | Reads the prompt, check if the handshake is done and if the prompt is a |
| 34 | password request and returns the password. Otherwise return error. |
| 35 | |
| 36 | @note |
| 37 | 1. this plugin shows how a client authentication plugin |
| 38 | may read a MySQL protocol OK packet internally - which is important |
| 39 | where a number of packets is not known in advance. |
| 40 | 2. the first byte of the prompt is special. it is not |
| 41 | shown to the user, but signals whether it is the last question |
| 42 | (prompt[0] & 1 == 1) or not last (prompt[0] & 1 == 0), |
| 43 | and whether the input is a password (not echoed). |
| 44 | 3. the prompt is expected to be sent zero-terminated |
| 45 | */ |
| 46 | static int test_plugin_client(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql) |
| 47 | { |
| 48 | unsigned char *pkt, cmd= 0; |
| 49 | int pkt_len, res; |
| 50 | char *reply; |
| 51 | |
| 52 | do |
| 53 | { |
| 54 | /* read the prompt */ |
| 55 | pkt_len= vio->read_packet(vio, &pkt); |
| 56 | if (pkt_len < 0) |
| 57 | return CR_ERROR; |
| 58 | |
| 59 | if (pkt == 0) |
| 60 | { |
| 61 | /* |
| 62 | in mysql_change_user() the client sends the first packet, so |
| 63 | the first vio->read_packet() does nothing (pkt == 0). |
| 64 | |
| 65 | We send the "password", assuming the client knows what its doing. |
| 66 | (in other words, the dialog plugin should be only set as a default |
| 67 | authentication plugin on the client if the first question |
| 68 | asks for a password - which will be sent in cleat text, by the way) |
| 69 | */ |
| 70 | reply= mysql->passwd; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | cmd= *pkt++; |
| 75 | |
| 76 | /* is it MySQL protocol (0=OK or 254=need old password) packet ? */ |
| 77 | if (cmd == 0 || cmd == 254) |
| 78 | return CR_OK_HANDSHAKE_COMPLETE; /* yes. we're done */ |
| 79 | |
| 80 | /* |
| 81 | asking for a password with an empty prompt means mysql->password |
| 82 | otherwise return an error |
| 83 | */ |
| 84 | if ((cmd == LAST_PASSWORD[0] || cmd == PASSWORD_QUESTION[0]) && *pkt == 0) |
| 85 | reply= mysql->passwd; |
| 86 | else |
| 87 | return CR_ERROR; |
| 88 | } |
| 89 | if (!reply) |
| 90 | return CR_ERROR; |
| 91 | /* send the reply to the server */ |
| 92 | res= vio->write_packet(vio, (const unsigned char *) reply, |
| 93 | (int)strlen(reply) + 1); |
| 94 | |
| 95 | if (res) |
| 96 | return CR_ERROR; |
| 97 | |
| 98 | /* repeat unless it was the last question */ |
| 99 | } while (cmd != LAST_QUESTION[0] && cmd != PASSWORD_QUESTION[0]); |
| 100 | |
| 101 | /* the job of reading the ok/error packet is left to the server */ |
| 102 | return CR_OK; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | mysql_declare_client_plugin(AUTHENTICATION) |
| 107 | "qa_auth_client" , |
| 108 | "Horst Hunger" , |
| 109 | "Dialog Client Authentication Plugin" , |
| 110 | {0,1,0}, |
| 111 | "GPL" , |
| 112 | NULL, |
| 113 | NULL, |
| 114 | NULL, |
| 115 | NULL, |
| 116 | test_plugin_client |
| 117 | mysql_end_client_plugin; |
| 118 | |