sogo-auth.php 3.0 KB

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