sogo-auth.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $is_eas = preg_match('/^(\/SOGo|)\/(dav|Microsoft-Server-ActiveSync).*/', $_SERVER['HTTP_X_ORIGINAL_URI']);
  15. $login_check = check_login($username, $password, $is_eas && $ALLOW_APP_PASSWORDS_IN_EAS);
  16. if ($login_check === 'user') {
  17. header("X-User: $username");
  18. header("X-Auth: Basic ".base64_encode("$username:$password"));
  19. header("X-Auth-Type: Basic");
  20. exit;
  21. } else {
  22. header('HTTP/1.0 401 Unauthorized');
  23. echo 'Invalid login';
  24. exit;
  25. }
  26. }
  27. // check permissions and redirect for direct GET ?login=xy requests
  28. elseif (isset($_GET['login'])) {
  29. // load prerequisites only when required
  30. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  31. // check if dual_login is active
  32. $is_dual = (!empty($_SESSION["dual-login"]["username"])) ? true : false;
  33. // check permissions (if dual_login is active, deny sso when acl is not given)
  34. $login = html_entity_decode(rawurldecode($_GET["login"]));
  35. if ($ALLOW_ADMIN_EMAIL_LOGIN === 0 && $is_dual === true) {
  36. header('HTTP/1.0 403 Forbidden');
  37. echo "Admin login is forbidden";
  38. exit;
  39. }
  40. if (isset($_SESSION['mailcow_cc_role']) &&
  41. ($_SESSION['acl']['login_as'] == "1" || ($is_dual === false && $login == $_SESSION['mailcow_cc_username']))) {
  42. if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
  43. if (user_get_alias_details($login) !== false) {
  44. // load master password
  45. $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  46. // register username and password in session
  47. $_SESSION[$session_var_user_allowed][] = $login;
  48. $_SESSION[$session_var_pass] = $sogo_sso_pass;
  49. // redirect to sogo (sogo will get the correct credentials via nginx auth_request
  50. header("Location: /SOGo/so/${login}");
  51. exit;
  52. }
  53. }
  54. }
  55. header('HTTP/1.0 403 Forbidden');
  56. echo "Access is forbidden";
  57. exit;
  58. }
  59. // only check for admin-login on sogo GUI requests
  60. elseif (
  61. strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 9), "/SOGo/so/") === 0
  62. ) {
  63. // this is an nginx auth_request call, we check for existing sogo-sso session variables
  64. session_start();
  65. // extract email address from "/SOGo/so/user@domain/xy"
  66. $url_parts = explode("/", $_SERVER['HTTP_X_ORIGINAL_URI']);
  67. $email = $url_parts[3];
  68. // check if this email is in session allowed list
  69. if (
  70. !empty($email) &&
  71. filter_var($email, FILTER_VALIDATE_EMAIL) &&
  72. is_array($_SESSION[$session_var_user_allowed]) &&
  73. in_array($email, $_SESSION[$session_var_user_allowed])
  74. ) {
  75. $username = $email;
  76. $password = $_SESSION[$session_var_pass];
  77. header("X-User: $username");
  78. header("X-Auth: Basic ".base64_encode("$username:$password"));
  79. header("X-Auth-Type: Basic");
  80. exit;
  81. }
  82. }
  83. // if username is empty, SOGo will use the normal login methods / login form
  84. header("X-User: ");
  85. header("X-Auth: ");
  86. header("X-Auth-Type: ");