1 | /* Copyright (C) 2010 Sergei Golubchik and Monty Program Ab |
2 | Copyright (c) 2010, 2011, Oracle and/or its affiliates. |
3 | |
4 | This program is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU General Public License as |
6 | published by the Free Software Foundation; version 2 of the |
7 | License. |
8 | |
9 | This program 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 |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
17 | |
18 | /** |
19 | @file |
20 | |
21 | examples for dialog client authentication plugin |
22 | |
23 | Two examples are provided: two_questions server plugin, that asks |
24 | the password and an "Are you sure?" question with a reply "yes, of course". |
25 | It demonstrates the usage of "password" (input is hidden) and "ordinary" |
26 | (input can be echoed) questions, and how to mark the last question, |
27 | to avoid an extra roundtrip. |
28 | |
29 | And three_attempts plugin that gives the user three attempts to enter |
30 | a correct password. It shows the situation when a number of questions |
31 | is not known in advance. |
32 | */ |
33 | |
34 | #include <mysql/plugin_auth.h> |
35 | #include <string.h> |
36 | #include <stdio.h> |
37 | #include <stdlib.h> |
38 | #include <mysql/auth_dialog_client.h> |
39 | |
40 | /********************* SERVER SIDE ****************************************/ |
41 | |
42 | /** |
43 | dialog demo with two questions, one password and one, the last, ordinary. |
44 | */ |
45 | static int two_questions(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info) |
46 | { |
47 | unsigned char *pkt; |
48 | int pkt_len; |
49 | |
50 | /* send a password question */ |
51 | if (vio->write_packet(vio, |
52 | (const unsigned char *) PASSWORD_QUESTION "Password, please:" , |
53 | 18)) |
54 | return CR_ERROR; |
55 | |
56 | /* read the answer */ |
57 | if ((pkt_len= vio->read_packet(vio, &pkt)) < 0) |
58 | return CR_ERROR; |
59 | |
60 | info->password_used= PASSWORD_USED_YES; |
61 | |
62 | /* fail if the password is wrong */ |
63 | if (strcmp((const char *) pkt, info->auth_string)) |
64 | return CR_ERROR; |
65 | |
66 | /* send the last, ordinary, question */ |
67 | if (vio->write_packet(vio, |
68 | (const unsigned char *) LAST_QUESTION "Are you sure ?" , |
69 | 15)) |
70 | return CR_ERROR; |
71 | |
72 | /* read the answer */ |
73 | if ((pkt_len= vio->read_packet(vio, &pkt)) < 0) |
74 | return CR_ERROR; |
75 | |
76 | /* check the reply */ |
77 | return strcmp((const char *) pkt, "yes, of course" ) ? CR_ERROR : CR_OK; |
78 | } |
79 | |
80 | static struct st_mysql_auth two_handler= |
81 | { |
82 | MYSQL_AUTHENTICATION_INTERFACE_VERSION, |
83 | "dialog" , /* requires dialog client plugin */ |
84 | two_questions |
85 | }; |
86 | |
87 | /* dialog demo where the number of questions is not known in advance */ |
88 | static int three_attempts(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info) |
89 | { |
90 | unsigned char *pkt; |
91 | int pkt_len, i; |
92 | |
93 | for (i= 0; i < 3; i++) |
94 | { |
95 | /* send the prompt */ |
96 | if (vio->write_packet(vio, |
97 | (const unsigned char *) PASSWORD_QUESTION "Password, please:" , 18)) |
98 | return CR_ERROR; |
99 | |
100 | /* read the password */ |
101 | if ((pkt_len= vio->read_packet(vio, &pkt)) < 0) |
102 | return CR_ERROR; |
103 | |
104 | info->password_used= PASSWORD_USED_YES; |
105 | |
106 | /* |
107 | finish, if the password is correct. |
108 | note, that we did not mark the prompt packet as "last" |
109 | */ |
110 | if (strcmp((const char *) pkt, info->auth_string) == 0) |
111 | return CR_OK; |
112 | } |
113 | |
114 | return CR_ERROR; |
115 | } |
116 | |
117 | static struct st_mysql_auth three_handler= |
118 | { |
119 | MYSQL_AUTHENTICATION_INTERFACE_VERSION, |
120 | "dialog" , /* requires dialog client plugin */ |
121 | three_attempts |
122 | }; |
123 | |
124 | mysql_declare_plugin(dialog) |
125 | { |
126 | MYSQL_AUTHENTICATION_PLUGIN, |
127 | &two_handler, |
128 | "two_questions" , |
129 | "Sergei Golubchik" , |
130 | "Dialog plugin demo 1" , |
131 | PLUGIN_LICENSE_GPL, |
132 | NULL, |
133 | NULL, |
134 | 0x0100, |
135 | NULL, |
136 | NULL, |
137 | NULL, |
138 | 0, |
139 | }, |
140 | { |
141 | MYSQL_AUTHENTICATION_PLUGIN, |
142 | &three_handler, |
143 | "three_attempts" , |
144 | "Sergei Golubchik" , |
145 | "Dialog plugin demo 2" , |
146 | PLUGIN_LICENSE_GPL, |
147 | NULL, |
148 | NULL, |
149 | 0x0100, |
150 | NULL, |
151 | NULL, |
152 | NULL, |
153 | 0, |
154 | } |
155 | mysql_declare_plugin_end; |
156 | |
157 | |