aboutsummaryrefslogtreecommitdiff
path: root/mariadb-connector-c-v_2.3.7/plugins/auth/dialog.c
blob: 7f26ae6bf36da65a317b4632cd200ec66fb47733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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 <http://www.gnu.org/licenses>
   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 <my_global.h>
#include <mysql.h>
#include <mysql/client_plugin.h>
#include <string.h>
#include <memory.h>

#ifndef WIN32
#include <dlfcn.h>
#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;
}
/* }}} */