ldap-auth.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <string.h>
  2. #include <mysql.h>
  3. #include <mysql/plugin_auth.h>
  4. #include <mysql/client_plugin.h>
  5. #include "interfaces.h"
  6. #include "MySQLLDAPAuthModule.h"
  7. #include "LDAPReader.h"
  8. static IMySQLLDAPAuthModule* _module;
  9. static int authenticateUser(MYSQL_PLUGIN_VIO *vio, MYSQL_SERVER_AUTH_INFO *info)
  10. {
  11. char *pkt;
  12. int pkt_len;
  13. /* read the password as null-terminated string, fail on error */
  14. if ((pkt_len= vio->read_packet(vio, (unsigned char**) &pkt)) < 0)
  15. return CR_ERROR;
  16. /* fail on empty password */
  17. if (!pkt_len || *pkt == '\0')
  18. {
  19. info->password_used= PASSWORD_USED_NO;
  20. return CR_ERROR;
  21. }
  22. info->password_used= PASSWORD_USED_YES;
  23. return _module->Authenticate(info->user_name, pkt) ? CR_OK : CR_ERROR;
  24. }
  25. static int auth_simple_client (MYSQL_PLUGIN_VIO *vio, MYSQL *mysql)
  26. {
  27. return vio->write_packet(vio, (const unsigned char *)mysql->passwd, strlen(mysql->passwd) + 1) ?
  28. CR_ERROR : CR_OK;
  29. }
  30. static struct st_mysql_auth auth_ldap_handler =
  31. {
  32. MYSQL_AUTHENTICATION_INTERFACE_VERSION,
  33. "auth_ldap",
  34. authenticateUser,
  35. NULL,
  36. NULL
  37. };
  38. int initModule(void*)
  39. {
  40. _module = new MySQLLDAPAuthModule(std::make_unique<LDAPReader>());
  41. return 0;
  42. }
  43. int destroyModule(void*)
  44. {
  45. delete _module;
  46. _module = nullptr;
  47. return 0;
  48. }
  49. mysql_declare_plugin(auth_ldap)
  50. {
  51. MYSQL_AUTHENTICATION_PLUGIN,
  52. &auth_ldap_handler,
  53. "auth_ldap",
  54. "isundil",
  55. "LDAP authentication plugin",
  56. PLUGIN_LICENSE_GPL,
  57. initModule,
  58. destroyModule,
  59. 0x0100,
  60. NULL,
  61. NULL,
  62. NULL,
  63. 0
  64. } mysql_declare_plugin_end;
  65. mysql_declare_client_plugin(AUTHENTICATION)
  66. "auth_ldap",
  67. "isundil",
  68. "LDAP Authentication plugin",
  69. {1,0,0},
  70. "GPL",
  71. NULL,
  72. NULL,
  73. NULL,
  74. NULL,
  75. auth_simple_client
  76. mysql_end_client_plugin;