blob: 0cbd05a0f698d370b35f1d2ae32ace5c6b18056b [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3// Slave does not serve UI
4/* if (!preg_match('/y|yes/i', getenv('MASTER'))) {
5 header('Location: /SOGo', true, 307);
6 exit;
7}*/
8
9require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php';
10$default_autodiscover_config = $autodiscover_config;
11
12if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php')) {
13 include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php';
14}
15unset($https_port);
16$autodiscover_config = array_merge($default_autodiscover_config, $autodiscover_config);
17
18header_remove("X-Powered-By");
19
20// Yubi OTP API
21require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/Yubico.php';
22
23// WebAuthn
24require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/WebAuthn.php';
25
26// Autoload composer
27require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/vendor/autoload.php';
28
29// Load Sieve
30require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/sieve/SieveParser.php';
31
32// minifierExtended
33require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/JSminifierExtended.php';
34require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/CSSminifierExtended.php';
35
36require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/array_merge_real.php';
37
38// Minify JS
39use MatthiasMullie\Minify;
40$js_minifier = new JSminifierExtended();
41$js_dir = array_diff(scandir('/web/js/build'), array('..', '.'));
42foreach ($js_dir as $js_file) {
43 $js_minifier->add('/web/js/build/' . $js_file);
44}
45
46// Minify CSS
47$css_minifier = new CSSminifierExtended();
48$css_dir = array_diff(scandir('/web/css/build'), array('..', '.'));
49foreach ($css_dir as $css_file) {
50 $css_minifier->add('/web/css/build/' . $css_file);
51}
52
53// U2F API + T/HOTP API
54$u2f = new u2flib_server\U2F('https://' . $_SERVER['HTTP_HOST']);
55$qrprovider = new RobThree\Auth\Providers\Qr\QRServerProvider();
56$tfa = new RobThree\Auth\TwoFactorAuth($OTP_LABEL, 6, 30, 'sha1', $qrprovider);
57
58// FIDO2
59$formats = $GLOBALS['FIDO2_FORMATS'];
60$WebAuthn = new \WebAuthn\WebAuthn('WebAuthn Library', $_SERVER['HTTP_HOST'], $formats);
61$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/solo.pem');
62$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/apple.pem');
63$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/nitro.pem');
64$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/yubico.pem');
65$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/hypersecu.pem');
66$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/globalSign.pem');
67$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/googleHardware.pem');
68$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/microsoftTpmCollection.pem');
69$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/huawei.pem');
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020070$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/trustkey.pem');
71$WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates/bsi.pem');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010072
73// Redis
74$redis = new Redis();
75try {
76 if (!empty(getenv('REDIS_SLAVEOF_IP'))) {
77 $redis->connect(getenv('REDIS_SLAVEOF_IP'), getenv('REDIS_SLAVEOF_PORT'));
78 }
79 else {
80 $redis->connect('redis-mailcow', 6379);
81 }
82}
83catch (Exception $e) {
84?>
85<center style='font-family:sans-serif;'>Connection to Redis failed.<br /><br />The following error was reported:<br/><?=$e->getMessage();?></center>
86<?php
87exit;
88}
89
90// PDO
91// Calculate offset
92// $now = new DateTime();
93// $mins = $now->getOffset() / 60;
94// $sgn = ($mins < 0 ? -1 : 1);
95// $mins = abs($mins);
96// $hrs = floor($mins / 60);
97// $mins -= $hrs * 60;
98// $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
99
100$dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
101$opt = [
102 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
103 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
104 PDO::ATTR_EMULATE_PREPARES => false,
105 //PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '" . $offset . "', group_concat_max_len = 3423543543;",
106];
107try {
108 $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
109}
110catch (PDOException $e) {
111// Stop when SQL connection fails
112?>
113<center style='font-family:sans-serif;'>Connection to database failed.<br /><br />The following error was reported:<br/> <?=$e->getMessage();?></center>
114<?php
115exit;
116}
117// Stop when dockerapi is not available
118if (fsockopen("tcp://dockerapi", 443, $errno, $errstr) === false) {
119?>
120<center style='font-family:sans-serif;'>Connection to dockerapi container failed.<br /><br />The following error was reported:<br/><?=$errno;?> - <?=$errstr;?></center>
121<?php
122exit;
123}
124
125// OAuth2
126class mailcowPdo extends OAuth2\Storage\Pdo {
127 public function __construct($connection, $config = array()) {
128 parent::__construct($connection, $config);
129 $this->config['user_table'] = 'mailbox';
130 }
131 public function checkUserCredentials($username, $password) {
132 if (check_login($username, $password) == 'user') {
133 return true;
134 }
135 return false;
136 }
137 public function getUserDetails($username) {
138 return $this->getUser($username);
139 }
140}
141$oauth2_scope_storage = new OAuth2\Storage\Memory(array('default_scope' => 'profile', 'supported_scopes' => array('profile')));
142$oauth2_storage = new mailcowPdo(array('dsn' => $dsn, 'username' => $database_user, 'password' => $database_pass));
143$oauth2_server = new OAuth2\Server($oauth2_storage, array(
144 'refresh_token_lifetime' => $REFRESH_TOKEN_LIFETIME,
145 'access_lifetime' => $ACCESS_TOKEN_LIFETIME,
146));
147$oauth2_server->setScopeUtil(new OAuth2\Scope($oauth2_scope_storage));
148$oauth2_server->addGrantType(new OAuth2\GrantType\AuthorizationCode($oauth2_storage));
149$oauth2_server->addGrantType(new OAuth2\GrantType\UserCredentials($oauth2_storage));
150$oauth2_server->addGrantType(new OAuth2\GrantType\RefreshToken($oauth2_storage, array(
151 'always_issue_new_refresh_token' => true
152)));
153
154function exception_handler($e) {
155 if ($e instanceof PDOException) {
156 $_SESSION['return'][] = array(
157 'type' => 'danger',
158 'log' => array(__FUNCTION__),
159 'msg' => array('mysql_error', $e)
160 );
161 return false;
162 }
163 else {
164 $_SESSION['return'][] = array(
165 'type' => 'danger',
166 'log' => array(__FUNCTION__),
167 'msg' => 'An unknown error occured: ' . print_r($e, true)
168 );
169 return false;
170 }
171}
172set_exception_handler('exception_handler');
173
174// TODO: Move function
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200175function get_remote_ip() {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100176 $remote = $_SERVER['REMOTE_ADDR'];
177 if (filter_var($remote, FILTER_VALIDATE_IP) === false) {
178 return '0.0.0.0';
179 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200180 return $remote;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100181}
182
183// Load core functions first
184require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';
185require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/sessions.inc.php';
186
187// IMAP lib
188// use Ddeboer\Imap\Server;
189// $imap_server = new Server('dovecot', 143, '/imap/tls/novalidate-cert');
190
191// Set language
192if (!isset($_SESSION['mailcow_locale']) && !isset($_COOKIE['mailcow_locale'])) {
193 if ($DETECT_LANGUAGE && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
194 $header_lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200195 if (array_key_exists($header_lang, $AVAILABLE_LANGUAGES)) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100196 $_SESSION['mailcow_locale'] = $header_lang;
197 }
198 }
199 else {
200 $_SESSION['mailcow_locale'] = strtolower(trim($DEFAULT_LANG));
201 }
202}
203if (isset($_COOKIE['mailcow_locale'])) {
204 (preg_match('/^[a-z]{2}$/', $_COOKIE['mailcow_locale'])) ? $_SESSION['mailcow_locale'] = $_COOKIE['mailcow_locale'] : setcookie("mailcow_locale", "", time() - 300);
205}
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200206if (isset($_GET['lang']) && array_key_exists($_GET['lang'], $AVAILABLE_LANGUAGES)) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100207 $_SESSION['mailcow_locale'] = $_GET['lang'];
208 setcookie("mailcow_locale", $_GET['lang'], time()+30758400); // one year
209}
210
211/*
212 * load language
213 */
214$lang = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/lang/lang.en.json'), true);
215
216$langFile = $_SERVER['DOCUMENT_ROOT'] . '/lang/lang.'.$_SESSION['mailcow_locale'].'.json';
217if(file_exists($langFile)) {
218 $lang = array_merge_real($lang, json_decode(file_get_contents($langFile), true));
219}
220
221require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.acl.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100222require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.address_rewriting.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100223require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.admin.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200224require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.app_passwd.inc.php';
225require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.customize.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100226require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.dkim.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200227require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.docker.inc.php';
228require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.domain_admin.inc.php';
229require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fail2ban.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100230require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fwdhost.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200231require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailbox.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100232require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailq.inc.php';
233require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.oauth2.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200234require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.policy.inc.php';
235require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.presets.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100236require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.pushover.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200237require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quarantine.inc.php';
238require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quota_notification.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100239require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.ratelimit.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100240require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.rspamd.inc.php';
241require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.tls_policy_maps.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200242require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.transports.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100243require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/init_db.inc.php';
244require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/triggers.inc.php';
245init_db_schema();
246if (isset($_SESSION['mailcow_cc_role'])) {
247 // if ($_SESSION['mailcow_cc_role'] == 'user') {
248 // list($master_user, $master_passwd) = explode(':', file_get_contents('/etc/sogo/sieve.creds'));
249 // $imap_connection = $imap_server->authenticate($_SESSION['mailcow_cc_username'] . '*' . trim($master_user), trim($master_passwd));
250 // $master_user = $master_passwd = null;
251 // }
252 acl('to_session');
253}
254$UI_TEXTS = customize('get', 'ui_texts');