mailcowauth.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Redis
  21. $redis = new Redis();
  22. try {
  23. if (!empty(getenv('REDIS_SLAVEOF_IP'))) {
  24. $redis->connect(getenv('REDIS_SLAVEOF_IP'), getenv('REDIS_SLAVEOF_PORT'));
  25. }
  26. else {
  27. $redis->connect('redis-mailcow', 6379);
  28. }
  29. $redis->auth(getenv("REDISPASS"));
  30. }
  31. catch (Exception $e) {
  32. error_log("MAILCOWAUTH: " . $e . PHP_EOL);
  33. http_response_code(500); // Internal Server Error
  34. echo json_encode($return);
  35. exit;
  36. }
  37. // Init database
  38. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  39. $opt = [
  40. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  41. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  42. PDO::ATTR_EMULATE_PREPARES => false,
  43. ];
  44. try {
  45. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  46. }
  47. catch (PDOException $e) {
  48. error_log("MAILCOWAUTH: " . $e . PHP_EOL);
  49. http_response_code(500); // Internal Server Error
  50. echo json_encode($return);
  51. exit;
  52. }
  53. // Load core functions first
  54. require_once 'functions.inc.php';
  55. require_once 'functions.auth.inc.php';
  56. require_once 'sessions.inc.php';
  57. require_once 'functions.mailbox.inc.php';
  58. require_once 'functions.ratelimit.inc.php';
  59. require_once 'functions.acl.inc.php';
  60. $isSOGoRequest = $post['real_rip'] == getenv('IPV4_NETWORK') . '.248';
  61. $result = false;
  62. $protocol = $post['protocol'];
  63. if ($isSOGoRequest) {
  64. $protocol = null;
  65. // This is a SOGo Auth request. First check for SSO password.
  66. $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  67. if ($sogo_sso_pass === $post['password']){
  68. error_log('MAILCOWAUTH: SOGo SSO auth for user ' . $post['username']);
  69. $result = true;
  70. }
  71. }
  72. if ($result === false){
  73. $result = apppass_login($post['username'], $post['password'], $protocol, array(
  74. 'is_internal' => true,
  75. 'remote_addr' => $post['real_rip']
  76. ));
  77. if ($result) error_log('MAILCOWAUTH: App auth for user ' . $post['username']);
  78. }
  79. if ($result === false){
  80. // Init Identity Provider
  81. $iam_provider = identity_provider('init');
  82. $iam_settings = identity_provider('get');
  83. $result = user_login($post['username'], $post['password'], array('is_internal' => true));
  84. if ($result) error_log('MAILCOWAUTH: User auth for user ' . $post['username']);
  85. }
  86. if ($result) {
  87. http_response_code(200); // OK
  88. $return['success'] = true;
  89. } else {
  90. error_log("MAILCOWAUTH: Login failed for user " . $post['username']);
  91. http_response_code(401); // Unauthorized
  92. }
  93. echo json_encode($return);
  94. session_destroy();
  95. exit;