| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?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 |  | 
|  | 9 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php'; | 
|  | 10 | $default_autodiscover_config = $autodiscover_config; | 
|  | 11 |  | 
|  | 12 | if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php')) { | 
|  | 13 | include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php'; | 
|  | 14 | } | 
|  | 15 | unset($https_port); | 
|  | 16 | $autodiscover_config = array_merge($default_autodiscover_config, $autodiscover_config); | 
|  | 17 |  | 
|  | 18 | header_remove("X-Powered-By"); | 
|  | 19 |  | 
|  | 20 | // Yubi OTP API | 
|  | 21 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/Yubico.php'; | 
|  | 22 |  | 
|  | 23 | // WebAuthn | 
|  | 24 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/WebAuthn.php'; | 
|  | 25 |  | 
|  | 26 | // Autoload composer | 
|  | 27 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/vendor/autoload.php'; | 
|  | 28 |  | 
|  | 29 | // Load Sieve | 
|  | 30 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/sieve/SieveParser.php'; | 
|  | 31 |  | 
|  | 32 | // minifierExtended | 
|  | 33 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/JSminifierExtended.php'; | 
|  | 34 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/CSSminifierExtended.php'; | 
|  | 35 |  | 
|  | 36 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/array_merge_real.php'; | 
|  | 37 |  | 
|  | 38 | // Minify JS | 
|  | 39 | use MatthiasMullie\Minify; | 
|  | 40 | $js_minifier = new JSminifierExtended(); | 
|  | 41 | $js_dir = array_diff(scandir('/web/js/build'), array('..', '.')); | 
|  | 42 | foreach ($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('..', '.')); | 
|  | 49 | foreach ($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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 70 | $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 Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 72 |  | 
|  | 73 | // Redis | 
|  | 74 | $redis = new Redis(); | 
|  | 75 | try { | 
|  | 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 | } | 
|  | 83 | catch (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 | 
|  | 87 | exit; | 
|  | 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 | ]; | 
|  | 107 | try { | 
|  | 108 | $pdo = new PDO($dsn, $database_user, $database_pass, $opt); | 
|  | 109 | } | 
|  | 110 | catch (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 | 
|  | 115 | exit; | 
|  | 116 | } | 
|  | 117 | // Stop when dockerapi is not available | 
|  | 118 | if (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 | 
|  | 122 | exit; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | // OAuth2 | 
|  | 126 | class 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 |  | 
|  | 154 | function 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 | } | 
|  | 172 | set_exception_handler('exception_handler'); | 
|  | 173 |  | 
|  | 174 | // TODO: Move function | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 175 | function get_remote_ip() { | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 176 | $remote = $_SERVER['REMOTE_ADDR']; | 
|  | 177 | if (filter_var($remote, FILTER_VALIDATE_IP) === false) { | 
|  | 178 | return '0.0.0.0'; | 
|  | 179 | } | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 180 | return $remote; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
|  | 183 | // Load core functions first | 
|  | 184 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php'; | 
|  | 185 | require_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 | 
|  | 192 | if (!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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 195 | if (array_key_exists($header_lang, $AVAILABLE_LANGUAGES)) { | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 196 | $_SESSION['mailcow_locale'] = $header_lang; | 
|  | 197 | } | 
|  | 198 | } | 
|  | 199 | else { | 
|  | 200 | $_SESSION['mailcow_locale'] = strtolower(trim($DEFAULT_LANG)); | 
|  | 201 | } | 
|  | 202 | } | 
|  | 203 | if (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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 206 | if (isset($_GET['lang']) && array_key_exists($_GET['lang'], $AVAILABLE_LANGUAGES)) { | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 207 | $_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'; | 
|  | 217 | if(file_exists($langFile)) { | 
|  | 218 | $lang = array_merge_real($lang, json_decode(file_get_contents($langFile), true)); | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.acl.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 222 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.address_rewriting.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 223 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.admin.inc.php'; | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 224 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.app_passwd.inc.php'; | 
|  | 225 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.customize.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 226 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.dkim.inc.php'; | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 227 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.docker.inc.php'; | 
|  | 228 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.domain_admin.inc.php'; | 
|  | 229 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fail2ban.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 230 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fwdhost.inc.php'; | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 231 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailbox.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 232 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailq.inc.php'; | 
|  | 233 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.oauth2.inc.php'; | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 234 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.policy.inc.php'; | 
|  | 235 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.presets.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 236 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.pushover.inc.php'; | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 237 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quarantine.inc.php'; | 
|  | 238 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quota_notification.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 239 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.ratelimit.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 240 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.rspamd.inc.php'; | 
|  | 241 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.tls_policy_maps.inc.php'; | 
| Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 242 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.transports.inc.php'; | 
| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 243 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/init_db.inc.php'; | 
|  | 244 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/triggers.inc.php'; | 
|  | 245 | init_db_schema(); | 
|  | 246 | if (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'); |