MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qa_auth_client.c
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 
29 #define ORDINARY_QUESTION "\2"
30 #define LAST_QUESTION "\3"
31 #define LAST_PASSWORD "\4"
32 #define PASSWORD_QUESTION "\5"
33 
34 /********************* CLIENT SIDE ***************************************/
35 /*
36  client plugin used for testing the plugin API
37 */
38 #include <mysql.h>
39 
56 static int test_plugin_client(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
57 {
58  unsigned char *pkt, cmd= 0;
59  int pkt_len, res;
60  char *reply;
61 
62  do
63  {
64  /* read the prompt */
65  pkt_len= vio->read_packet(vio, &pkt);
66  if (pkt_len < 0)
67  return CR_ERROR;
68 
69  if (pkt == 0)
70  {
71  /*
72  in mysql_change_user() the client sends the first packet, so
73  the first vio->read_packet() does nothing (pkt == 0).
74 
75  We send the "password", assuming the client knows what its doing.
76  (in other words, the dialog plugin should be only set as a default
77  authentication plugin on the client if the first question
78  asks for a password - which will be sent in cleat text, by the way)
79  */
80  reply= mysql->passwd;
81  }
82  else
83  {
84  cmd= *pkt++;
85 
86  /* is it MySQL protocol (0=OK or 254=need old password) packet ? */
87  if (cmd == 0 || cmd == 254)
88  return CR_OK_HANDSHAKE_COMPLETE; /* yes. we're done */
89 
90  /*
91  asking for a password with an empty prompt means mysql->password
92  otherwise return an error
93  */
94  if ((cmd == LAST_PASSWORD[0] || cmd == PASSWORD_QUESTION[0]) && *pkt == 0)
95  reply= mysql->passwd;
96  else
97  return CR_ERROR;
98  }
99  if (!reply)
100  return CR_ERROR;
101  /* send the reply to the server */
102  res= vio->write_packet(vio, (const unsigned char *) reply,
103  strlen(reply) + 1);
104 
105  if (res)
106  return CR_ERROR;
107 
108  /* repeat unless it was the last question */
109  } while (cmd != LAST_QUESTION[0] && cmd != PASSWORD_QUESTION[0]);
110 
111  /* the job of reading the ok/error packet is left to the server */
112  return CR_OK;
113 }
114 
115 
116 mysql_declare_client_plugin(AUTHENTICATION)
117  "qa_auth_client",
118  "Horst Hunger",
119  "Dialog Client Authentication Plugin",
120  {0,1,0},
121  "GPL",
122  NULL,
123  NULL,
124  NULL,
125  NULL,
126  test_plugin_client
127 mysql_end_client_plugin;