sogo-auth.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';
  13. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.auth.inc.php';
  14. $username = $_SERVER['PHP_AUTH_USER'];
  15. $password = $_SERVER['PHP_AUTH_PW'];
  16. $is_eas = false;
  17. $is_dav = false;
  18. $original_uri = isset($_SERVER['HTTP_X_ORIGINAL_URI']) ? $_SERVER['HTTP_X_ORIGINAL_URI'] : '';
  19. if (preg_match('/^(\/SOGo|)\/dav.*/', $original_uri) === 1) {
  20. $is_dav = true;
  21. }
  22. elseif (preg_match('/^(\/SOGo|)\/Microsoft-Server-ActiveSync.*/', $original_uri) === 1) {
  23. $is_eas = true;
  24. }
  25. $login_check = check_login($username, $password, array('dav' => $is_dav, 'eas' => $is_eas));
  26. if ($login_check === 'user') {
  27. header("X-User: $username");
  28. header("X-Auth: Basic ".base64_encode("$username:$password"));
  29. header("X-Auth-Type: Basic");
  30. exit;
  31. } else {
  32. header('HTTP/1.0 401 Unauthorized');
  33. echo 'Invalid login';
  34. exit;
  35. }
  36. }
  37. // check permissions and redirect for direct GET ?login=xy requests
  38. elseif (isset($_GET['login'])) {
  39. // load prerequisites only when required
  40. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  41. // check if dual_login is active
  42. $is_dual = (!empty($_SESSION["dual-login"]["username"])) ? true : false;
  43. // check permissions (if dual_login is active, deny sso when acl is not given)
  44. $login = html_entity_decode(rawurldecode($_GET["login"]));
  45. if (isset($_SESSION['mailcow_cc_role']) &&
  46. (($_SESSION['acl']['login_as'] == "1" && $ALLOW_ADMIN_EMAIL_LOGIN !== 0) || ($is_dual === false && $login == $_SESSION['mailcow_cc_username']))) {
  47. if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
  48. if (user_get_alias_details($login) !== false) {
  49. // load master password
  50. $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  51. // register username and password in session
  52. $_SESSION[$session_var_user_allowed][] = $login;
  53. $_SESSION[$session_var_pass] = $sogo_sso_pass;
  54. // update sasl logs
  55. $service = ($app_passwd_data['eas'] === true) ? 'EAS' : 'DAV';
  56. $stmt = $pdo->prepare("REPLACE INTO sasl_log (`service`, `app_password`, `username`, `real_rip`) VALUES ('SSO', 0, :username, :remote_addr)");
  57. $stmt->execute(array(
  58. ':username' => $login,
  59. ':remote_addr' => ($_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'])
  60. ));
  61. // redirect to sogo (sogo will get the correct credentials via nginx auth_request
  62. header("Location: /SOGo/so/{$login}");
  63. exit;
  64. }
  65. }
  66. }
  67. header("Location: /SOGo/");
  68. exit;
  69. }
  70. // only check for admin-login on sogo GUI requests
  71. elseif (isset($_SERVER['HTTP_X_ORIGINAL_URI']) && strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 9), "/SOGo/so/") === 0) {
  72. // this is an nginx auth_request call, we check for existing sogo-sso session variables
  73. session_start();
  74. // extract email address from "/SOGo/so/user@domain/xy"
  75. $url_parts = explode("/", $_SERVER['HTTP_X_ORIGINAL_URI']);
  76. $email_list = array(
  77. $url_parts[3], // Requested mailbox
  78. ($_SESSION['mailcow_cc_username'] ?? ''), // Current user
  79. ($_SESSION["dual-login"]["username"] ?? ''), // Dual login user
  80. );
  81. foreach($email_list as $email) {
  82. // check if this email is in session allowed list
  83. if (
  84. !empty($email) &&
  85. filter_var($email, FILTER_VALIDATE_EMAIL) &&
  86. is_array($_SESSION[$session_var_user_allowed]) &&
  87. in_array($email, $_SESSION[$session_var_user_allowed])
  88. ) {
  89. $username = $email;
  90. $password = $_SESSION[$session_var_pass];
  91. header("X-User: $username");
  92. header("X-Auth: Basic ".base64_encode("$username:$password"));
  93. header("X-Auth-Type: Basic");
  94. exit;
  95. }
  96. }
  97. }
  98. // if username is empty, SOGo will use the normal login methods / login form
  99. header("X-User: ");
  100. header("X-Auth: ");
  101. header("X-Auth-Type: ");