autodiscover.php 7.2 KB

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