sogo-auth.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. $ALLOW_ADMIN_EMAIL_LOGIN = (preg_match(
  3. "/^([yY][eE][sS]|[yY])+$/",
  4. $_ENV["ALLOW_ADMIN_EMAIL_LOGIN"]
  5. ));
  6. $session_var_user_allowed = 'sogo-sso-user-allowed';
  7. $session_var_pass = 'sogo-sso-pass';
  8. // validate credentials for basic auth requests
  9. if (isset($_SERVER['PHP_AUTH_USER'])) {
  10. // load prerequisites only when required
  11. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  12. $username = $_SERVER['PHP_AUTH_USER'];
  13. $password = $_SERVER['PHP_AUTH_PW'];
  14. // Determine service type for protocol access check
  15. $service = 'NONE';
  16. $original_uri = isset($_SERVER['HTTP_X_ORIGINAL_URI']) ? $_SERVER['HTTP_X_ORIGINAL_URI'] : '';
  17. if (preg_match('/^(\/SOGo|)\/dav.*/', $original_uri) === 1) {
  18. $service = 'DAV';
  19. }
  20. elseif (preg_match('/^(\/SOGo|)\/Microsoft-Server-ActiveSync.*/', $original_uri) === 1) {
  21. $service = 'EAS';
  22. }
  23. $login_check = check_login($username, $password, array('service' => $service));
  24. if ($login_check === 'user') {
  25. header("X-User: $username");
  26. header("X-Auth: Basic ".base64_encode("$username:$password"));
  27. header("X-Auth-Type: Basic");
  28. exit;
  29. } else {
  30. header('HTTP/1.0 401 Unauthorized');
  31. echo 'Invalid login';
  32. exit;
  33. }
  34. }
  35. // check permissions and redirect for direct GET ?login=xy requests
  36. elseif (isset($_GET['login'])) {
  37. // load prerequisites only when required
  38. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  39. // check if dual_login is active
  40. $is_dual = (!empty($_SESSION["dual-login"]["username"])) ? true : false;
  41. // check permissions (if dual_login is active, deny sso when acl is not given)
  42. $login = html_entity_decode(rawurldecode($_GET["login"]));
  43. if (isset($_SESSION['mailcow_cc_role']) &&
  44. (($_SESSION['acl']['login_as'] == "1" && $ALLOW_ADMIN_EMAIL_LOGIN !== 0) || ($is_dual === false && $login == $_SESSION['mailcow_cc_username']))) {
  45. if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
  46. if (user_get_alias_details($login) !== false) {
  47. // Block SOGo access if pending actions (2FA setup, password update)
  48. if (!empty($_SESSION['pending_tfa_setup']) || !empty($_SESSION['pending_pw_update'])) {
  49. header("Location: /");
  50. exit;
  51. }
  52. // register username in session
  53. $_SESSION[$session_var_user_allowed][] = $login;
  54. // set dual login
  55. if ($_SESSION['acl']['login_as'] == "1" && $ALLOW_ADMIN_EMAIL_LOGIN !== 0 && $is_dual === false && $_SESSION['mailcow_cc_role'] != "user"){
  56. $_SESSION["dual-login"]["username"] = $_SESSION['mailcow_cc_username'];
  57. $_SESSION["dual-login"]["role"] = $_SESSION['mailcow_cc_role'];
  58. $_SESSION['mailcow_cc_username'] = $login;
  59. $_SESSION['mailcow_cc_role'] = "user";
  60. }
  61. // update sasl logs
  62. $stmt = $pdo->prepare("REPLACE INTO sasl_log (`service`, `app_password`, `username`, `real_rip`) VALUES ('SSO', 0, :username, :remote_addr)");
  63. $stmt->execute(array(
  64. ':username' => $login,
  65. ':remote_addr' => ($_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'])
  66. ));
  67. // redirect to sogo (sogo will get the correct credentials via nginx auth_request
  68. header("Location: /SOGo/so/");
  69. exit;
  70. }
  71. }
  72. }
  73. header("Location: /");
  74. exit;
  75. }
  76. // only check for admin-login on sogo GUI requests
  77. elseif (isset($_SERVER['HTTP_X_ORIGINAL_URI']) && strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 9), "/SOGo/so/") === 0) {
  78. // this is an nginx auth_request call, we check for existing sogo-sso session variables
  79. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php';
  80. if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php')) {
  81. include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php';
  82. }
  83. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/sessions.inc.php';
  84. $email_list = array(
  85. ($_SESSION['mailcow_cc_username'] ?? ''), // Current user
  86. ($_SESSION["dual-login"]["username"] ?? ''), // Dual login user
  87. );
  88. foreach($email_list as $email) {
  89. // check if this email is in session allowed list
  90. if (
  91. !empty($email) &&
  92. filter_var($email, FILTER_VALIDATE_EMAIL) &&
  93. is_array($_SESSION[$session_var_user_allowed]) &&
  94. in_array($email, $_SESSION[$session_var_user_allowed]) &&
  95. !$_SESSION['pending_pw_update'] &&
  96. !$_SESSION['pending_tfa_setup']
  97. ) {
  98. $username = $email;
  99. $password = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  100. header("X-User: $username");
  101. header("X-Auth: Basic ".base64_encode("$username:$password"));
  102. header("X-Auth-Type: Basic");
  103. exit;
  104. }
  105. }
  106. }
  107. // if username is empty, SOGo will use the normal login methods / login form
  108. header("X-User: ");
  109. header("X-Auth: ");
  110. header("X-Auth-Type: ");