sasl_cyrus.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. -- sasl.lua v0.4
  2. -- Copyright (C) 2008-2009 Tobias Markmann
  3. --
  4. -- All rights reserved.
  5. --
  6. -- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  7. --
  8. -- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9. -- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. -- * Neither the name of Tobias Markmann nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  11. --
  12. -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13. local cyrussasl = require "cyrussasl";
  14. local log = require "util.logger".init("sasl_cyrus");
  15. local setmetatable = setmetatable
  16. local pcall = pcall
  17. local s_match, s_gmatch = string.match, string.gmatch
  18. local sasl_errstring = {
  19. -- SASL result codes --
  20. [1] = "another step is needed in authentication";
  21. [0] = "successful result";
  22. [-1] = "generic failure";
  23. [-2] = "memory shortage failure";
  24. [-3] = "overflowed buffer";
  25. [-4] = "mechanism not supported";
  26. [-5] = "bad protocol / cancel";
  27. [-6] = "can't request info until later in exchange";
  28. [-7] = "invalid parameter supplied";
  29. [-8] = "transient failure (e.g., weak key)";
  30. [-9] = "integrity check failed";
  31. [-12] = "SASL library not initialized";
  32. -- client only codes --
  33. [2] = "needs user interaction";
  34. [-10] = "server failed mutual authentication step";
  35. [-11] = "mechanism doesn't support requested feature";
  36. -- server only codes --
  37. [-13] = "authentication failure";
  38. [-14] = "authorization failure";
  39. [-15] = "mechanism too weak for this user";
  40. [-16] = "encryption needed to use mechanism";
  41. [-17] = "One time use of a plaintext password will enable requested mechanism for user";
  42. [-18] = "passphrase expired, has to be reset";
  43. [-19] = "account disabled";
  44. [-20] = "user not found";
  45. [-23] = "version mismatch with plug-in";
  46. [-24] = "remote authentication server unavailable";
  47. [-26] = "user exists, but no verifier for user";
  48. -- codes for password setting --
  49. [-21] = "passphrase locked";
  50. [-22] = "requested change was not needed";
  51. [-27] = "passphrase is too weak for security policy";
  52. [-28] = "user supplied passwords not permitted";
  53. };
  54. setmetatable(sasl_errstring, { __index = function() return "undefined error!" end });
  55. local _ENV = nil;
  56. -- luacheck: std none
  57. local method = {};
  58. method.__index = method;
  59. local initialized = false;
  60. local function init(service_name)
  61. if not initialized then
  62. local st, errmsg = pcall(cyrussasl.server_init, service_name);
  63. if st then
  64. initialized = true;
  65. else
  66. log("error", "Failed to initialize Cyrus SASL: %s", errmsg);
  67. end
  68. end
  69. end
  70. -- create a new SASL object which can be used to authenticate clients
  71. -- host_fqdn may be nil in which case gethostname() gives the value.
  72. -- For GSSAPI, this determines the hostname in the service ticket (after
  73. -- reverse DNS canonicalization, only if [libdefaults] rdns = true which
  74. -- is the default).
  75. local function new(realm, service_name, app_name, host_fqdn)
  76. init(app_name or service_name);
  77. local st, ret = pcall(cyrussasl.server_new, service_name, host_fqdn, realm, nil, nil)
  78. if not st then
  79. log("error", "Creating SASL server connection failed: %s", ret);
  80. return nil;
  81. end
  82. local sasl_i = { realm = realm, service_name = service_name, cyrus = ret };
  83. if cyrussasl.set_canon_cb then
  84. local c14n_cb = function (user)
  85. local node = s_match(user, "^([^@]+)");
  86. log("debug", "Canonicalizing username %s to %s", user, node)
  87. return node
  88. end
  89. cyrussasl.set_canon_cb(sasl_i.cyrus, c14n_cb);
  90. end
  91. cyrussasl.setssf(sasl_i.cyrus, 0, 0xffffffff)
  92. local mechanisms = {};
  93. local cyrus_mechs = cyrussasl.listmech(sasl_i.cyrus, nil, "", " ", "");
  94. for w in s_gmatch(cyrus_mechs, "[^ ]+") do
  95. mechanisms[w] = true;
  96. end
  97. sasl_i.mechs = mechanisms;
  98. return setmetatable(sasl_i, method);
  99. end
  100. -- get a fresh clone with the same realm and service name
  101. function method:clean_clone()
  102. return new(self.realm, self.service_name)
  103. end
  104. -- get a list of possible SASL mechanims to use
  105. function method:mechanisms()
  106. return self.mechs;
  107. end
  108. -- select a mechanism to use
  109. function method:select(mechanism)
  110. if not self.selected and self.mechs[mechanism] then
  111. self.selected = mechanism;
  112. return true;
  113. end
  114. end
  115. -- feed new messages to process into the library
  116. function method:process(message)
  117. local err;
  118. local data;
  119. if not self.first_step_done then
  120. err, data = cyrussasl.server_start(self.cyrus, self.selected, message or "")
  121. self.first_step_done = true;
  122. else
  123. err, data = cyrussasl.server_step(self.cyrus, message or "")
  124. end
  125. self.username = cyrussasl.get_username(self.cyrus)
  126. if (err == 0) then -- SASL_OK
  127. if self.require_provisioning and not self.require_provisioning(self.username) then
  128. return "failure", "not-authorized", "User authenticated successfully, but not provisioned for XMPP";
  129. end
  130. return "success", data
  131. elseif (err == 1) then -- SASL_CONTINUE
  132. return "challenge", data
  133. elseif (err == -4) then -- SASL_NOMECH
  134. log("debug", "SASL mechanism not available from remote end")
  135. return "failure", "invalid-mechanism", "SASL mechanism not available"
  136. elseif (err == -13) then -- SASL_BADAUTH
  137. return "failure", "not-authorized", sasl_errstring[err];
  138. else
  139. log("debug", "Got SASL error condition %d: %s", err, sasl_errstring[err]);
  140. return "failure", "undefined-condition", sasl_errstring[err];
  141. end
  142. end
  143. return {
  144. new = new;
  145. };