mailcowauth.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. ini_set('error_reporting', 0);
  3. header('Content-Type: application/json');
  4. $post = trim(file_get_contents('php://input'));
  5. if ($post) {
  6. $post = json_decode($post, true);
  7. }
  8. $return = array("success" => false);
  9. if(!isset($post['username']) || !isset($post['password']) || !isset($post['real_rip'])){
  10. error_log("MAILCOWAUTH: Bad Request");
  11. http_response_code(400); // Bad Request
  12. echo json_encode($return);
  13. exit();
  14. }
  15. require_once('../../../web/inc/vars.inc.php');
  16. if (file_exists('../../../web/inc/vars.local.inc.php')) {
  17. include_once('../../../web/inc/vars.local.inc.php');
  18. }
  19. require_once '../../../web/inc/lib/vendor/autoload.php';
  20. // Init database
  21. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  22. $opt = [
  23. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  24. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  25. PDO::ATTR_EMULATE_PREPARES => false,
  26. ];
  27. try {
  28. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  29. }
  30. catch (PDOException $e) {
  31. error_log("MAILCOWAUTH: " . $e . PHP_EOL);
  32. http_response_code(500); // Internal Server Error
  33. echo json_encode($return);
  34. exit;
  35. }
  36. // Load core functions first
  37. require_once 'functions.inc.php';
  38. require_once 'functions.auth.inc.php';
  39. require_once 'sessions.inc.php';
  40. require_once 'functions.mailbox.inc.php';
  41. $isSOGoRequest = $post['real_rip'] == getenv('IPV4_NETWORK') . '.248';
  42. $result = false;
  43. $protocol = $post['protocol'];
  44. if ($isSOGoRequest) {
  45. $protocol = null;
  46. // This is a SOGo Auth request. First check for SSO password.
  47. $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  48. if ($sogo_sso_pass === $post['password']){
  49. error_log('MAILCOWAUTH: SOGo SSO auth for user ' . $post['username']);
  50. $result = true;
  51. }
  52. }
  53. if ($result === false){
  54. $result = apppass_login($post['username'], $post['password'], $protocol, array(
  55. 'is_internal' => true,
  56. 'remote_addr' => $post['real_rip']
  57. ));
  58. if ($result) error_log('MAILCOWAUTH: App auth for user ' . $post['username']);
  59. }
  60. if ($result === false){
  61. $result = user_login($post['username'], $post['password'], $protocol, array('is_internal' => true));
  62. if ($result) error_log('MAILCOWAUTH: User auth for user ' . $post['username']);
  63. }
  64. if ($result) {
  65. http_response_code(200); // OK
  66. $return['success'] = true;
  67. } else {
  68. error_log("MAILCOWAUTH: Login failed for user " . $post['username']);
  69. http_response_code(401); // Unauthorized
  70. }
  71. echo json_encode($return);
  72. session_destroy();
  73. exit;