triggers.user.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // handle iam authentication
  3. if ($iam_provider){
  4. if (isset($_GET['iam_sso'])){
  5. // redirect for sso
  6. $redirect_uri = identity_provider('get-redirect');
  7. $redirect_uri = !empty($redirect_uri) ? $redirect_uri : '/';
  8. header('Location: ' . $redirect_uri);
  9. die();
  10. }
  11. if ($_SESSION['iam_token'] && $_SESSION['iam_refresh_token']) {
  12. // Session found, try to refresh
  13. $isRefreshed = identity_provider('refresh-token');
  14. if (!$isRefreshed){
  15. // Session could not be refreshed, redirect to provider
  16. $redirect_uri = identity_provider('get-redirect');
  17. $redirect_uri = !empty($redirect_uri) ? $redirect_uri : '/';
  18. header('Location: ' . $redirect_uri);
  19. die();
  20. }
  21. } elseif ($_GET['code'] && $_GET['state'] === $_SESSION['oauth2state']) {
  22. // Check given state against previously stored one to mitigate CSRF attack
  23. // Received access token in $_GET['code']
  24. // extract info and verify user
  25. identity_provider('verify-sso');
  26. }
  27. }
  28. if (isset($_POST["pw_reset_request"]) && !empty($_POST['username'])) {
  29. reset_password("issue", $_POST['username']);
  30. header("Location: /");
  31. exit;
  32. }
  33. if (isset($_POST["pw_reset"])) {
  34. $username = reset_password("check", $_POST['token']);
  35. $reset_result = reset_password("reset", array(
  36. 'new_password' => $_POST['new_password'],
  37. 'new_password2' => $_POST['new_password2'],
  38. 'token' => $_POST['token'],
  39. 'username' => $username,
  40. 'check_tfa' => True
  41. ));
  42. if ($reset_result){
  43. header("Location: /");
  44. exit;
  45. }
  46. }
  47. if (isset($_POST["verify_tfa_login"])) {
  48. if (verify_tfa_login($_SESSION['pending_mailcow_cc_username'], $_POST)) {
  49. if ($_SESSION['pending_mailcow_cc_role'] == "user") {
  50. if (isset($_SESSION['pending_pw_reset_token']) && isset($_SESSION['pending_pw_new_password'])) {
  51. reset_password("reset", array(
  52. 'new_password' => $_SESSION['pending_pw_new_password'],
  53. 'new_password2' => $_SESSION['pending_pw_new_password'],
  54. 'token' => $_SESSION['pending_pw_reset_token'],
  55. 'username' => $_SESSION['pending_mailcow_cc_username']
  56. ));
  57. unset($_SESSION['pending_pw_reset_token']);
  58. unset($_SESSION['pending_pw_new_password']);
  59. unset($_SESSION['pending_mailcow_cc_username']);
  60. unset($_SESSION['pending_tfa_methods']);
  61. header("Location: /");
  62. die();
  63. } else {
  64. set_user_loggedin_session($_SESSION['pending_mailcow_cc_username']);
  65. if (isset($_SESSION['oauth2_request'])) {
  66. $oauth2_request = $_SESSION['oauth2_request'];
  67. unset($_SESSION['oauth2_request']);
  68. header('Location: ' . $oauth2_request);
  69. die();
  70. }
  71. $user_details = mailbox("get", "mailbox_details", $_SESSION['mailcow_cc_username']);
  72. $is_dual = (!empty($_SESSION["dual-login"]["username"])) ? true : false;
  73. // If pending actions exist, redirect to / to show modal
  74. if (!empty($_SESSION['pending_tfa_setup']) || !empty($_SESSION['pending_pw_update'])) {
  75. header("Location: /");
  76. die();
  77. }
  78. if (intval($user_details['attributes']['sogo_access']) == 1 &&
  79. intval($user_details['attributes']['force_pw_update']) != 1 &&
  80. getenv('SKIP_SOGO') != "y" &&
  81. !$is_dual) {
  82. header("Location: /SOGo/so/");
  83. die();
  84. } else {
  85. header("Location: /user");
  86. die();
  87. }
  88. }
  89. }
  90. }
  91. unset($_SESSION['pending_mailcow_cc_username']);
  92. unset($_SESSION['pending_mailcow_cc_role']);
  93. unset($_SESSION['pending_tfa_methods']);
  94. }
  95. if (isset($_POST["verify_fido2_login"])) {
  96. $res = fido2(array(
  97. "action" => "verify",
  98. "token" => $_POST["token"],
  99. "user" => "user"
  100. ));
  101. if (is_array($res) && $res['role'] == "user" && !empty($res['username'])){
  102. set_user_loggedin_session($res['username']);
  103. $_SESSION["fido2_cid"] = $res['cid'];
  104. }
  105. exit;
  106. }
  107. if (isset($_GET["cancel_tfa_login"])) {
  108. unset($_SESSION['pending_pw_reset_token']);
  109. unset($_SESSION['pending_pw_new_password']);
  110. unset($_SESSION['pending_mailcow_cc_username']);
  111. unset($_SESSION['pending_mailcow_cc_role']);
  112. unset($_SESSION['pending_tfa_methods']);
  113. header("Location: /");
  114. }
  115. if (isset($_GET["cancel_tfa_setup"])) {
  116. session_regenerate_id(true);
  117. session_unset();
  118. session_destroy();
  119. session_write_close();
  120. header("Location: /");
  121. exit();
  122. }
  123. if (isset($_POST["login_user"]) && isset($_POST["pass_user"])) {
  124. $login_user = strtolower(trim($_POST["login_user"]));
  125. $as = check_login($login_user, $_POST["pass_user"], array("role" => "user", "service" => "MAILCOWUI"));
  126. if ($as == "user") {
  127. set_user_loggedin_session($login_user);
  128. $http_parameters = explode('&', $_SESSION['index_query_string']);
  129. unset($_SESSION['index_query_string']);
  130. if (in_array('mobileconfig', $http_parameters)) {
  131. if (in_array('only_email', $http_parameters)) {
  132. header("Location: /mobileconfig.php?only_email");
  133. die();
  134. }
  135. header("Location: /mobileconfig.php");
  136. die();
  137. }
  138. if (isset($_SESSION['oauth2_request'])) {
  139. $oauth2_request = $_SESSION['oauth2_request'];
  140. unset($_SESSION['oauth2_request']);
  141. header('Location: ' . $oauth2_request);
  142. die();
  143. }
  144. $user_details = mailbox("get", "mailbox_details", $login_user);
  145. $is_dual = (!empty($_SESSION["dual-login"]["username"])) ? true : false;
  146. // If pending actions exist, redirect to / to show modal
  147. if (!empty($_SESSION['pending_tfa_setup']) || !empty($_SESSION['pending_pw_update'])) {
  148. header("Location: /");
  149. die();
  150. }
  151. if (intval($user_details['attributes']['sogo_access']) == 1 &&
  152. intval($user_details['attributes']['force_pw_update']) != 1 &&
  153. getenv('SKIP_SOGO') != "y" &&
  154. !$is_dual) {
  155. header("Location: /SOGo/so/");
  156. die();
  157. } else {
  158. header("Location: /user");
  159. die();
  160. }
  161. }
  162. elseif ($as != "pending") {
  163. unset($_SESSION['pending_mailcow_cc_username']);
  164. unset($_SESSION['pending_mailcow_cc_role']);
  165. unset($_SESSION['pending_tfa_methods']);
  166. unset($_SESSION['mailcow_cc_username']);
  167. unset($_SESSION['mailcow_cc_role']);
  168. }
  169. }
  170. ?>