sogo-auth.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 = 'sogo-sso-user';
  7. $session_var_pass = 'sogo-sso-pass';
  8. // prevent if feature is disabled
  9. if (!$ALLOW_ADMIN_EMAIL_LOGIN) {
  10. header('HTTP/1.0 403 Forbidden');
  11. echo "this feature is disabled";
  12. exit;
  13. }
  14. // validate credentials for basic auth requests
  15. elseif (isset($_SERVER['PHP_AUTH_USER'])) {
  16. // load prerequisites only when required
  17. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  18. $username = $_SERVER['PHP_AUTH_USER'];
  19. $password = $_SERVER['PHP_AUTH_PW'];
  20. $login_check = check_login($username, $password);
  21. if ($login_check === 'user') {
  22. header("X-User: $username");
  23. header("X-Auth: Basic ".base64_encode("$username:$password"));
  24. header("X-Auth-Type: Basic");
  25. exit;
  26. } else {
  27. header('HTTP/1.0 401 Unauthorized');
  28. echo 'Invalid login';
  29. exit;
  30. }
  31. }
  32. // check permissions and redirect for direct GET ?login=xy requests
  33. elseif (isset($_GET['login'])) {
  34. // load prerequisites only when required
  35. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  36. // check permissions
  37. if (isset($_SESSION['mailcow_cc_role']) && $_SESSION['acl']['login_as'] == "1") {
  38. $login = html_entity_decode(rawurldecode($_GET["login"]));
  39. if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
  40. if (!empty(mailbox('get', 'mailbox_details', $login))) {
  41. // load master password
  42. $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  43. // register username and password in session
  44. $_SESSION[$session_var_user] = $login;
  45. $_SESSION[$session_var_pass] = $sogo_sso_pass;
  46. // redirect to sogo (sogo will get the correct credentials via nginx auth_request
  47. header("Location: /SOGo/");
  48. exit;
  49. }
  50. }
  51. }
  52. header('HTTP/1.0 403 Forbidden');
  53. exit;
  54. }
  55. // do not check for admin-login / sogo-sso for EAS and DAV requests, SOGo can check auth itself if no authorization header is set
  56. elseif (
  57. strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 28), "/Microsoft-Server-ActiveSync") !== 0 &&
  58. strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 9), "/SOGo/dav") !== 0
  59. ) {
  60. // this is an nginx auth_request call, we check for existing sogo-sso session variables
  61. session_start();
  62. if (isset($_SESSION[$session_var_user]) && filter_var($_SESSION[$session_var_user], FILTER_VALIDATE_EMAIL)) {
  63. $username = $_SESSION[$session_var_user];
  64. $password = $_SESSION[$session_var_pass];
  65. header("X-User: $username");
  66. header("X-Auth: Basic ".base64_encode("$username:$password"));
  67. header("X-Auth-Type: Basic");
  68. } else {
  69. // if username is empty, SOGo will display the normal login form
  70. header("X-User: ");
  71. header("X-Auth: ");
  72. header("X-Auth-Type: ");
  73. }
  74. exit;
  75. }