mod_auth_cyrus.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. -- Prosody IM
  2. -- Copyright (C) 2008-2010 Matthew Wild
  3. -- Copyright (C) 2008-2010 Waqas Hussain
  4. --
  5. -- This project is MIT/X11 licensed. Please see the
  6. -- COPYING file in the source package for more information.
  7. --
  8. -- luacheck: ignore 212
  9. local log = require "util.logger".init("auth_cyrus");
  10. local usermanager_user_exists = require "core.usermanager".user_exists;
  11. local cyrus_service_realm = module:get_option("cyrus_service_realm");
  12. local cyrus_service_name = module:get_option("cyrus_service_name");
  13. local cyrus_application_name = module:get_option("cyrus_application_name");
  14. local require_provisioning = module:get_option("cyrus_require_provisioning") or false;
  15. local host_fqdn = module:get_option("cyrus_server_fqdn");
  16. prosody.unlock_globals(); --FIXME: Figure out why this is needed and
  17. -- why cyrussasl isn't caught by the sandbox
  18. local cyrus_new = module:require "sasl_cyrus".new;
  19. prosody.lock_globals();
  20. local new_sasl = function(realm)
  21. return cyrus_new(
  22. cyrus_service_realm or realm,
  23. cyrus_service_name or "xmpp",
  24. cyrus_application_name or "prosody",
  25. host_fqdn
  26. );
  27. end
  28. do -- diagnostic
  29. local list;
  30. for mechanism in pairs(new_sasl(module.host):mechanisms()) do
  31. list = (not(list) and mechanism) or (list..", "..mechanism);
  32. end
  33. if not list then
  34. module:log("error", "No Cyrus SASL mechanisms available");
  35. else
  36. module:log("debug", "Available Cyrus SASL mechanisms: %s", list);
  37. end
  38. end
  39. local host = module.host;
  40. -- define auth provider
  41. local provider = {};
  42. log("debug", "initializing default authentication provider for host '%s'", host);
  43. function provider.test_password(username, password)
  44. return nil, "Legacy auth not supported with Cyrus SASL.";
  45. end
  46. function provider.get_password(username)
  47. return nil, "Passwords unavailable for Cyrus SASL.";
  48. end
  49. function provider.set_password(username, password)
  50. return nil, "Passwords unavailable for Cyrus SASL.";
  51. end
  52. function provider.user_exists(username)
  53. if require_provisioning then
  54. return usermanager_user_exists(username, host);
  55. end
  56. return true;
  57. end
  58. function provider.create_user(username, password)
  59. return nil, "Account creation/modification not available with Cyrus SASL.";
  60. end
  61. function provider.get_sasl_handler()
  62. local handler = new_sasl(host);
  63. if require_provisioning then
  64. function handler.require_provisioning(username)
  65. return usermanager_user_exists(username, host);
  66. end
  67. end
  68. return handler;
  69. end
  70. module:provides("auth", provider);