| Matthias Andreas Benkard | 97f1202 | 2026-10-23 08:46:40 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | function check_login($user, $pass, $extra = null) { |
| 3 | global $pdo; |
| 4 | global $redis; |
| 5 | |
| 6 | $is_internal = $extra['is_internal']; |
| 7 | $role = $extra['role']; |
| 8 | $extra['service'] = !isset($extra['service']) ? 'NONE' : $extra['service']; |
| 9 | |
| 10 | // Try validate admin |
| 11 | if (!isset($role) || $role == "admin") { |
| 12 | $result = admin_login($user, $pass); |
| 13 | if ($result !== false){ |
| 14 | return $result; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | // Try validate domain admin |
| 19 | if (!isset($role) || $role == "domain_admin") { |
| 20 | $result = domainadmin_login($user, $pass); |
| 21 | if ($result !== false) { |
| 22 | return $result; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | |
| 27 | // Try validate app password |
| 28 | if (!isset($role) || $role == "app") { |
| 29 | $result = apppass_login($user, $pass, $extra); |
| 30 | if ($result !== false) { |
| 31 | $real_rip = ($_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR']); |
| 32 | set_sasl_log($user, $real_rip, $extra['service'], $pass); |
| 33 | return $result; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Try validate user |
| 38 | if (!isset($role) || $role == "user") { |
| 39 | $result = user_login($user, $pass, $extra); |
| 40 | if ($result !== false) { |
| 41 | $real_rip = ($_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR']); |
| 42 | set_sasl_log($user, $real_rip, $extra['service']); |
| 43 | return $result; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // skip log and only return false if it's an internal request |
| 48 | if ($is_internal == true) return false; |
| 49 | |
| 50 | if (!isset($_SESSION['ldelay'])) { |
| 51 | $_SESSION['ldelay'] = "0"; |
| 52 | $redis->publish("F2B_CHANNEL", "mailcow UI: Invalid password for " . $user . " by " . $_SERVER['REMOTE_ADDR']); |
| 53 | error_log("mailcow UI: Invalid password for " . $user . " by " . $_SERVER['REMOTE_ADDR']); |
| 54 | } |
| 55 | elseif (!isset($_SESSION['mailcow_cc_username'])) { |
| 56 | $_SESSION['ldelay'] = $_SESSION['ldelay']+0.5; |
| 57 | $redis->publish("F2B_CHANNEL", "mailcow UI: Invalid password for " . $user . " by " . $_SERVER['REMOTE_ADDR']); |
| 58 | error_log("mailcow UI: Invalid password for " . $user . " by " . $_SERVER['REMOTE_ADDR']); |
| 59 | } |
| 60 | $_SESSION['return'][] = array( |
| 61 | 'type' => 'danger', |
| 62 | 'log' => array(__FUNCTION__, $user, '*'), |
| 63 | 'msg' => 'login_failed' |
| 64 | ); |
| 65 | |
| 66 | sleep($_SESSION['ldelay']); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | function admin_login($user, $pass){ |
| 71 | global $pdo; |
| 72 | |
| 73 | if (!filter_var($user, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $user))) { |
| 74 | if (!$is_internal){ |
| 75 | $_SESSION['return'][] = array( |
| 76 | 'type' => 'danger', |
| 77 | 'log' => array(__FUNCTION__, $user, '*'), |
| 78 | 'msg' => 'malformed_username' |
| 79 | ); |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | $user = strtolower(trim($user)); |
| 85 | $stmt = $pdo->prepare("SELECT `password`, `attributes` FROM `admin` |
| 86 | WHERE `superadmin` = '1' |
| 87 | AND `active` = '1' |
| 88 | AND `username` = :user"); |
| 89 | $stmt->execute(array(':user' => $user)); |
| 90 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 91 | |
| 92 | // verify password |
| 93 | if (verify_hash($row['password'], $pass)) { |
| 94 | $admin_attrs = json_decode($row['attributes'], true) ?? []; |
| 95 | |
| 96 | // Check force_pw_update |
| 97 | if (intval($admin_attrs['force_pw_update'] ?? 0) == 1) { |
| 98 | $_SESSION['pending_pw_update'] = true; |
| 99 | } |
| 100 | |
| 101 | // check for tfa authenticators |
| 102 | $authenticators = get_tfa($user); |
| 103 | if (isset($authenticators['additional']) && is_array($authenticators['additional']) && count($authenticators['additional']) > 0) { |
| 104 | // active tfa authenticators found, set pending user login |
| 105 | $_SESSION['pending_mailcow_cc_username'] = $user; |
| 106 | $_SESSION['pending_mailcow_cc_role'] = "admin"; |
| 107 | $_SESSION['pending_tfa_methods'] = $authenticators['additional']; |
| 108 | unset($_SESSION['ldelay']); |
| 109 | $_SESSION['return'][] = array( |
| 110 | 'type' => 'info', |
| 111 | 'log' => array(__FUNCTION__, $user, '*'), |
| 112 | 'msg' => 'awaiting_tfa_confirmation' |
| 113 | ); |
| 114 | return "pending"; |
| 115 | } else { |
| 116 | unset($_SESSION['ldelay']); |
| 117 | // Reactivate TFA if it was set to "deactivate TFA for next login" |
| 118 | $stmt = $pdo->prepare("UPDATE `tfa` SET `active`='1' WHERE `username` = :user"); |
| 119 | $stmt->execute(array(':user' => $user)); |
| 120 | // Check force_tfa: only force setup if NO TFA exists at all |
| 121 | if (intval($admin_attrs['force_tfa'] ?? 0) == 1 && !tfa_exists($user)) { |
| 122 | $_SESSION['pending_tfa_setup'] = true; |
| 123 | } |
| 124 | $_SESSION['return'][] = array( |
| 125 | 'type' => 'success', |
| 126 | 'log' => array(__FUNCTION__, $user, '*'), |
| 127 | 'msg' => array('logged_in_as', $user) |
| 128 | ); |
| 129 | return "admin"; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return false; |
| 134 | } |
| 135 | function domainadmin_login($user, $pass){ |
| 136 | global $pdo; |
| 137 | |
| 138 | if (!filter_var($user, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $user))) { |
| 139 | if (!$is_internal){ |
| 140 | $_SESSION['return'][] = array( |
| 141 | 'type' => 'danger', |
| 142 | 'log' => array(__FUNCTION__, $user, '*'), |
| 143 | 'msg' => 'malformed_username' |
| 144 | ); |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | $stmt = $pdo->prepare("SELECT `password`, `attributes` FROM `admin` |
| 150 | WHERE `superadmin` = '0' |
| 151 | AND `active`='1' |
| 152 | AND `username` = :user"); |
| 153 | $stmt->execute(array(':user' => $user)); |
| 154 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 155 | |
| 156 | // verify password |
| 157 | if (verify_hash($row['password'], $pass) !== false) { |
| 158 | $admin_attrs = json_decode($row['attributes'], true) ?? []; |
| 159 | |
| 160 | // Check force_pw_update |
| 161 | if (intval($admin_attrs['force_pw_update'] ?? 0) == 1) { |
| 162 | $_SESSION['pending_pw_update'] = true; |
| 163 | } |
| 164 | |
| 165 | // check for tfa authenticators |
| 166 | $authenticators = get_tfa($user); |
| 167 | if (isset($authenticators['additional']) && is_array($authenticators['additional']) && count($authenticators['additional']) > 0) { |
| 168 | $_SESSION['pending_mailcow_cc_username'] = $user; |
| 169 | $_SESSION['pending_mailcow_cc_role'] = "domainadmin"; |
| 170 | $_SESSION['pending_tfa_methods'] = $authenticators['additional']; |
| 171 | unset($_SESSION['ldelay']); |
| 172 | $_SESSION['return'][] = array( |
| 173 | 'type' => 'info', |
| 174 | 'log' => array(__FUNCTION__, $user, '*'), |
| 175 | 'msg' => 'awaiting_tfa_confirmation' |
| 176 | ); |
| 177 | return "pending"; |
| 178 | } |
| 179 | else { |
| 180 | unset($_SESSION['ldelay']); |
| 181 | // Reactivate TFA if it was set to "deactivate TFA for next login" |
| 182 | $stmt = $pdo->prepare("UPDATE `tfa` SET `active`='1' WHERE `username` = :user"); |
| 183 | $stmt->execute(array(':user' => $user)); |
| 184 | // Check force_tfa: only force setup if NO TFA exists at all |
| 185 | if (intval($admin_attrs['force_tfa'] ?? 0) == 1 && !tfa_exists($user)) { |
| 186 | $_SESSION['pending_tfa_setup'] = true; |
| 187 | } |
| 188 | $_SESSION['return'][] = array( |
| 189 | 'type' => 'success', |
| 190 | 'log' => array(__FUNCTION__, $user, '*'), |
| 191 | 'msg' => array('logged_in_as', $user) |
| 192 | ); |
| 193 | return "domainadmin"; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | function user_login($user, $pass, $extra = null){ |
| 200 | global $pdo; |
| 201 | global $iam_provider; |
| 202 | global $iam_settings; |
| 203 | |
| 204 | $is_internal = $extra['is_internal']; |
| 205 | $extra['service'] = !isset($extra['service']) ? 'NONE' : $extra['service']; |
| 206 | |
| 207 | if (!filter_var($user, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $user))) { |
| 208 | if (!$is_internal){ |
| 209 | $_SESSION['return'][] = array( |
| 210 | 'type' => 'danger', |
| 211 | 'log' => array(__FUNCTION__, $user, '*'), |
| 212 | 'msg' => 'malformed_username' |
| 213 | ); |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | $stmt = $pdo->prepare("SELECT |
| 219 | mailbox.*, |
| 220 | domain.active AS d_active |
| 221 | FROM `mailbox` |
| 222 | INNER JOIN domain on mailbox.domain = domain.domain |
| 223 | WHERE `kind` NOT REGEXP 'location|thing|group' |
| 224 | AND `username` = :user"); |
| 225 | $stmt->execute(array(':user' => $user)); |
| 226 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 227 | |
| 228 | // user does not exist, try call idp login and create user if possible via rest flow |
| 229 | if (!$row){ |
| 230 | $result = false; |
| 231 | if ($iam_settings['authsource'] == 'keycloak' && intval($iam_settings['mailpassword_flow']) == 1){ |
| 232 | $result = keycloak_mbox_login_rest($user, $pass, array('is_internal' => $is_internal, 'create' => true)); |
| 233 | } else if ($iam_settings['authsource'] == 'ldap') { |
| 234 | $result = ldap_mbox_login($user, $pass, array('is_internal' => $is_internal, 'create' => true)); |
| 235 | } |
| 236 | if ($result !== false){ |
| 237 | // double check if mailbox is active |
| 238 | $stmt = $pdo->prepare("SELECT * FROM `mailbox` |
| 239 | INNER JOIN domain on mailbox.domain = domain.domain |
| 240 | WHERE `kind` NOT REGEXP 'location|thing|group' |
| 241 | AND `mailbox`.`active`='1' |
| 242 | AND `domain`.`active`='1' |
| 243 | AND `username` = :user"); |
| 244 | $stmt->execute(array(':user' => $user)); |
| 245 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 246 | |
| 247 | if (!empty($row)) { |
| 248 | // check if user has access to service (imap, smtp, pop3, sieve, dav, eas) if service is set |
| 249 | $row['attributes'] = json_decode($row['attributes'], true); |
| 250 | if ($extra['service'] != 'NONE') { |
| 251 | $key = strtolower($extra['service']) . "_access"; |
| 252 | if (isset($row['attributes'][$key]) && $row['attributes'][$key] != '1') { |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | clear_session(); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | // check if user has access to service (imap, smtp, pop3, sieve) if service is set |
| 264 | $row['attributes'] = json_decode($row['attributes'], true); |
| 265 | if ($extra['service'] != 'NONE') { |
| 266 | $key = strtolower($extra['service']) . "_access"; |
| 267 | if (isset($row['attributes'][$key]) && $row['attributes'][$key] != '1') { |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | switch ($row['authsource']) { |
| 272 | case 'keycloak': |
| 273 | // user authsource is keycloak, try using via rest flow |
| 274 | if (intval($iam_settings['mailpassword_flow']) == 1){ |
| 275 | $result = keycloak_mbox_login_rest($user, $pass, array('is_internal' => $is_internal)); |
| 276 | if ($result !== false) { |
| 277 | // double check if mailbox and domain is active |
| 278 | $stmt = $pdo->prepare("SELECT * FROM `mailbox` |
| 279 | INNER JOIN domain on mailbox.domain = domain.domain |
| 280 | WHERE `kind` NOT REGEXP 'location|thing|group' |
| 281 | AND `mailbox`.`active`='1' |
| 282 | AND `domain`.`active`='1' |
| 283 | AND `username` = :user"); |
| 284 | $stmt->execute(array(':user' => $user)); |
| 285 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 286 | if (empty($row)) { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | $row['attributes'] = json_decode($row['attributes'], true); |
| 291 | |
| 292 | // check for tfa authenticators |
| 293 | $authenticators = get_tfa($user); |
| 294 | if (isset($authenticators['additional']) && is_array($authenticators['additional']) && count($authenticators['additional']) > 0 && !$is_internal) { |
| 295 | // authenticators found, init TFA flow |
| 296 | $_SESSION['pending_mailcow_cc_username'] = $user; |
| 297 | $_SESSION['pending_mailcow_cc_role'] = "user"; |
| 298 | $_SESSION['pending_tfa_methods'] = $authenticators['additional']; |
| 299 | unset($_SESSION['ldelay']); |
| 300 | $_SESSION['return'][] = array( |
| 301 | 'type' => 'success', |
| 302 | 'log' => array(__FUNCTION__, $user, '*', 'Provider: Keycloak'), |
| 303 | 'msg' => array('logged_in_as', $user) |
| 304 | ); |
| 305 | return "pending"; |
| 306 | } else if (!isset($authenticators['additional']) || !is_array($authenticators['additional']) || count($authenticators['additional']) == 0) { |
| 307 | // no authenticators found, login successfull |
| 308 | if (!$is_internal){ |
| 309 | unset($_SESSION['ldelay']); |
| 310 | // Reactivate TFA if it was set to "deactivate TFA for next login" |
| 311 | $stmt = $pdo->prepare("UPDATE `tfa` SET `active`='1' WHERE `username` = :user"); |
| 312 | $stmt->execute(array(':user' => $user)); |
| 313 | // Check force_tfa: only force setup if NO TFA exists at all |
| 314 | if (intval($row['attributes']['force_tfa']) == 1 && !tfa_exists($user)) { |
| 315 | $_SESSION['pending_tfa_setup'] = true; |
| 316 | } |
| 317 | $_SESSION['return'][] = array( |
| 318 | 'type' => 'success', |
| 319 | 'log' => array(__FUNCTION__, $user, '*', 'Provider: Keycloak'), |
| 320 | 'msg' => array('logged_in_as', $user) |
| 321 | ); |
| 322 | } |
| 323 | return "user"; |
| 324 | } |
| 325 | } |
| 326 | return $result; |
| 327 | } else { |
| 328 | return false; |
| 329 | } |
| 330 | break; |
| 331 | case 'ldap': |
| 332 | // user authsource is ldap |
| 333 | $result = ldap_mbox_login($user, $pass, array('is_internal' => $is_internal)); |
| 334 | if ($result !== false) { |
| 335 | // double check if mailbox and domain is active |
| 336 | $stmt = $pdo->prepare("SELECT * FROM `mailbox` |
| 337 | INNER JOIN domain on mailbox.domain = domain.domain |
| 338 | WHERE `kind` NOT REGEXP 'location|thing|group' |
| 339 | AND `mailbox`.`active`='1' |
| 340 | AND `domain`.`active`='1' |
| 341 | AND `username` = :user"); |
| 342 | $stmt->execute(array(':user' => $user)); |
| 343 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 344 | if (empty($row)) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | $row['attributes'] = json_decode($row['attributes'], true); |
| 349 | |
| 350 | // check for tfa authenticators |
| 351 | $authenticators = get_tfa($user); |
| 352 | if (isset($authenticators['additional']) && is_array($authenticators['additional']) && count($authenticators['additional']) > 0 && !$is_internal) { |
| 353 | // authenticators found, init TFA flow |
| 354 | $_SESSION['pending_mailcow_cc_username'] = $user; |
| 355 | $_SESSION['pending_mailcow_cc_role'] = "user"; |
| 356 | $_SESSION['pending_tfa_methods'] = $authenticators['additional']; |
| 357 | unset($_SESSION['ldelay']); |
| 358 | $_SESSION['return'][] = array( |
| 359 | 'type' => 'success', |
| 360 | 'log' => array(__FUNCTION__, $user, '*', 'Provider: LDAP'), |
| 361 | 'msg' => array('logged_in_as', $user) |
| 362 | ); |
| 363 | return "pending"; |
| 364 | } else if (!isset($authenticators['additional']) || !is_array($authenticators['additional']) || count($authenticators['additional']) == 0) { |
| 365 | // no authenticators found, login successfull |
| 366 | if (!$is_internal){ |
| 367 | unset($_SESSION['ldelay']); |
| 368 | // Reactivate TFA if it was set to "deactivate TFA for next login" |
| 369 | $stmt = $pdo->prepare("UPDATE `tfa` SET `active`='1' WHERE `username` = :user"); |
| 370 | $stmt->execute(array(':user' => $user)); |
| 371 | // Check force_tfa: only force setup if NO TFA exists at all |
| 372 | if (intval($row['attributes']['force_tfa']) == 1 && !tfa_exists($user)) { |
| 373 | $_SESSION['pending_tfa_setup'] = true; |
| 374 | } |
| 375 | $_SESSION['return'][] = array( |
| 376 | 'type' => 'success', |
| 377 | 'log' => array(__FUNCTION__, $user, '*', 'Provider: LDAP'), |
| 378 | 'msg' => array('logged_in_as', $user) |
| 379 | ); |
| 380 | } |
| 381 | return "user"; |
| 382 | } |
| 383 | } |
| 384 | return $result; |
| 385 | break; |
| 386 | case 'mailcow': |
| 387 | if ($row['active'] != 1 || $row['d_active'] != 1) { |
| 388 | return false; |
| 389 | } |
| 390 | // verify password |
| 391 | if (verify_hash($row['password'], $pass) !== false) { |
| 392 | |
| 393 | if (intval($row['attributes']['force_pw_update']) == 1) { |
| 394 | $_SESSION['pending_pw_update'] = true; |
| 395 | } |
| 396 | |
| 397 | // check for tfa authenticators |
| 398 | $authenticators = get_tfa($user); |
| 399 | if (isset($authenticators['additional']) && is_array($authenticators['additional']) && count($authenticators['additional']) > 0 && !$is_internal) { |
| 400 | // authenticators found, init TFA flow |
| 401 | $_SESSION['pending_mailcow_cc_username'] = $user; |
| 402 | $_SESSION['pending_mailcow_cc_role'] = "user"; |
| 403 | $_SESSION['pending_tfa_methods'] = $authenticators['additional']; |
| 404 | unset($_SESSION['ldelay']); |
| 405 | $_SESSION['return'][] = array( |
| 406 | 'type' => 'success', |
| 407 | 'log' => array(__FUNCTION__, $user, '*', 'Provider: mailcow'), |
| 408 | 'msg' => array('logged_in_as', $user) |
| 409 | ); |
| 410 | return "pending"; |
| 411 | } else if (!isset($authenticators['additional']) || !is_array($authenticators['additional']) || count($authenticators['additional']) == 0) { |
| 412 | // no authenticators found, login successfull |
| 413 | if (!$is_internal){ |
| 414 | unset($_SESSION['ldelay']); |
| 415 | // Reactivate TFA if it was set to "deactivate TFA for next login" |
| 416 | $stmt = $pdo->prepare("UPDATE `tfa` SET `active`='1' WHERE `username` = :user"); |
| 417 | $stmt->execute(array(':user' => $user)); |
| 418 | // Check force_tfa: only force setup if NO TFA exists at all |
| 419 | if (intval($row['attributes']['force_tfa']) == 1 && !tfa_exists($user)) { |
| 420 | $_SESSION['pending_tfa_setup'] = true; |
| 421 | } |
| 422 | $_SESSION['return'][] = array( |
| 423 | 'type' => 'success', |
| 424 | 'log' => array(__FUNCTION__, $user, '*', 'Provider: mailcow'), |
| 425 | 'msg' => array('logged_in_as', $user) |
| 426 | ); |
| 427 | } |
| 428 | return "user"; |
| 429 | } |
| 430 | } |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | return false; |
| 435 | } |
| 436 | function apppass_login($user, $pass, $extra = null){ |
| 437 | global $pdo; |
| 438 | |
| 439 | $is_internal = $extra['is_internal']; |
| 440 | |
| 441 | if (!filter_var($user, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $user))) { |
| 442 | if (!$is_internal){ |
| 443 | $_SESSION['return'][] = array( |
| 444 | 'type' => 'danger', |
| 445 | 'log' => array(__FUNCTION__, $user, '*'), |
| 446 | 'msg' => 'malformed_username' |
| 447 | ); |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | $extra['service'] = !isset($extra['service']) ? 'NONE' : $extra['service']; |
| 453 | if (!$is_internal && $extra['service'] == 'NONE') { |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | // fetch app password data |
| 458 | $stmt = $pdo->prepare("SELECT `app_passwd`.*, `app_passwd`.`password` as `password`, `app_passwd`.`id` as `app_passwd_id` FROM `app_passwd` |
| 459 | INNER JOIN `mailbox` ON `mailbox`.`username` = `app_passwd`.`mailbox` |
| 460 | INNER JOIN `domain` ON `mailbox`.`domain` = `domain`.`domain` |
| 461 | WHERE `mailbox`.`kind` NOT REGEXP 'location|thing|group' |
| 462 | AND `mailbox`.`active` = '1' |
| 463 | AND `domain`.`active` = '1' |
| 464 | AND `app_passwd`.`active` = '1' |
| 465 | AND `app_passwd`.`mailbox` = :user" |
| 466 | ); |
| 467 | // fetch password data |
| 468 | $stmt->execute(array( |
| 469 | ':user' => $user, |
| 470 | )); |
| 471 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 472 | |
| 473 | foreach ($rows as $row) { |
| 474 | if ($extra['service'] != 'NONE' && $row[strtolower($extra['service']) . '_access'] != '1'){ |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | // verify password |
| 479 | if (verify_hash($row['password'], $pass) !== false) { |
| 480 | $_SESSION['app_passwd_id'] = $row['app_passwd_id']; |
| 481 | unset($_SESSION['ldelay']); |
| 482 | return "user"; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return false; |
| 487 | } |
| 488 | // Keycloak REST Api Flow - auth user by mailcow_password attribute |
| 489 | // This password will be used for direct UI, IMAP and SMTP Auth |
| 490 | // To use direct user credentials, only Authorization Code Flow is valid |
| 491 | function keycloak_mbox_login_rest($user, $pass, $extra = null){ |
| 492 | global $pdo; |
| 493 | global $iam_provider; |
| 494 | global $iam_settings; |
| 495 | |
| 496 | $is_internal = $extra['is_internal']; |
| 497 | $create = $extra['create']; |
| 498 | |
| 499 | if (!filter_var($user, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $user))) { |
| 500 | if (!$is_internal){ |
| 501 | $_SESSION['return'][] = array( |
| 502 | 'type' => 'danger', |
| 503 | 'log' => array(__FUNCTION__, $user, '*'), |
| 504 | 'msg' => 'malformed_username' |
| 505 | ); |
| 506 | } |
| 507 | return false; |
| 508 | } |
| 509 | if (!$iam_provider) { |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | // get access_token for service account of mailcow client |
| 514 | $admin_token = identity_provider("get-keycloak-admin-token"); |
| 515 | |
| 516 | // get the mailcow_password attribute from keycloak user |
| 517 | $url = "{$iam_settings['server_url']}/admin/realms/{$iam_settings['realm']}/users"; |
| 518 | $queryParams = array('email' => $user, 'exact' => true); |
| 519 | $queryString = http_build_query($queryParams); |
| 520 | $curl = curl_init(); |
| 521 | curl_setopt($curl, CURLOPT_TIMEOUT, 7); |
| 522 | curl_setopt($curl, CURLOPT_URL, $url . '?' . $queryString); |
| 523 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 524 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
| 525 | 'Authorization: Bearer ' . $admin_token, |
| 526 | 'Content-Type: application/json' |
| 527 | )); |
| 528 | $user_res = json_decode(curl_exec($curl), true)[0]; |
| 529 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 530 | curl_close($curl); |
| 531 | if ($code != 200) { |
| 532 | $_SESSION['return'][] = array( |
| 533 | 'type' => 'danger', |
| 534 | 'log' => array(__FUNCTION__, $user, '*', 'Identity Provider returned HTTP ' . $code), |
| 535 | 'msg' => 'generic_server_error' |
| 536 | ); |
| 537 | return false; |
| 538 | } |
| 539 | if (!isset($user_res['attributes']['mailcow_password']) || !is_array($user_res['attributes']['mailcow_password'])){ |
| 540 | $_SESSION['return'][] = array( |
| 541 | 'type' => 'danger', |
| 542 | 'log' => array(__FUNCTION__, $user, '*', 'User has no mailcow_password attribute'), |
| 543 | 'msg' => 'generic_server_error' |
| 544 | ); |
| 545 | return false; |
| 546 | } |
| 547 | if (empty($user_res['attributes']['mailcow_password'][0])){ |
| 548 | $_SESSION['return'][] = array( |
| 549 | 'type' => 'danger', |
| 550 | 'log' => array(__FUNCTION__, $user, '*', "User's mailcow_password attribute is empty"), |
| 551 | 'msg' => 'generic_server_error' |
| 552 | ); |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | // validate mailcow_password |
| 557 | $mailcow_password = $user_res['attributes']['mailcow_password'][0]; |
| 558 | if (!verify_hash($mailcow_password, $pass)) { |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | // get mapped template |
| 563 | $user_template = $user_res['attributes']['mailcow_template'][0]; |
| 564 | $mapper_key = array_search($user_template, $iam_settings['mappers']); |
| 565 | |
| 566 | if (!$create) { |
| 567 | // login success |
| 568 | if ($mapper_key !== false) { |
| 569 | // update user |
| 570 | $_SESSION['access_all_exception'] = '1'; |
| 571 | mailbox('edit', 'mailbox_from_template', array( |
| 572 | 'username' => $user, |
| 573 | 'name' => $user_res['name'], |
| 574 | 'template' => $iam_settings['templates'][$mapper_key] |
| 575 | )); |
| 576 | $_SESSION['access_all_exception'] = '0'; |
| 577 | } |
| 578 | return 'user'; |
| 579 | } |
| 580 | |
| 581 | // check if login provisioning is enabled before creating user |
| 582 | if (!$iam_settings['login_provisioning']){ |
| 583 | if (!$is_internal){ |
| 584 | $_SESSION['return'][] = array( |
| 585 | 'type' => 'danger', |
| 586 | 'log' => array(__FUNCTION__, "Auto-create users on login is deactivated"), |
| 587 | 'msg' => 'login_failed' |
| 588 | ); |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | // check if matching attribute exist |
| 593 | if (empty($iam_settings['mappers']) || !$user_template || $mapper_key === false) { |
| 594 | if (!empty($iam_settings['default_template'])) { |
| 595 | $mbox_template = $iam_settings['default_template']; |
| 596 | } else { |
| 597 | $_SESSION['return'][] = array( |
| 598 | 'type' => 'danger', |
| 599 | 'log' => array(__FUNCTION__, $user, '*', 'No matching attribute mapping was found'), |
| 600 | 'msg' => 'generic_server_error' |
| 601 | ); |
| 602 | return false; |
| 603 | } |
| 604 | } else { |
| 605 | $mbox_template = $iam_settings['templates'][$mapper_key]; |
| 606 | } |
| 607 | |
| 608 | // create mailbox |
| 609 | $_SESSION['access_all_exception'] = '1'; |
| 610 | $create_res = mailbox('add', 'mailbox_from_template', array( |
| 611 | 'domain' => explode('@', $user)[1], |
| 612 | 'local_part' => explode('@', $user)[0], |
| 613 | 'name' => $user_res['name'], |
| 614 | 'authsource' => 'keycloak', |
| 615 | 'template' => $mbox_template |
| 616 | )); |
| 617 | $_SESSION['access_all_exception'] = '0'; |
| 618 | if (!$create_res){ |
| 619 | clear_session(); |
| 620 | $_SESSION['return'][] = array( |
| 621 | 'type' => 'danger', |
| 622 | 'log' => array(__FUNCTION__, $user, '*', 'Could not create mailbox on login'), |
| 623 | 'msg' => 'generic_server_error' |
| 624 | ); |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | return 'user'; |
| 629 | } |
| 630 | function ldap_mbox_login($user, $pass, $extra = null){ |
| 631 | global $pdo; |
| 632 | global $iam_provider; |
| 633 | global $iam_settings; |
| 634 | |
| 635 | $is_internal = $extra['is_internal']; |
| 636 | $create = $extra['create']; |
| 637 | |
| 638 | if (!filter_var($user, FILTER_VALIDATE_EMAIL) && !ctype_alnum(str_replace(array('_', '.', '-'), '', $user))) { |
| 639 | if (!$is_internal){ |
| 640 | $_SESSION['return'][] = array( |
| 641 | 'type' => 'danger', |
| 642 | 'log' => array(__FUNCTION__, $user, '*'), |
| 643 | 'msg' => 'malformed_username' |
| 644 | ); |
| 645 | } |
| 646 | return false; |
| 647 | } |
| 648 | if (!$iam_provider) { |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | try { |
| 653 | $ldap_query = $iam_provider->query(); |
| 654 | if (!empty($iam_settings['filter'])) { |
| 655 | $ldap_query = $ldap_query->rawFilter($iam_settings['filter']); |
| 656 | } |
| 657 | $ldap_query = $ldap_query->where($iam_settings['username_field'], '=', $user) |
| 658 | ->select([$iam_settings['username_field'], $iam_settings['attribute_field'], 'displayname', 'distinguishedname', 'dn']); |
| 659 | |
| 660 | $user_res = $ldap_query->firstOrFail(); |
| 661 | } catch (Exception $e) { |
| 662 | // clear $_SESSION['return'] to not leak data |
| 663 | $_SESSION['return'] = array(); |
| 664 | $_SESSION['return'][] = array( |
| 665 | 'type' => 'danger', |
| 666 | 'log' => array(__FUNCTION__, $user, '*', $e->getMessage()), |
| 667 | 'msg' => 'generic_server_error' |
| 668 | ); |
| 669 | return false; |
| 670 | } |
| 671 | try { |
| 672 | if (!$iam_provider->auth()->attempt($user_res['dn'], $pass)) { |
| 673 | return false; |
| 674 | } |
| 675 | } catch (Exception $e) { |
| 676 | // clear $_SESSION['return'] to not leak data |
| 677 | $_SESSION['return'] = array(); |
| 678 | $_SESSION['return'][] = array( |
| 679 | 'type' => 'danger', |
| 680 | 'log' => array(__FUNCTION__, $user, '*', $e->getMessage()), |
| 681 | 'msg' => 'generic_server_error' |
| 682 | ); |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | // get mapped template |
| 687 | $user_template = $user_res[$iam_settings['attribute_field']][0]; |
| 688 | $mapper_key = array_search($user_template, $iam_settings['mappers']); |
| 689 | |
| 690 | if (!$create) { |
| 691 | // login success |
| 692 | if ($mapper_key !== false) { |
| 693 | // update user |
| 694 | $_SESSION['access_all_exception'] = '1'; |
| 695 | mailbox('edit', 'mailbox_from_template', array( |
| 696 | 'username' => $user, |
| 697 | 'name' => $user_res['displayname'][0], |
| 698 | 'template' => $iam_settings['templates'][$mapper_key] |
| 699 | )); |
| 700 | $_SESSION['access_all_exception'] = '0'; |
| 701 | } |
| 702 | return 'user'; |
| 703 | } |
| 704 | |
| 705 | // check if login provisioning is enabled before creating user |
| 706 | if (!$iam_settings['login_provisioning']){ |
| 707 | if (!$is_internal){ |
| 708 | $_SESSION['return'][] = array( |
| 709 | 'type' => 'danger', |
| 710 | 'log' => array(__FUNCTION__, "Auto-create users on login is deactivated"), |
| 711 | 'msg' => 'login_failed' |
| 712 | ); |
| 713 | } |
| 714 | return false; |
| 715 | } |
| 716 | // check if matching attribute exist |
| 717 | if (empty($iam_settings['mappers']) || !$user_template || $mapper_key === false) { |
| 718 | if (!empty($iam_settings['default_template'])) { |
| 719 | $mbox_template = $iam_settings['default_template']; |
| 720 | } else { |
| 721 | $_SESSION['return'][] = array( |
| 722 | 'type' => 'danger', |
| 723 | 'log' => array(__FUNCTION__, $user, '*', 'No matching attribute mapping was found'), |
| 724 | 'msg' => 'generic_server_error' |
| 725 | ); |
| 726 | return false; |
| 727 | } |
| 728 | } else { |
| 729 | $mbox_template = $iam_settings['templates'][$mapper_key]; |
| 730 | } |
| 731 | |
| 732 | // create mailbox |
| 733 | $_SESSION['access_all_exception'] = '1'; |
| 734 | $create_res = mailbox('add', 'mailbox_from_template', array( |
| 735 | 'domain' => explode('@', $user)[1], |
| 736 | 'local_part' => explode('@', $user)[0], |
| 737 | 'name' => $user_res['displayname'][0], |
| 738 | 'authsource' => 'ldap', |
| 739 | 'template' => $mbox_template |
| 740 | )); |
| 741 | $_SESSION['access_all_exception'] = '0'; |
| 742 | if (!$create_res){ |
| 743 | clear_session(); |
| 744 | $_SESSION['return'][] = array( |
| 745 | 'type' => 'danger', |
| 746 | 'log' => array(__FUNCTION__, $user, '*', 'Could not create mailbox on login'), |
| 747 | 'msg' => 'generic_server_error' |
| 748 | ); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | return 'user'; |
| 753 | } |