ldap-auth.cpp 1.8 KB

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