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