2019-02-23 18:59:18 +02:00
|
|
|
<?php
|
|
|
|
|
2021-07-09 07:43:23 +02:00
|
|
|
$ALLOW_ADMIN_EMAIL_LOGIN = (preg_match(
|
|
|
|
"/^([yY][eE][sS]|[yY])+$/",
|
|
|
|
$_ENV["ALLOW_ADMIN_EMAIL_LOGIN"]
|
|
|
|
));
|
|
|
|
|
2019-03-02 13:32:10 +02:00
|
|
|
$session_var_user_allowed = 'sogo-sso-user-allowed';
|
2019-02-26 10:02:35 +02:00
|
|
|
$session_var_pass = 'sogo-sso-pass';
|
2019-02-23 18:59:18 +02:00
|
|
|
|
2019-02-28 00:06:19 +02:00
|
|
|
// validate credentials for basic auth requests
|
2021-06-23 14:17:39 +02:00
|
|
|
if (isset($_SERVER['PHP_AUTH_USER'])) {
|
2019-02-28 00:06:19 +02:00
|
|
|
// load prerequisites only when required
|
|
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
|
|
|
|
$username = $_SERVER['PHP_AUTH_USER'];
|
|
|
|
$password = $_SERVER['PHP_AUTH_PW'];
|
2021-10-28 21:57:19 +02:00
|
|
|
$is_eas = false;
|
|
|
|
$is_dav = false;
|
|
|
|
$original_uri = isset($_SERVER['HTTP_X_ORIGINAL_URI']) ? $_SERVER['HTTP_X_ORIGINAL_URI'] : '';
|
|
|
|
if (preg_match('/^(\/SOGo|)\/dav.*/', $original_uri) === 1) {
|
|
|
|
$is_dav = true;
|
|
|
|
}
|
|
|
|
elseif (preg_match('/^(\/SOGo|)\/Microsoft-Server-ActiveSync.*/', $original_uri) === 1) {
|
|
|
|
$is_eas = true;
|
|
|
|
}
|
2021-10-28 22:05:28 +02:00
|
|
|
$login_check = check_login($username, $password, array('dav' => $is_dav, 'eas' => $is_eas));
|
2019-02-28 00:06:19 +02:00
|
|
|
if ($login_check === 'user') {
|
|
|
|
header("X-User: $username");
|
|
|
|
header("X-Auth: Basic ".base64_encode("$username:$password"));
|
|
|
|
header("X-Auth-Type: Basic");
|
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
header('HTTP/1.0 401 Unauthorized');
|
|
|
|
echo 'Invalid login';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check permissions and redirect for direct GET ?login=xy requests
|
2019-02-23 23:26:41 +02:00
|
|
|
elseif (isset($_GET['login'])) {
|
2019-02-26 21:44:53 +02:00
|
|
|
// load prerequisites only when required
|
2019-02-24 01:15:09 +02:00
|
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
|
2021-06-23 14:17:39 +02:00
|
|
|
// check if dual_login is active
|
|
|
|
$is_dual = (!empty($_SESSION["dual-login"]["username"])) ? true : false;
|
|
|
|
// check permissions (if dual_login is active, deny sso when acl is not given)
|
|
|
|
$login = html_entity_decode(rawurldecode($_GET["login"]));
|
|
|
|
if (isset($_SESSION['mailcow_cc_role']) &&
|
2021-10-25 13:47:25 +02:00
|
|
|
(($_SESSION['acl']['login_as'] == "1" && $ALLOW_ADMIN_EMAIL_LOGIN !== 0) || ($is_dual === false && $login == $_SESSION['mailcow_cc_username']))) {
|
2019-02-23 23:26:41 +02:00
|
|
|
if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
|
2021-06-23 14:17:39 +02:00
|
|
|
if (user_get_alias_details($login) !== false) {
|
2019-02-26 21:44:53 +02:00
|
|
|
// load master password
|
2019-02-26 10:02:35 +02:00
|
|
|
$sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
|
2019-02-26 21:44:53 +02:00
|
|
|
// register username and password in session
|
2019-03-02 13:32:10 +02:00
|
|
|
$_SESSION[$session_var_user_allowed][] = $login;
|
2019-02-26 10:02:35 +02:00
|
|
|
$_SESSION[$session_var_pass] = $sogo_sso_pass;
|
2021-10-28 21:57:19 +02:00
|
|
|
// update sasl logs
|
|
|
|
$service = ($app_passwd_data['eas'] === true) ? 'EAS' : 'DAV';
|
|
|
|
$stmt = $pdo->prepare("REPLACE INTO sasl_log (`service`, `app_password`, `username`, `real_rip`) VALUES ('SSO', 0, :username, :remote_addr)");
|
|
|
|
$stmt->execute(array(
|
|
|
|
':username' => $login,
|
2021-10-30 14:34:33 +02:00
|
|
|
':remote_addr' => ($_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'])
|
2021-10-28 21:57:19 +02:00
|
|
|
));
|
2019-02-26 21:44:53 +02:00
|
|
|
// redirect to sogo (sogo will get the correct credentials via nginx auth_request
|
2023-04-08 17:29:34 +02:00
|
|
|
header("Location: /SOGo/so/{$login}");
|
2019-02-23 23:26:41 +02:00
|
|
|
exit;
|
|
|
|
}
|
2019-02-23 18:59:18 +02:00
|
|
|
}
|
2019-02-23 23:26:41 +02:00
|
|
|
}
|
2019-02-28 00:06:19 +02:00
|
|
|
header('HTTP/1.0 403 Forbidden');
|
2021-10-25 13:47:25 +02:00
|
|
|
echo "Forbidden";
|
2019-02-23 23:26:41 +02:00
|
|
|
exit;
|
|
|
|
}
|
2019-03-02 13:32:10 +02:00
|
|
|
// only check for admin-login on sogo GUI requests
|
2021-10-28 21:57:19 +02:00
|
|
|
elseif (isset($_SERVER['HTTP_X_ORIGINAL_URI']) && strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 9), "/SOGo/so/") === 0) {
|
2019-02-26 21:44:53 +02:00
|
|
|
// this is an nginx auth_request call, we check for existing sogo-sso session variables
|
2019-02-24 01:15:09 +02:00
|
|
|
session_start();
|
2019-03-02 13:32:10 +02:00
|
|
|
// extract email address from "/SOGo/so/user@domain/xy"
|
|
|
|
$url_parts = explode("/", $_SERVER['HTTP_X_ORIGINAL_URI']);
|
2022-02-01 16:26:48 +02:00
|
|
|
$email_list = array(
|
|
|
|
$url_parts[3], // Requested mailbox
|
|
|
|
($_SESSION['mailcow_cc_username'] ?? ''), // Current user
|
|
|
|
($_SESSION["dual-login"]["username"] ?? ''), // Dual login user
|
|
|
|
);
|
|
|
|
foreach($email_list as $email) {
|
|
|
|
// check if this email is in session allowed list
|
|
|
|
if (
|
|
|
|
!empty($email) &&
|
|
|
|
filter_var($email, FILTER_VALIDATE_EMAIL) &&
|
|
|
|
is_array($_SESSION[$session_var_user_allowed]) &&
|
|
|
|
in_array($email, $_SESSION[$session_var_user_allowed])
|
|
|
|
) {
|
|
|
|
$username = $email;
|
|
|
|
$password = $_SESSION[$session_var_pass];
|
|
|
|
header("X-User: $username");
|
|
|
|
header("X-Auth: Basic ".base64_encode("$username:$password"));
|
|
|
|
header("X-Auth-Type: Basic");
|
|
|
|
exit;
|
|
|
|
}
|
2019-02-23 23:26:41 +02:00
|
|
|
}
|
2019-02-23 18:59:18 +02:00
|
|
|
}
|
2019-03-02 13:32:10 +02:00
|
|
|
|
|
|
|
// if username is empty, SOGo will use the normal login methods / login form
|
|
|
|
header("X-User: ");
|
|
|
|
header("X-Auth: ");
|
|
|
|
header("X-Auth-Type: ");
|