sogo-auth.php 3.2 KB

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