summaryrefslogtreecommitdiff
path: root/plugin/auth/dialog.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/auth/dialog.c')
-rw-r--r--plugin/auth/dialog.c132
1 files changed, 52 insertions, 80 deletions
diff --git a/plugin/auth/dialog.c b/plugin/auth/dialog.c
index 72c6364f49a..f496d454256 100644
--- a/plugin/auth/dialog.c
+++ b/plugin/auth/dialog.c
@@ -1,17 +1,19 @@
/* Copyright (C) 2010 Sergei Golubchik and Monty Program Ab
+ Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; version 2 of the
+ License.
- This program 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 General Public License for more details.
+ This program 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 General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file
@@ -33,7 +35,9 @@
a correct password. It shows the situation when a number of questions
is not known in advance.
*/
-#define _GNU_SOURCE /* for RTLD_DEFAULT */
+#if defined (WIN32) && !defined (RTLD_DEFAULT)
+# define RTLD_DEFAULT GetModuleHandle(NULL)
+#endif
#include <mysql/plugin_auth.h>
#include <mysql/client_plugin.h>
@@ -41,9 +45,13 @@
#include <stdio.h>
#include <stdlib.h>
+#if !defined (_GNU_SOURCE)
+# define _GNU_SOURCE /* for RTLD_DEFAULT */
+#endif
+
/**
first byte of the question string is the question "type".
- It can be a "ordinary" or a "password" question.
+ It can be an "ordinary" or a "password" question.
The last bit set marks a last question in the authentication exchange.
*/
#define ORDINARY_QUESTION "\2"
@@ -51,14 +59,11 @@
#define PASSWORD_QUESTION "\4"
#define LAST_PASSWORD "\5"
-typedef unsigned char uchar;
-
/********************* SERVER SIDE ****************************************/
/**
- dialog demo with two questions, one password and one ordinary.
+ dialog demo with two questions, one password and one, the last, ordinary.
*/
-
static int two_questions(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
{
unsigned char *pkt;
@@ -66,7 +71,7 @@ static int two_questions(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
/* send a password question */
if (vio->write_packet(vio,
- (const uchar*) (PASSWORD_QUESTION "Password, please:"),
+ (const unsigned char *) PASSWORD_QUESTION "Password, please:",
18))
return CR_ERROR;
@@ -74,15 +79,16 @@ static int two_questions(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
if ((pkt_len= vio->read_packet(vio, &pkt)) < 0)
return CR_ERROR;
- info->password_used = 1;
+ info->password_used= PASSWORD_USED_YES;
/* fail if the password is wrong */
- if (strcmp((char*) pkt, info->auth_string))
+ if (strcmp((const char *) pkt, info->auth_string))
return CR_ERROR;
/* send the last, ordinary, question */
if (vio->write_packet(vio,
- (const uchar*) (LAST_QUESTION "Are you sure ?"), 15))
+ (const unsigned char *) LAST_QUESTION "Are you sure ?",
+ 15))
return CR_ERROR;
/* read the answer */
@@ -90,7 +96,7 @@ static int two_questions(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
return CR_ERROR;
/* check the reply */
- return strcmp((char*) pkt, "yes, of course") ? CR_ERROR : CR_OK;
+ return strcmp((const char *) pkt, "yes, of course") ? CR_ERROR : CR_OK;
}
static struct st_mysql_auth two_handler=
@@ -100,11 +106,7 @@ static struct st_mysql_auth two_handler=
two_questions
};
-
-/**
- dialog demo where the number of questions is not known in advance
-*/
-
+/* dialog demo where the number of questions is not known in advance */
static int three_attempts(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
{
unsigned char *pkt;
@@ -113,21 +115,21 @@ static int three_attempts(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
for (i= 0; i < 3; i++)
{
/* send the prompt */
- if (vio->write_packet(vio,
- (const uchar*) (PASSWORD_QUESTION "Password, please:"), 18))
+ if (vio->write_packet(vio,
+ (const unsigned char *) PASSWORD_QUESTION "Password, please:", 18))
return CR_ERROR;
/* read the password */
if ((pkt_len= vio->read_packet(vio, &pkt)) < 0)
return CR_ERROR;
- info->password_used = 1;
+ info->password_used= PASSWORD_USED_YES;
/*
finish, if the password is correct.
note, that we did not mark the prompt packet as "last"
*/
- if (strcmp((char*) pkt, info->auth_string) == 0)
+ if (strcmp((const char *) pkt, info->auth_string) == 0)
return CR_OK;
}
@@ -171,50 +173,18 @@ mysql_declare_plugin(dialog)
NULL
}
mysql_declare_plugin_end;
-maria_declare_plugin(dialog)
-{
- MYSQL_AUTHENTICATION_PLUGIN,
- &two_handler,
- "two_questions",
- "Sergei Golubchik",
- "Dialog plugin demo 1",
- PLUGIN_LICENSE_GPL,
- NULL,
- NULL,
- 0x0100,
- NULL,
- NULL,
- "1.0",
- MariaDB_PLUGIN_MATURITY_BETA
-},
-{
- MYSQL_AUTHENTICATION_PLUGIN,
- &three_handler,
- "three_attempts",
- "Sergei Golubchik",
- "Dialog plugin demo 2",
- PLUGIN_LICENSE_GPL,
- NULL,
- NULL,
- 0x0100,
- NULL,
- NULL,
- "1.0",
- MariaDB_PLUGIN_MATURITY_BETA
-}
-maria_declare_plugin_end;
/********************* CLIENT SIDE ***************************************/
/*
This plugin performs a dialog with the user, asking questions and
reading answers. Depending on the client it may be desirable to do it
using GUI, or console, with or without curses, or read answers
- from a smardcard, for example.
+ from a smartcard, for example.
To support all this variety, the dialog plugin has a callback function
"authentication_dialog_ask". If the client has a function of this name
dialog plugin will use it for communication with the user. Otherwise
- a default gets() based implementation will be used.
+ a default fgets() based implementation will be used.
*/
#include <mysql.h>
#include <dlfcn.h>
@@ -226,21 +196,17 @@ static char *builtin_ask(MYSQL *mysql __attribute__((unused)),
const char *prompt,
char *buf, int buf_len)
{
- int len;
-
+ char *ptr;
fputs(prompt, stdout);
fputc(' ', stdout);
- if (fgets(buf, buf_len, stdin) == 0)
- return 0;
-
- len= strlen(buf);
- if (len && buf[len-1]=='\n')
- buf[len-1]=0;
+ if (fgets(buf, buf_len, stdin) == NULL)
+ return NULL;
+ if ((ptr= strchr(buf, '\n')))
+ *ptr= 0;
return buf;
}
-
/**
The main function of the dialog plugin.
@@ -257,7 +223,6 @@ static char *builtin_ask(MYSQL *mysql __attribute__((unused)),
and whether the input is a password (not echoed).
3. the prompt is expected to be sent zero-terminated
*/
-
static int perform_dialog(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
{
unsigned char *pkt, cmd= 0;
@@ -277,7 +242,7 @@ static int perform_dialog(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
in mysql_change_user() the client sends the first packet, so
the first vio->read_packet() does nothing (pkt == 0).
- We send the "password", assuming the client knows what its doing.
+ We send the "password", assuming the client knows what it's doing.
(in other words, the dialog plugin should be only set as a default
authentication plugin on the client if the first question
asks for a password - which will be sent in clear text, by the way)
@@ -299,12 +264,14 @@ static int perform_dialog(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
if ((cmd >> 1) == 2 && *pkt == 0)
reply= mysql->passwd;
else
- reply= ask(mysql, cmd >> 1, (char*) pkt, reply_buf, sizeof(reply_buf));
+ reply= ask(mysql, cmd >> 1, (const char *) pkt,
+ reply_buf, sizeof(reply_buf));
if (!reply)
return CR_ERROR;
}
/* send the reply to the server */
- res= vio->write_packet(vio, (uchar*) reply, strlen(reply)+1);
+ res= vio->write_packet(vio, (const unsigned char *) reply,
+ strlen(reply)+1);
if (reply != mysql->passwd && reply != reply_buf)
free(reply);
@@ -319,7 +286,6 @@ static int perform_dialog(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
return CR_OK;
}
-
/**
initialization function of the dialog plugin
@@ -327,10 +293,13 @@ static int perform_dialog(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
or fall back to the default implementation.
*/
-static int init_dialog()
+static int init_dialog(char *unused1 __attribute__((unused)),
+ size_t unused2 __attribute__((unused)),
+ int unused3 __attribute__((unused)),
+ va_list unused4 __attribute__((unused)))
{
void *sym= dlsym(RTLD_DEFAULT, "mysql_authentication_dialog_ask");
- ask= sym ? (mysql_authentication_dialog_ask_t)sym : builtin_ask;
+ ask= sym ? (mysql_authentication_dialog_ask_t) sym : builtin_ask;
return 0;
}
@@ -339,8 +308,11 @@ mysql_declare_client_plugin(AUTHENTICATION)
"Sergei Golubchik",
"Dialog Client Authentication Plugin",
{0,1,0},
+ "GPL",
+ NULL,
init_dialog,
NULL,
+ NULL,
perform_dialog
mysql_end_client_plugin;