sogo-auth.php 4.1 KB

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