Improved Autodiscover

This update is for security purposes.
master
Phoenix Eve Aspacio 2017-02-11 13:20:04 +08:00 committed by GitHub
parent a6d5bcecb7
commit d6297d17c3
1 changed files with 106 additions and 90 deletions

View File

@ -1,6 +1,4 @@
<?php <?php
header("Content-Type: application/xml");
require_once "inc/vars.inc.php";
$config = array( $config = array(
'useEASforOutlook' => 'yes', 'useEASforOutlook' => 'yes',
'autodiscoverType' => 'activesync', 'autodiscoverType' => 'activesync',
@ -15,22 +13,43 @@ $config = array(
'ssl' => 'on' 'ssl' => 'on'
), ),
'activesync' => array( 'activesync' => array(
'url' => 'https://' . $mailcow_hostname . '/Microsoft-Server-ActiveSync' 'url' => 'https://'.$mailcow_hostname.'/Microsoft-Server-ActiveSync'
) )
); );
// If useEASforOutlook == no, the autodiscoverType option will be replaced to imap.
/* ---------- DO NOT MODIFY ANYTHING BEYOND THIS LINE. IGNORE AT YOUR OWN RISK. ---------- */
if ($config['useEASforOutlook'] == 'no') { if ($config['useEASforOutlook'] == 'no') {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Outlook')) { if (strpos($_SERVER['HTTP_USER_AGENT'], 'Outlook')) {
$config['autodiscoverType'] = 'imap'; $config['autodiscoverType'] = 'imap';
} }
} }
// Workaround for short open tags require_once 'inc/vars.inc.php';
echo '<?xml version="1.0" encoding="utf-8" ?>'; include_once 'inc/vars.local.inc.php';
?> require_once 'inc/functions.inc.php';
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<?php $dsn = "$database_type:host=$database_host;dbname=$database_name";
$data = trim(file_get_contents("php://input")); $opt = [
if(!$data) { PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $database_user, $database_pass, $opt);
$login_user = strtolower(trim($_SERVER['PHP_AUTH_USER']));
$as = check_login($login_user, $_SERVER['PHP_AUTH_PW']);
if (!isset($_SERVER['PHP_AUTH_USER']) OR $as !== "user") {
header('WWW-Authenticate: Basic realm=""');
header('HTTP/1.0 401 Unauthorized');
exit;
} else {
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
if ($as === "user") {
header("Content-Type: application/xml");
echo '<?xml version="1.0" encoding="utf-8" ?><Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">';
$data = trim(file_get_contents("php://input"));
if(!$data) {
list($usec, $sec) = explode(' ', microtime()); list($usec, $sec) = explode(' ', microtime());
echo '<Response>'; echo '<Response>';
echo '<Error Time="' . date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2) . '" Id="2477272013">'; echo '<Error Time="' . date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2) . '" Id="2477272013">';
@ -38,84 +57,81 @@ if(!$data) {
echo '</Response>'; echo '</Response>';
echo '</Autodiscover>'; echo '</Autodiscover>';
exit(0); exit(0);
} }
$discover = new SimpleXMLElement($data);
$email = $discover->Request->EMailAddress;
$discover = new SimpleXMLElement($data); if ($config['autodiscoverType'] == 'imap') {
$email = $discover->Request->EMailAddress; ?>
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
if ($config['autodiscoverType'] == 'imap') { <Account>
?> <AccountType>email</AccountType>
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"> <Action>settings</Action>
<Account> <Protocol>
<AccountType>email</AccountType> <Type>IMAP</Type>
<Action>settings</Action> <Server><?php echo $config['imap']['server']; ?></Server>
<Protocol> <Port><?php echo $config['imap']['port']; ?></Port>
<Type>IMAP</Type> <DomainRequired>off</DomainRequired>
<Server><?php echo $config['imap']['server']; ?></Server> <LoginName><?php echo $email; ?></LoginName>
<Port><?php echo $config['imap']['port']; ?></Port> <SPA>off</SPA>
<DomainRequired>off</DomainRequired> <SSL><?php echo $config['imap']['ssl']; ?></SSL>
<LoginName><?php echo $email; ?></LoginName> <AuthRequired>on</AuthRequired>
<SPA>off</SPA> </Protocol>
<SSL><?php echo $config['imap']['ssl']; ?></SSL> <Protocol>
<AuthRequired>on</AuthRequired> <Type>SMTP</Type>
</Protocol> <Server><?php echo $config['smtp']['server']; ?></Server>
<Protocol> <Port><?php echo $config['smtp']['port']; ?></Port>
<Type>SMTP</Type> <DomainRequired>off</DomainRequired>
<Server><?php echo $config['smtp']['server']; ?></Server> <LoginName><?php echo $email; ?></LoginName>
<Port><?php echo $config['smtp']['port']; ?></Port> <SPA>off</SPA>
<DomainRequired>off</DomainRequired> <SSL><?php echo $config['smtp']['ssl']; ?></SSL>
<LoginName><?php echo $email; ?></LoginName> <AuthRequired>on</AuthRequired>
<SPA>off</SPA> <UsePOPAuth>on</UsePOPAuth>
<SSL><?php echo $config['smtp']['ssl']; ?></SSL> <SMTPLast>off</SMTPLast>
<AuthRequired>on</AuthRequired> </Protocol>
<UsePOPAuth>on</UsePOPAuth> </Account>
<SMTPLast>off</SMTPLast> </Response>
</Protocol> <?php
</Account> }
</Response> else if ($config['autodiscoverType'] == 'activesync') {
<?php $username = trim($email);
} try {
else if ($config['autodiscoverType'] == 'activesync') { $stmt = $pdo->prepare("SELECT `name` FROM `mailbox` WHERE `username`= :username");
$dsn = "$database_type:host=$database_host;dbname=$database_name"; $stmt->execute(array(':username' => $username));
$opt = [ $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, }
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, catch(PDOException $e) {
PDO::ATTR_EMULATE_PREPARES => false, die("Failed to determine name from SQL");
]; }
$pdo = new PDO($dsn, $database_user, $database_pass, $opt); if (!empty($MailboxData['name'])) {
$username = trim($email); $displayname = utf8_encode($MailboxData['name']);
try { }
$stmt = $pdo->prepare("SELECT `name` FROM `mailbox` WHERE `username`= :username"); else {
$stmt->execute(array(':username' => $username)); $displayname = $email;
$MailboxData = $stmt->fetch(PDO::FETCH_ASSOC); }
} ?>
catch(PDOException $e) { <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006">
die("Failed to determine name from SQL"); <Culture>en:en</Culture>
} <User>
if (!empty($MailboxData['name'])) { <DisplayName><?php echo $displayname; ?></DisplayName>
$displayname = utf8_encode($MailboxData['name']); <EMailAddress><?php echo $email; ?></EMailAddress>
} </User>
else { <Action>
$displayname = $email; <Settings>
} <Server>
?> <Type>MobileSync</Type>
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006"> <Url><?php echo $config['activesync']['url']; ?></Url>
<Culture>en:en</Culture> <Name><?php echo $config['activesync']['url']; ?></Name>
<User> </Server>
<DisplayName><?php echo $displayname; ?></DisplayName> </Settings>
<EMailAddress><?php echo $email; ?></EMailAddress> </Action>
</User> </Response>
<Action> <?php
<Settings> }
<Server> ?>
<Type>MobileSync</Type>
<Url><?php echo $config['activesync']['url']; ?></Url>
<Name><?php echo $config['activesync']['url']; ?></Name>
</Server>
</Settings>
</Action>
</Response>
<?php
}
?>
</Autodiscover> </Autodiscover>
<?php
}
}
}
?>