autodiscover.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php';
  3. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';
  4. $default_autodiscover_config = $autodiscover_config;
  5. if(file_exists('inc/vars.local.inc.php')) {
  6. include_once 'inc/vars.local.inc.php';
  7. }
  8. $autodiscover_config = array_merge($default_autodiscover_config, $autodiscover_config);
  9. // Redis
  10. $redis = new Redis();
  11. try {
  12. if (!empty(getenv('REDIS_SLAVEOF_IP'))) {
  13. $redis->connect(getenv('REDIS_SLAVEOF_IP'), getenv('REDIS_SLAVEOF_PORT'));
  14. }
  15. else {
  16. $redis->connect('redis-mailcow', 6379);
  17. }
  18. }
  19. catch (Exception $e) {
  20. exit;
  21. }
  22. error_reporting(0);
  23. $data = trim(file_get_contents("php://input"));
  24. if (strpos($data, 'autodiscover/outlook/responseschema') !== false) {
  25. $autodiscover_config['autodiscoverType'] = 'imap';
  26. if ($autodiscover_config['useEASforOutlook'] == 'yes' &&
  27. // Office for macOS does not support EAS
  28. strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') === false &&
  29. // Outlook 2013 (version 15) or higher
  30. preg_match('/(Outlook|Office).+1[5-9]\./', $_SERVER['HTTP_USER_AGENT'])
  31. ) {
  32. $autodiscover_config['autodiscoverType'] = 'activesync';
  33. }
  34. }
  35. if (getenv('SKIP_SOGO') == "y") {
  36. $autodiscover_config['autodiscoverType'] = 'imap';
  37. }
  38. //$dsn = $database_type . ":host=" . $database_host . ";dbname=" . $database_name;
  39. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  40. $opt = [
  41. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  42. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  43. PDO::ATTR_EMULATE_PREPARES => false,
  44. ];
  45. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  46. $login_user = strtolower(trim($_SERVER['PHP_AUTH_USER']));
  47. $login_pass = trim(htmlspecialchars_decode($_SERVER['PHP_AUTH_PW']));
  48. if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW'])) {
  49. $json = json_encode(
  50. array(
  51. "time" => time(),
  52. "ua" => $_SERVER['HTTP_USER_AGENT'],
  53. "user" => "none",
  54. "service" => "Error: must be authenticated"
  55. )
  56. );
  57. $redis->lPush('AUTODISCOVER_LOG', $json);
  58. header('WWW-Authenticate: Basic realm="' . $_SERVER['HTTP_HOST'] . '"');
  59. header('HTTP/1.0 401 Unauthorized');
  60. exit(0);
  61. }
  62. $login_role = check_login($login_user, $login_pass);
  63. if ($login_role === "user") {
  64. header("Content-Type: application/xml");
  65. echo '<?xml version="1.0" encoding="utf-8" ?>' . PHP_EOL;
  66. ?>
  67. <Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
  68. <?php
  69. if(!$data) {
  70. try {
  71. $json = json_encode(
  72. array(
  73. "time" => time(),
  74. "ua" => $_SERVER['HTTP_USER_AGENT'],
  75. "user" => $_SERVER['PHP_AUTH_USER'],
  76. "service" => "Error: invalid or missing request data"
  77. )
  78. );
  79. $redis->lPush('AUTODISCOVER_LOG', $json);
  80. $redis->lTrim('AUTODISCOVER_LOG', 0, 100);
  81. }
  82. catch (RedisException $e) {
  83. $_SESSION['return'][] = array(
  84. 'type' => 'danger',
  85. 'msg' => 'Redis: '.$e
  86. );
  87. return false;
  88. }
  89. list($usec, $sec) = explode(' ', microtime());
  90. ?>
  91. <Response>
  92. <Error Time="<?=date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2);?>" Id="2477272013">
  93. <ErrorCode>600</ErrorCode>
  94. <Message>Invalid Request</Message>
  95. <DebugData />
  96. </Error>
  97. </Response>
  98. </Autodiscover>
  99. <?php
  100. exit(0);
  101. }
  102. try {
  103. $discover = new SimpleXMLElement($data);
  104. $email = $discover->Request->EMailAddress;
  105. } catch (Exception $e) {
  106. $email = $_SERVER['PHP_AUTH_USER'];
  107. }
  108. $username = trim($email);
  109. try {
  110. $stmt = $pdo->prepare("SELECT `name` FROM `mailbox` WHERE `username`= :username");
  111. $stmt->execute(array(':username' => $username));
  112. $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
  113. }
  114. catch(PDOException $e) {
  115. die("Failed to determine name from SQL");
  116. }
  117. if (!empty($MailboxData['name'])) {
  118. $displayname = $MailboxData['name'];
  119. }
  120. else {
  121. $displayname = $email;
  122. }
  123. try {
  124. $json = json_encode(
  125. array(
  126. "time" => time(),
  127. "ua" => $_SERVER['HTTP_USER_AGENT'],
  128. "user" => $_SERVER['PHP_AUTH_USER'],
  129. "service" => $autodiscover_config['autodiscoverType']
  130. )
  131. );
  132. $redis->lPush('AUTODISCOVER_LOG', $json);
  133. $redis->lTrim('AUTODISCOVER_LOG', 0, 100);
  134. }
  135. catch (RedisException $e) {
  136. $_SESSION['return'][] = array(
  137. 'type' => 'danger',
  138. 'msg' => 'Redis: '.$e
  139. );
  140. return false;
  141. }
  142. if ($autodiscover_config['autodiscoverType'] == 'imap') {
  143. ?>
  144. <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
  145. <User>
  146. <DisplayName><?=htmlspecialchars($displayname, ENT_XML1 | ENT_QUOTES, 'UTF-8');?></DisplayName>
  147. </User>
  148. <Account>
  149. <AccountType>email</AccountType>
  150. <Action>settings</Action>
  151. <Protocol>
  152. <Type>IMAP</Type>
  153. <Server><?=$autodiscover_config['imap']['server'];?></Server>
  154. <Port><?=$autodiscover_config['imap']['port'];?></Port>
  155. <DomainRequired>off</DomainRequired>
  156. <LoginName><?=$email;?></LoginName>
  157. <SPA>off</SPA>
  158. <SSL>on</SSL>
  159. <AuthRequired>on</AuthRequired>
  160. </Protocol>
  161. <Protocol>
  162. <Type>SMTP</Type>
  163. <Server><?=$autodiscover_config['smtp']['server'];?></Server>
  164. <Port><?=$autodiscover_config['smtp']['port'];?></Port>
  165. <DomainRequired>off</DomainRequired>
  166. <LoginName><?=$email;?></LoginName>
  167. <SPA>off</SPA>
  168. <SSL>on</SSL>
  169. <AuthRequired>on</AuthRequired>
  170. <UsePOPAuth>on</UsePOPAuth>
  171. <SMTPLast>off</SMTPLast>
  172. </Protocol>
  173. <?php
  174. if (getenv('SKIP_SOGO') != "y") {
  175. ?>
  176. <Protocol>
  177. <Type>CalDAV</Type>
  178. <Server>https://<?=$autodiscover_config['caldav']['server'];?><?php if ($autodiscover_config['caldav']['port'] != 443) echo ':'.$autodiscover_config['caldav']['port']; ?>/SOGo/dav/<?=$email;?>/</Server>
  179. <DomainRequired>off</DomainRequired>
  180. <LoginName><?=$email;?></LoginName>
  181. </Protocol>
  182. <Protocol>
  183. <Type>CardDAV</Type>
  184. <Server>https://<?=$autodiscover_config['carddav']['server'];?><?php if ($autodiscover_config['caldav']['port'] != 443) echo ':'.$autodiscover_config['carddav']['port']; ?>/SOGo/dav/<?=$email;?>/</Server>
  185. <DomainRequired>off</DomainRequired>
  186. <LoginName><?=$email;?></LoginName>
  187. </Protocol>
  188. <?php
  189. }
  190. ?>
  191. </Account>
  192. </Response>
  193. <?php
  194. }
  195. else if ($autodiscover_config['autodiscoverType'] == 'activesync') {
  196. ?>
  197. <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006">
  198. <Culture>en:en</Culture>
  199. <User>
  200. <DisplayName><?=htmlspecialchars($displayname, ENT_XML1 | ENT_QUOTES, 'UTF-8');?></DisplayName>
  201. <EMailAddress><?=$email;?></EMailAddress>
  202. </User>
  203. <Action>
  204. <Settings>
  205. <Server>
  206. <Type>MobileSync</Type>
  207. <Url><?=$autodiscover_config['activesync']['url'];?></Url>
  208. <Name><?=$autodiscover_config['activesync']['url'];?></Name>
  209. </Server>
  210. </Settings>
  211. </Action>
  212. </Response>
  213. <?php
  214. }
  215. ?>
  216. </Autodiscover>
  217. <?php
  218. }
  219. ?>