mailcowCommandExecutor.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php namespace LeeSherwood\Ejabberd\CommandExecutors;
  2. /**
  3. * Dummy command executor for ejabberd
  4. *
  5. * This class implements the command executor interface and just returns some sane boolean values.
  6. * You may want to use this class to test your ejabberd external authentication module is set up correctly
  7. * before you start creating your custom code.
  8. *
  9. * @package LeeSherwood\Ejabberd
  10. * @author Lee Sherwood
  11. */
  12. use \PDO;
  13. class mailcowCommandExecutor implements CommandExecutorInterface {
  14. /**
  15. * Authenticate a user (login)
  16. *
  17. * @param string $username
  18. * @param string $servername
  19. * @param string $password
  20. *
  21. * @return bool
  22. */
  23. public static function verify_salted_hash($hash, $password, $algo, $salt_length) {
  24. // Decode hash
  25. $dhash = base64_decode($hash);
  26. // Get first n bytes of binary which equals a SSHA hash
  27. $ohash = substr($dhash, 0, $salt_length);
  28. // Remove SSHA hash from decoded hash to get original salt string
  29. $osalt = str_replace($ohash, '', $dhash);
  30. // Check single salted SSHA hash against extracted hash
  31. if (hash_equals(hash($algo, $password . $osalt, true), $ohash)) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. public static function verify_hash($hash, $password) {
  37. if (preg_match('/^{(.+)}(.+)/i', $hash, $hash_array)) {
  38. $scheme = strtoupper($hash_array[1]);
  39. $hash = $hash_array[2];
  40. switch ($scheme) {
  41. case "ARGON2I":
  42. case "ARGON2ID":
  43. case "BLF-CRYPT":
  44. case "CRYPT":
  45. case "DES-CRYPT":
  46. case "MD5-CRYPT":
  47. case "MD5":
  48. case "SHA256-CRYPT":
  49. case "SHA512-CRYPT":
  50. return password_verify($password, $hash);
  51. case "CLEAR":
  52. case "CLEARTEXT":
  53. case "PLAIN":
  54. return $password == $hash;
  55. case "LDAP-MD5":
  56. $hash = base64_decode($hash);
  57. return hash_equals(hash('md5', $password, true), $hash);
  58. case "PBKDF2":
  59. $components = explode('$', $hash);
  60. $salt = $components[2];
  61. $rounds = $components[3];
  62. $hash = $components[4];
  63. return hash_equals(hash_pbkdf2('sha1', $password, $salt, $rounds), $hash);
  64. case "PLAIN-MD4":
  65. return hash_equals(hash('md4', $password), $hash);
  66. case "PLAIN-MD5":
  67. return md5($password) == $hash;
  68. case "PLAIN-TRUNC":
  69. $components = explode('-', $hash);
  70. if (count($components) > 1) {
  71. $trunc_len = $components[0];
  72. $trunc_password = $components[1];
  73. return substr($password, 0, $trunc_len) == $trunc_password;
  74. } else {
  75. return $password == $hash;
  76. }
  77. case "SHA":
  78. case "SHA1":
  79. case "SHA256":
  80. case "SHA512":
  81. // SHA is an alias for SHA1
  82. $scheme = $scheme == "SHA" ? "sha1" : strtolower($scheme);
  83. $hash = base64_decode($hash);
  84. return hash_equals(hash($scheme, $password, true), $hash);
  85. case "SMD5":
  86. return self::verify_salted_hash($hash, $password, 'md5', 16);
  87. case "SSHA":
  88. return self::verify_salted_hash($hash, $password, 'sha1', 20);
  89. case "SSHA256":
  90. return self::verify_salted_hash($hash, $password, 'sha256', 32);
  91. case "SSHA512":
  92. return self::verify_salted_hash($hash, $password, 'sha512', 64);
  93. default:
  94. return false;
  95. }
  96. }
  97. return false;
  98. }
  99. public function authenticate($username, $servername, $password)
  100. {
  101. $database_type = 'mysql';
  102. $database_sock = '/var/run/mysqld/mysqld.sock';
  103. $database_user = '__DBUSER__';
  104. $database_pass = '__DBPASS__';
  105. $database_name = '__DBNAME__';
  106. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  107. $opt = [
  108. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  109. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  110. PDO::ATTR_EMULATE_PREPARES => false
  111. ];
  112. try {
  113. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  114. }
  115. catch (PDOException $e) {
  116. return false;
  117. }
  118. if (!filter_var($username, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $username))) {
  119. return false;
  120. }
  121. $username = strtolower(trim($username));
  122. $stmt = $pdo->prepare("SELECT `password` FROM `mailbox`
  123. INNER JOIN domain on mailbox.domain = domain.domain
  124. WHERE `kind` NOT REGEXP 'location|thing|group'
  125. AND `mailbox`.`active`= '1'
  126. AND `domain`.`active`= '1'
  127. AND `domain`.`xmpp` = '1'
  128. AND JSON_UNQUOTE(JSON_VALUE(`mailbox`.`attributes`, '$.xmpp_access')) = '1'
  129. AND CONCAT(`domain`.`xmpp_prefix`, '.', `domain`.`domain`) = :servername
  130. AND `username` = CONCAT(:local_part, '@', `domain`.`domain`)");
  131. $stmt->execute(array(':local_part' => $username, ':servername' => $servername));
  132. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  133. foreach ($rows as $row) {
  134. if (self::verify_hash($row['password'], $password) !== false) {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * Check if a user exists
  142. *
  143. * @param string $username
  144. * @param string $servername
  145. *
  146. * @return bool
  147. */
  148. public function userExists($username, $servername)
  149. {
  150. return true;
  151. }
  152. /**
  153. * Set a password for a user
  154. *
  155. * @param string $username
  156. * @param string $servername
  157. * @param string $password
  158. *
  159. * @return bool
  160. */
  161. public function setPassword($username, $servername, $password)
  162. {
  163. return false;
  164. }
  165. /**
  166. * Register a user
  167. *
  168. * @param string $username
  169. * @param string $servername
  170. * @param string $password
  171. *
  172. * @return bool
  173. */
  174. public function register($username, $servername, $password)
  175. {
  176. return false;
  177. }
  178. /**
  179. * Delete a user
  180. *
  181. * @param string $username
  182. * @param string $servername
  183. *
  184. * @return bool
  185. */
  186. public function removeUser($username, $servername)
  187. {
  188. return false;
  189. }
  190. /**
  191. * Delete a user with password validation
  192. *
  193. * @param string $username
  194. * @param string $servername
  195. * @param string $password
  196. *
  197. * @return bool
  198. */
  199. public function removeUserWithPassword($username, $servername, $password)
  200. {
  201. return false;
  202. }
  203. }