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