2019-02-23 17:59:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
$ALLOW_ADMIN_EMAIL_LOGIN = (preg_match(
|
2019-02-23 22:26:41 +01:00
|
|
|
"/^([yY][eE][sS]|[yY])+$/",
|
|
|
|
$_ENV["ALLOW_ADMIN_EMAIL_LOGIN"]
|
2019-02-23 17:59:18 +01:00
|
|
|
));
|
|
|
|
|
2019-02-26 09:02:35 +01:00
|
|
|
$session_var_user = 'sogo-sso-user';
|
|
|
|
$session_var_pass = 'sogo-sso-pass';
|
2019-02-23 17:59:18 +01:00
|
|
|
|
2019-02-27 23:06:19 +01:00
|
|
|
// prevent if feature is disabled
|
2019-02-23 17:59:18 +01:00
|
|
|
if (!$ALLOW_ADMIN_EMAIL_LOGIN) {
|
2019-02-27 23:06:19 +01:00
|
|
|
header('HTTP/1.0 403 Forbidden');
|
2019-02-26 20:44:53 +01:00
|
|
|
echo "this feature is disabled";
|
2019-02-23 22:26:41 +01:00
|
|
|
exit;
|
|
|
|
}
|
2019-02-27 23:06:19 +01:00
|
|
|
// validate credentials for basic auth requests
|
|
|
|
elseif (isset($_SERVER['PHP_AUTH_USER'])) {
|
|
|
|
// load prerequisites only when required
|
|
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
|
|
|
|
$username = $_SERVER['PHP_AUTH_USER'];
|
|
|
|
$password = $_SERVER['PHP_AUTH_PW'];
|
|
|
|
$login_check = check_login($username, $password);
|
|
|
|
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 22:26:41 +01:00
|
|
|
elseif (isset($_GET['login'])) {
|
2019-02-26 20:44:53 +01:00
|
|
|
// load prerequisites only when required
|
2019-02-24 00:15:09 +01:00
|
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
|
2019-02-26 20:44:53 +01:00
|
|
|
// check permissions
|
2019-02-23 22:26:41 +01:00
|
|
|
if (isset($_SESSION['mailcow_cc_role']) && $_SESSION['acl']['login_as'] == "1") {
|
|
|
|
$login = html_entity_decode(rawurldecode($_GET["login"]));
|
|
|
|
if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
if (!empty(mailbox('get', 'mailbox_details', $login))) {
|
2019-02-26 20:44:53 +01:00
|
|
|
// load master password
|
2019-02-26 09:02:35 +01:00
|
|
|
$sogo_sso_pass = file_get_contents("/etc/sogo-sso/sogo-sso.pass");
|
2019-02-26 20:44:53 +01:00
|
|
|
// register username and password in session
|
2019-02-26 09:02:35 +01:00
|
|
|
$_SESSION[$session_var_user] = $login;
|
|
|
|
$_SESSION[$session_var_pass] = $sogo_sso_pass;
|
2019-02-26 20:44:53 +01:00
|
|
|
// redirect to sogo (sogo will get the correct credentials via nginx auth_request
|
2019-02-23 22:26:41 +01:00
|
|
|
header("Location: /SOGo/");
|
|
|
|
exit;
|
|
|
|
}
|
2019-02-23 17:59:18 +01:00
|
|
|
}
|
2019-02-23 22:26:41 +01:00
|
|
|
}
|
2019-02-27 23:06:19 +01:00
|
|
|
header('HTTP/1.0 403 Forbidden');
|
2019-02-23 22:26:41 +01:00
|
|
|
exit;
|
|
|
|
}
|
2019-02-27 23:06:19 +01:00
|
|
|
// do not check for admin-login / sogo-sso for EAS and DAV requests, SOGo can check auth itself if no authorization header is set
|
|
|
|
elseif (
|
2019-02-27 23:14:30 +01:00
|
|
|
strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 28), "/Microsoft-Server-ActiveSync") == 0 &&
|
|
|
|
strcasecmp(substr($_SERVER['HTTP_X_ORIGINAL_URI'], 0, 9), "/SOGo/dav") == 0
|
2019-02-27 23:06:19 +01:00
|
|
|
) {
|
2019-02-26 20:44:53 +01:00
|
|
|
// this is an nginx auth_request call, we check for existing sogo-sso session variables
|
2019-02-24 00:15:09 +01:00
|
|
|
session_start();
|
2019-02-26 09:02:35 +01:00
|
|
|
if (isset($_SESSION[$session_var_user]) && filter_var($_SESSION[$session_var_user], FILTER_VALIDATE_EMAIL)) {
|
|
|
|
$username = $_SESSION[$session_var_user];
|
|
|
|
$password = $_SESSION[$session_var_pass];
|
|
|
|
header("X-User: $username");
|
|
|
|
header("X-Auth: Basic ".base64_encode("$username:$password"));
|
|
|
|
header("X-Auth-Type: Basic");
|
|
|
|
} else {
|
|
|
|
// if username is empty, SOGo will display the normal login form
|
|
|
|
header("X-User: ");
|
|
|
|
header("X-Auth: ");
|
|
|
|
header("X-Auth-Type: ");
|
2019-02-23 22:26:41 +01:00
|
|
|
}
|
|
|
|
exit;
|
2019-02-23 17:59:18 +01:00
|
|
|
}
|