sogo-auth.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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);
  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 (isset($_SESSION['mailcow_cc_role']) &&
  36. (($_SESSION['acl']['login_as'] == "1" && $ALLOW_ADMIN_EMAIL_LOGIN !== 0) || ($is_dual === false && $login == $_SESSION['mailcow_cc_username']))) {
  37. if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
  38. if (user_get_alias_details($login) !== false) {
  39. // load master password
  40. $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  41. // register username and password in session
  42. $_SESSION[$session_var_user_allowed][] = $login;
  43. $_SESSION[$session_var_pass] = $sogo_sso_pass;
  44. // redirect to sogo (sogo will get the correct credentials via nginx auth_request
  45. header("Location: /SOGo/so/${login}");
  46. exit;
  47. }
  48. }
  49. }
  50. header('HTTP/1.0 403 Forbidden');
  51. echo "Forbidden";
  52. exit;
  53. }
  54. // only check for admin-login on sogo GUI requests
  55. elseif (
  56. strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 9), "/SOGo/so/") === 0
  57. ) {
  58. // this is an nginx auth_request call, we check for existing sogo-sso session variables
  59. session_start();
  60. // extract email address from "/SOGo/so/user@domain/xy"
  61. $url_parts = explode("/", $_SERVER['HTTP_X_ORIGINAL_URI']);
  62. $email = $url_parts[3];
  63. // check if this email is in session allowed list
  64. if (
  65. !empty($email) &&
  66. filter_var($email, FILTER_VALIDATE_EMAIL) &&
  67. is_array($_SESSION[$session_var_user_allowed]) &&
  68. in_array($email, $_SESSION[$session_var_user_allowed])
  69. ) {
  70. $username = $email;
  71. $password = $_SESSION[$session_var_pass];
  72. header("X-User: $username");
  73. header("X-Auth: Basic ".base64_encode("$username:$password"));
  74. header("X-Auth-Type: Basic");
  75. exit;
  76. }
  77. }
  78. // if username is empty, SOGo will use the normal login methods / login form
  79. header("X-User: ");
  80. header("X-Auth: ");
  81. header("X-Auth-Type: ");