| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 1 | <?php |
| 2 | // Start session |
| 3 | if (session_status() !== PHP_SESSION_ACTIVE) { |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 4 | session_name($SESSION_NAME); |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 5 | ini_set("session.cookie_httponly", 1); |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 6 | ini_set("session.cookie_samesite", $SESSION_SAMESITE_POLICY); |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 7 | ini_set('session.gc_maxlifetime', $SESSION_LIFETIME); |
| 8 | } |
| 9 | |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 10 | $_SESSION['access_all_exception'] = '0'; |
| 11 | |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 12 | if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && |
| 13 | strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == "https") { |
| 14 | if (session_status() !== PHP_SESSION_ACTIVE) { |
| 15 | ini_set("session.cookie_secure", 1); |
| 16 | } |
| 17 | $IS_HTTPS = true; |
| 18 | } |
| 19 | elseif (isset($_SERVER['HTTPS'])) { |
| 20 | if (session_status() !== PHP_SESSION_ACTIVE) { |
| 21 | ini_set("session.cookie_secure", 1); |
| 22 | } |
| 23 | $IS_HTTPS = true; |
| 24 | } |
| 25 | else { |
| 26 | $IS_HTTPS = false; |
| 27 | } |
| 28 | |
| 29 | if (session_status() !== PHP_SESSION_ACTIVE) { |
| 30 | session_start(); |
| 31 | } |
| 32 | |
| 33 | if (!isset($_SESSION['CSRF']['TOKEN'])) { |
| 34 | $_SESSION['CSRF']['TOKEN'] = bin2hex(random_bytes(32)); |
| 35 | } |
| 36 | |
| 37 | // Set session UA |
| 38 | if (!isset($_SESSION['SESS_REMOTE_UA'])) { |
| 39 | $_SESSION['SESS_REMOTE_UA'] = $_SERVER['HTTP_USER_AGENT']; |
| 40 | } |
| 41 | |
| 42 | // Keep session active |
| 43 | if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > $SESSION_LIFETIME)) { |
| 44 | session_unset(); |
| 45 | session_destroy(); |
| 46 | } |
| 47 | $_SESSION['LAST_ACTIVITY'] = time(); |
| 48 | |
| 49 | // API |
| 50 | if (!empty($_SERVER['HTTP_X_API_KEY'])) { |
| 51 | $stmt = $pdo->prepare("SELECT * FROM `api` WHERE `api_key` = :api_key AND `active` = '1';"); |
| 52 | $stmt->execute(array( |
| 53 | ':api_key' => preg_replace('/[^a-zA-Z0-9-]/', '', $_SERVER['HTTP_X_API_KEY']) |
| 54 | )); |
| 55 | $api_return = $stmt->fetch(PDO::FETCH_ASSOC); |
| 56 | if (!empty($api_return['api_key'])) { |
| 57 | $skip_ip_check = ($api_return['skip_ip_check'] == 1); |
| 58 | $remote = get_remote_ip(false); |
| 59 | $allow_from = array_map('trim', preg_split( "/( |,|;|\n)/", $api_return['allow_from'])); |
| 60 | if ($skip_ip_check === true || ip_acl($remote, $allow_from)) { |
| 61 | $_SESSION['mailcow_cc_username'] = 'API'; |
| 62 | $_SESSION['mailcow_cc_role'] = 'admin'; |
| 63 | $_SESSION['mailcow_cc_api'] = true; |
| 64 | if ($api_return['access'] == 'rw') { |
| 65 | $_SESSION['mailcow_cc_api_access'] = 'rw'; |
| 66 | } |
| 67 | else { |
| 68 | $_SESSION['mailcow_cc_api_access'] = 'ro'; |
| 69 | } |
| 70 | } |
| 71 | else { |
| 72 | $redis->publish("F2B_CHANNEL", "mailcow UI: Invalid password for API_USER by " . $_SERVER['REMOTE_ADDR']); |
| 73 | error_log("mailcow UI: Invalid password for " . $user . " by " . $_SERVER['REMOTE_ADDR']); |
| 74 | http_response_code(401); |
| 75 | echo json_encode(array( |
| 76 | 'type' => 'error', |
| 77 | 'msg' => 'api access denied for ip ' . $_SERVER['REMOTE_ADDR'] |
| 78 | )); |
| 79 | unset($_POST); |
| 80 | exit(); |
| 81 | } |
| 82 | } |
| 83 | else { |
| 84 | $redis->publish("F2B_CHANNEL", "mailcow UI: Invalid password for API_USER by " . $_SERVER['REMOTE_ADDR']); |
| 85 | error_log("mailcow UI: Invalid password for " . $user . " by " . $_SERVER['REMOTE_ADDR']); |
| 86 | http_response_code(401); |
| 87 | echo json_encode(array( |
| 88 | 'type' => 'error', |
| 89 | 'msg' => 'authentication failed' |
| 90 | )); |
| 91 | unset($_POST); |
| 92 | exit(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Handle logouts |
| 97 | if (isset($_POST["logout"])) { |
| 98 | if (isset($_SESSION["dual-login"])) { |
| 99 | $_SESSION["mailcow_cc_username"] = $_SESSION["dual-login"]["username"]; |
| 100 | $_SESSION["mailcow_cc_role"] = $_SESSION["dual-login"]["role"]; |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 101 | unset($_SESSION['sogo-sso-user-allowed']); |
| 102 | unset($_SESSION['sogo-sso-pass']); |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 103 | unset($_SESSION["dual-login"]); |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 104 | if ($_SESSION["mailcow_cc_role"] == "admin"){ |
| 105 | header("Location: /admin/mailbox"); |
| 106 | } elseif ($_SESSION["mailcow_cc_role"] == "domainadmin") { |
| 107 | header("Location: /domainadmin/mailbox"); |
| 108 | } else { |
| 109 | header("Location: /"); |
| 110 | } |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 111 | exit(); |
| 112 | } |
| 113 | else { |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 114 | $role = $_SESSION["mailcow_cc_role"]; |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 115 | session_regenerate_id(true); |
| 116 | session_unset(); |
| 117 | session_destroy(); |
| 118 | session_write_close(); |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 119 | if ($role == "admin") { |
| 120 | header("Location: /admin"); |
| 121 | } |
| 122 | elseif ($role == "domainadmin") { |
| 123 | header("Location: /domainadmin"); |
| 124 | } |
| 125 | else { |
| 126 | header("Location: /"); |
| 127 | } |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | // Check session |
| 132 | function session_check() { |
| 133 | if (isset($_SESSION['mailcow_cc_api']) && $_SESSION['mailcow_cc_api'] === true) { |
| 134 | return true; |
| 135 | } |
| 136 | if (!isset($_SESSION['SESS_REMOTE_UA']) || ($_SESSION['SESS_REMOTE_UA'] != $_SERVER['HTTP_USER_AGENT'])) { |
| 137 | $_SESSION['return'][] = array( |
| 138 | 'type' => 'warning', |
| 139 | 'msg' => 'session_ua' |
| 140 | ); |
| 141 | return false; |
| 142 | } |
| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 143 | // Check if this is a POST request (form-encoded or JSON) |
| 144 | $is_post_request = !empty($_POST) || ( |
| 145 | isset($_SERVER['CONTENT_TYPE']) && |
| 146 | strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false |
| 147 | ); |
| 148 | |
| 149 | if ($is_post_request) { |
| 150 | // Skip CSRF check for DataTables server-side processing endpoints |
| 151 | // These are read-only operations (equivalent to GET) authenticated by session |
| 152 | $is_search_endpoint = ( |
| 153 | isset($_GET['query']) && |
| 154 | preg_match('#^search/(domain|mailbox)$#', $_GET['query']) |
| 155 | ); |
| 156 | |
| 157 | if (!$is_search_endpoint && !empty($_POST)) { |
| 158 | if ($_SESSION['CSRF']['TOKEN'] != $_POST['csrf_token']) { |
| 159 | $_SESSION['return'][] = array( |
| 160 | 'type' => 'warning', |
| 161 | 'msg' => 'session_token' |
| 162 | ); |
| 163 | return false; |
| 164 | } |
| 165 | unset($_POST['csrf_token']); |
| 166 | $_SESSION['CSRF']['TOKEN'] = bin2hex(random_bytes(32)); |
| 167 | $_SESSION['CSRF']['TIME'] = time(); |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 168 | } |
| Matthias Andreas Benkard | d63fe56 | 2023-11-18 13:18:30 +0100 | [diff] [blame] | 169 | } |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | if (isset($_SESSION['mailcow_cc_role']) && session_check() === false) { |
| 174 | $_POST = array(); |
| 175 | $_FILES = array(); |
| 176 | } |