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