You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.9 KiB

15 years ago
  1. /* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU General Public License as
  4. published by the Free Software Foundation; version 2 of the
  5. License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. #include <my_global.h>
  14. #include <mysql/plugin_auth.h>
  15. #include <mysql/client_plugin.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. /**
  20. first byte of the question string is the question "type".
  21. It can be a "ordinary" or a "password" question.
  22. The last bit set marks a last question in the authentication exchange.
  23. */
  24. #define ORDINARY_QUESTION "\2"
  25. #define LAST_QUESTION "\3"
  26. #define LAST_PASSWORD "\4"
  27. #define PASSWORD_QUESTION "\5"
  28. /********************* CLIENT SIDE ***************************************/
  29. /*
  30. client plugin used for testing the plugin API
  31. */
  32. #include <mysql.h>
  33. /**
  34. The main function of the test plugin.
  35. Reads the prompt, check if the handshake is done and if the prompt is a
  36. password request and returns the password. Otherwise return error.
  37. @note
  38. 1. this plugin shows how a client authentication plugin
  39. may read a MySQL protocol OK packet internally - which is important
  40. where a number of packets is not known in advance.
  41. 2. the first byte of the prompt is special. it is not
  42. shown to the user, but signals whether it is the last question
  43. (prompt[0] & 1 == 1) or not last (prompt[0] & 1 == 0),
  44. and whether the input is a password (not echoed).
  45. 3. the prompt is expected to be sent zero-terminated
  46. */
  47. static int test_plugin_client(MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
  48. {
  49. unsigned char *pkt, cmd= 0;
  50. int pkt_len, res;
  51. char *reply;
  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. if (pkt == 0)
  59. {
  60. /*
  61. in mysql_change_user() the client sends the first packet, so
  62. the first vio->read_packet() does nothing (pkt == 0).
  63. We send the "password", assuming the client knows what its doing.
  64. (in other words, the dialog plugin should be only set as a default
  65. authentication plugin on the client if the first question
  66. asks for a password - which will be sent in cleat text, by the way)
  67. */
  68. reply= mysql->passwd;
  69. }
  70. else
  71. {
  72. cmd= *pkt++;
  73. /* is it MySQL protocol (0=OK or 254=need old password) packet ? */
  74. if (cmd == 0 || cmd == 254)
  75. return CR_OK_HANDSHAKE_COMPLETE; /* yes. we're done */
  76. /*
  77. asking for a password with an empty prompt means mysql->password
  78. otherwise return an error
  79. */
  80. if ((cmd == LAST_PASSWORD[0] || cmd == PASSWORD_QUESTION[0]) && *pkt == 0)
  81. reply= mysql->passwd;
  82. else
  83. return CR_ERROR;
  84. }
  85. if (!reply)
  86. return CR_ERROR;
  87. /* send the reply to the server */
  88. res= vio->write_packet(vio, (const unsigned char *) reply,
  89. strlen(reply) + 1);
  90. if (res)
  91. return CR_ERROR;
  92. /* repeat unless it was the last question */
  93. } while (cmd != LAST_QUESTION[0] && cmd != PASSWORD_QUESTION[0]);
  94. /* the job of reading the ok/error packet is left to the server */
  95. return CR_OK;
  96. }
  97. mysql_declare_client_plugin(AUTHENTICATION)
  98. "qa_auth_client",
  99. "Horst Hunger",
  100. "Dialog Client Authentication Plugin",
  101. {0,1,0},
  102. "GPL",
  103. NULL,
  104. NULL,
  105. NULL,
  106. NULL,
  107. test_plugin_client
  108. mysql_end_client_plugin;