sogo-auth.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. if (!$ALLOW_ADMIN_EMAIL_LOGIN) {
  9. header('HTTP/1.0 401 Forbidden');
  10. echo "this feature is disabled";
  11. exit;
  12. }
  13. elseif (isset($_GET['login'])) {
  14. // load prerequisites only when required
  15. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  16. // check permissions
  17. if (isset($_SESSION['mailcow_cc_role']) && $_SESSION['acl']['login_as'] == "1") {
  18. $login = html_entity_decode(rawurldecode($_GET["login"]));
  19. if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
  20. if (!empty(mailbox('get', 'mailbox_details', $login))) {
  21. // load master password
  22. $sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
  23. // register username and password in session
  24. $_SESSION[$session_var_user] = $login;
  25. $_SESSION[$session_var_pass] = $sogo_sso_pass;
  26. // redirect to sogo (sogo will get the correct credentials via nginx auth_request
  27. header("Location: /SOGo/");
  28. exit;
  29. }
  30. }
  31. }
  32. header('HTTP/1.0 401 Forbidden');
  33. exit;
  34. }
  35. else {
  36. // this is an nginx auth_request call, we check for existing sogo-sso session variables
  37. session_start();
  38. if (isset($_SESSION[$session_var_user]) && filter_var($_SESSION[$session_var_user], FILTER_VALIDATE_EMAIL)) {
  39. $username = $_SESSION[$session_var_user];
  40. $password = $_SESSION[$session_var_pass];
  41. header("X-User: $username");
  42. header("X-Auth: Basic ".base64_encode("$username:$password"));
  43. header("X-Auth-Type: Basic");
  44. } else {
  45. // if username is empty, SOGo will display the normal login form
  46. header("X-User: ");
  47. header("X-Auth: ");
  48. header("X-Auth-Type: ");
  49. }
  50. exit;
  51. }