Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 3 | // check for development mode |
| 4 | $DEV_MODE = (getenv('DEV_MODE') == 'y'); |
| 5 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 6 | // Slave does not serve UI |
| 7 | /* if (!preg_match('/y|yes/i', getenv('MASTER'))) { |
| 8 | header('Location: /SOGo', true, 307); |
| 9 | exit; |
| 10 | }*/ |
| 11 | |
| 12 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php'; |
| 13 | $default_autodiscover_config = $autodiscover_config; |
| 14 | |
| 15 | if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php')) { |
| 16 | include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php'; |
| 17 | } |
| 18 | unset($https_port); |
| 19 | $autodiscover_config = array_merge($default_autodiscover_config, $autodiscover_config); |
| 20 | |
| 21 | header_remove("X-Powered-By"); |
| 22 | |
| 23 | // Yubi OTP API |
| 24 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/Yubico.php'; |
| 25 | |
| 26 | // WebAuthn |
| 27 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/WebAuthn.php'; |
| 28 | |
| 29 | // Autoload composer |
| 30 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/vendor/autoload.php'; |
| 31 | |
| 32 | // Load Sieve |
| 33 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/sieve/SieveParser.php'; |
| 34 | |
| 35 | // minifierExtended |
| 36 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/JSminifierExtended.php'; |
| 37 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/CSSminifierExtended.php'; |
| 38 | |
| 39 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/array_merge_real.php'; |
| 40 | |
| 41 | // Minify JS |
| 42 | use MatthiasMullie\Minify; |
| 43 | $js_minifier = new JSminifierExtended(); |
| 44 | $js_dir = array_diff(scandir('/web/js/build'), array('..', '.')); |
| 45 | foreach ($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('..', '.')); |
| 52 | foreach ($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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 73 | $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 Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 75 | |
| 76 | // Redis |
| 77 | $redis = new Redis(); |
| 78 | try { |
| 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 | } |
| 86 | catch (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 |
| 90 | exit; |
| 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 | ]; |
| 110 | try { |
| 111 | $pdo = new PDO($dsn, $database_user, $database_pass, $opt); |
| 112 | } |
| 113 | catch (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 |
| 118 | exit; |
| 119 | } |
| 120 | // Stop when dockerapi is not available |
| 121 | if (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 |
| 125 | exit; |
| 126 | } |
| 127 | |
| 128 | // OAuth2 |
| 129 | class 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 | |
| 157 | function 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 Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 175 | if(!$DEV_MODE) { |
| 176 | set_exception_handler('exception_handler'); |
| 177 | } |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 178 | |
| 179 | // TODO: Move function |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 180 | function get_remote_ip() { |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 181 | $remote = $_SERVER['REMOTE_ADDR']; |
| 182 | if (filter_var($remote, FILTER_VALIDATE_IP) === false) { |
| 183 | return '0.0.0.0'; |
| 184 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 185 | return $remote; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Load core functions first |
| 189 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php'; |
| 190 | require_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 |
| 197 | if (!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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 200 | if (array_key_exists($header_lang, $AVAILABLE_LANGUAGES)) { |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 201 | $_SESSION['mailcow_locale'] = $header_lang; |
| 202 | } |
| 203 | } |
| 204 | else { |
| 205 | $_SESSION['mailcow_locale'] = strtolower(trim($DEFAULT_LANG)); |
| 206 | } |
| 207 | } |
| 208 | if (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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 211 | if (isset($_GET['lang']) && array_key_exists($_GET['lang'], $AVAILABLE_LANGUAGES)) { |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 212 | $_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'; |
| 222 | if(file_exists($langFile)) { |
| 223 | $lang = array_merge_real($lang, json_decode(file_get_contents($langFile), true)); |
| 224 | } |
| 225 | |
| 226 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.acl.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 227 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.address_rewriting.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 228 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.admin.inc.php'; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 229 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.app_passwd.inc.php'; |
| 230 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.customize.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 231 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.dkim.inc.php'; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 232 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.docker.inc.php'; |
| 233 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.domain_admin.inc.php'; |
| 234 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fail2ban.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 235 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fwdhost.inc.php'; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 236 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailbox.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 237 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailq.inc.php'; |
| 238 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.oauth2.inc.php'; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 239 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.policy.inc.php'; |
| 240 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.presets.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 241 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.pushover.inc.php'; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 242 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quarantine.inc.php'; |
| 243 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quota_notification.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 244 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.ratelimit.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 245 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.rspamd.inc.php'; |
| 246 | 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] | 247 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.transports.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 248 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/init_db.inc.php'; |
| 249 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/triggers.inc.php'; |
Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 250 | require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/twig.inc.php'; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 251 | init_db_schema(); |
| 252 | if (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'); |