From 653d36564528a480a36a5aeb38523dacc4c37138 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 20:18:11 +0100 Subject: [PATCH] Document and package the Roundcube sign-on plugin --- deploy/roundcube/apache.conf.example | 23 ++++ deploy/roundcube/config.inc.php.example | 12 ++ deploy/roundcube/coulobre_webmail.php | 151 ++++++++++++++++++++++++ deploy/roundcube/pool.conf.example | 15 +++ docs/deployment.md | 70 +++++++++++ 5 files changed, 271 insertions(+) create mode 100644 deploy/roundcube/apache.conf.example create mode 100644 deploy/roundcube/config.inc.php.example create mode 100644 deploy/roundcube/coulobre_webmail.php create mode 100644 deploy/roundcube/pool.conf.example diff --git a/deploy/roundcube/apache.conf.example b/deploy/roundcube/apache.conf.example new file mode 100644 index 0000000..e2ae0cb --- /dev/null +++ b/deploy/roundcube/apache.conf.example @@ -0,0 +1,23 @@ + + ServerName webmail.example.org + DocumentRoot /opt/roundcube/public_html + + SSLEngine On + SSLCertificateFile /etc/ssl/coulobre-webmail/fullchain.pem + SSLCertificateKeyFile /etc/ssl/coulobre-webmail/privkey.pem + + + Options FollowSymLinks + AllowOverride None + DirectoryIndex index.php + Require all granted + + + SetHandler "proxy:unix:/run/php/coulobre-roundcube.sock|fcgi://localhost/" + + + + Require all denied + + + diff --git a/deploy/roundcube/config.inc.php.example b/deploy/roundcube/config.inc.php.example new file mode 100644 index 0000000..1f94133 --- /dev/null +++ b/deploy/roundcube/config.inc.php.example @@ -0,0 +1,12 @@ + ['verify_peer' => true, 'verify_peer_name' => true], +]; +$config['force_https'] = true; +$config['session_samesite'] = 'Lax'; diff --git a/deploy/roundcube/coulobre_webmail.php b/deploy/roundcube/coulobre_webmail.php new file mode 100644 index 0000000..669d630 --- /dev/null +++ b/deploy/roundcube/coulobre_webmail.php @@ -0,0 +1,151 @@ +add_hook('startup', [$this, 'startSignOn']); + $this->add_hook('authenticate', [$this, 'authenticateSignOn']); + } + + public function startSignOn(array $args): array + { + if (!array_key_exists('_coulobre_ticket', $_POST)) { + return $args; + } + + $roundcube = rcmail::get_instance(); + $origin = $roundcube->config->get('coulobre_panel_origin', ''); + if ( + !is_string($origin) + || preg_match('~\Ahttps://(?:[a-z0-9.-]+|\[[a-f0-9:]+\])(?::[0-9]{1,5})?\z~iD', $origin) !== 1 + ) { + $this->reject(503); + } + + if ( + ($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST' + || !rcube_utils::https_check() + || ($_SERVER['HTTP_ORIGIN'] ?? '') !== $origin + ) { + $this->reject(403); + } + + $ticket = $_POST['_coulobre_ticket']; + if ( + count($_POST) !== 1 + || !is_string($ticket) + || preg_match('/\A[A-Za-z0-9_-]{43}\z/D', $ticket) !== 1 + ) { + $this->reject(400); + } + + // Use the configured mail service for the selected address. + $host = $roundcube->config->get('imap_host'); + if ( + !is_string($host) + || preg_match('~\A(?:ssl|tls)://(?:[a-z0-9.-]+|\[[a-f0-9:]+\])(?::[0-9]{1,5})?\z~iD', $host) !== 1 + ) { + $this->reject(503); + } + $keyPath = $roundcube->config->get('coulobre_bridge_key_file', ''); + $key = is_string($keyPath) && is_readable($keyPath) + ? trim((string) @file_get_contents($keyPath, false, null, 0, 512)) + : ''; + if ( + !function_exists('curl_init') + || preg_match('/\A[A-Za-z0-9_-]{32,128}\z/D', $key) !== 1 + ) { + $this->reject(503); + } + + $this->login = $this->exchangeTicket($origin, $key, $ticket); + $this->login['host'] = $host; + + // Reset the previous mailbox session after the ticket is accepted. + $roundcube->kill_session(); + return ['task' => 'login', 'action' => 'login'] + $args; + } + + public function authenticateSignOn(array $args): array + { + if ($this->login === null) { + return $args; + } + + $login = $this->login; + $this->login = null; + return [ + 'host' => $login['host'], + 'user' => $login['user'], + 'pass' => $login['password'], + 'valid' => true, + // A first visit or a cross-site POST may have no Roundcube cookie. + 'cookiecheck' => false, + ] + $args; + } + + private function exchangeTicket(string $origin, string $key, string $ticket): array + { + $connection = curl_init($origin . '/_bridge/roundcube/'); + $body = ''; + curl_setopt_array($connection, [ + CURLOPT_POST => true, + CURLOPT_FOLLOWLOCATION => false, + CURLOPT_SSL_VERIFYPEER => true, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_CONNECTTIMEOUT => 4, + CURLOPT_TIMEOUT => 20, + CURLOPT_HTTPHEADER => [ + 'Accept: application/json', + 'Content-Type: application/x-www-form-urlencoded', + 'X-Coulobre-Bridge-Key: ' . $key, + ], + CURLOPT_POSTFIELDS => http_build_query(['ticket' => $ticket]), + CURLOPT_WRITEFUNCTION => static function ($connection, string $chunk) use (&$body): int { + if (strlen($body) + strlen($chunk) > 4096) { + return 0; + } + $body .= $chunk; + return strlen($chunk); + }, + ]); + $success = curl_exec($connection); + $status = curl_getinfo($connection, CURLINFO_RESPONSE_CODE); + if ($status === 404) { + $this->reject(403); + } + if (!$success || $status !== 200) { + $this->reject(503); + } + + $login = json_decode($body, true); + if ( + !is_array($login) + || !is_string($login['user'] ?? null) + || strlen($login['user']) > 254 + || preg_match('/\A[a-z0-9][a-z0-9._-]*@[a-z0-9.-]+\z/D', $login['user']) !== 1 + || !is_string($login['password'] ?? null) + || preg_match('/\A[A-Za-z0-9_-]{43}\z/D', $login['password']) !== 1 + ) { + $this->reject(502); + } + return $login; + } + + private function reject(int $status): never + { + http_response_code($status); + header('Cache-Control: private, no-store'); + header('Content-Type: text/plain; charset=UTF-8'); + header('Referrer-Policy: no-referrer'); + exit('Webmail sign-in could not be completed. Open the mailbox from Coulobre Panel again.'); + } +} diff --git a/deploy/roundcube/pool.conf.example b/deploy/roundcube/pool.conf.example new file mode 100644 index 0000000..4207bb3 --- /dev/null +++ b/deploy/roundcube/pool.conf.example @@ -0,0 +1,15 @@ +[coulobre-roundcube] +user = coulobre-roundcube +group = coulobre-roundcube +listen = /run/php/coulobre-roundcube.sock +listen.owner = www-data +listen.group = www-data +listen.mode = 0660 +pm = ondemand +pm.max_children = 4 +clear_env = yes +security.limit_extensions = .php +php_admin_flag[display_errors] = off +php_admin_flag[session.use_strict_mode] = on +php_admin_flag[session.cookie_secure] = on +php_admin_flag[session.cookie_httponly] = on diff --git a/docs/deployment.md b/docs/deployment.md index 155e10e..033ed42 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -840,6 +840,76 @@ managed login. Existing database grants scope the session to the selected database. Sign out of phpMyAdmin to end its session. [phpMyAdmin signon mode](https://docs.phpmyadmin.net/en/latest/config.html#example-for-signon-authentication) +### Open a mailbox in Roundcube + +On Ubuntu 26.04 with PHP 8.5, use the current Roundcube 1.7 Complete +release from [Roundcube Downloads](https://roundcube.net/download/). Verify +the archive and install Roundcube in `/opt/roundcube` following the +[upstream manual setup](https://github.com/roundcube/roundcubemail/wiki/Installation). +The Ubuntu 26.04 package at version 1.6.11 cannot start with PHP 8.5. +Install the required PHP extensions for Roundcube and its database, as +well as `php8.5-curl` for the bridge. + +Use the mail server already configured for Coulobre. Confirm that a +mailbox can sign in over TLS using its full email address. Configure a +single IMAP host with a trusted certificate and configure Roundcube's SMTP +submission settings for sending mail. The same setup works with the panel's +local or remote [mailbox credential service](#enable-connection-details-for-remote-mailboxes). + +Use a dedicated PHP-FPM account. Grant that account read access to the +Roundcube configuration and bridge key, access to the database, and write +access to the Roundcube temporary files and logs. Use the same account for +Roundcube maintenance jobs. Keep the application code and configuration +owned by root. + +```sh +sudo useradd --system --user-group --no-create-home --shell /usr/sbin/nologin coulobre-roundcube +sudo install -d -o root -g coulobre-roundcube -m 0750 /etc/coulobre-roundcube +sudo install -o root -g coulobre-roundcube -m 0640 /dev/null /etc/coulobre-roundcube/bridge.key +sudo sh -c 'openssl rand -hex 32 > /etc/coulobre-roundcube/bridge.key' +sudo install -D -m 0644 deploy/roundcube/coulobre_webmail.php \ + /opt/roundcube/plugins/coulobre_webmail/coulobre_webmail.php +sudo install -m 0644 deploy/roundcube/pool.conf.example \ + /etc/php/8.5/fpm/pool.d/coulobre-roundcube.conf +sudo install -o root -g coulobre-roundcube -m 0640 deploy/roundcube/config.inc.php.example \ + /etc/coulobre-roundcube/webmail.inc.php +sudoedit /etc/coulobre-roundcube/webmail.inc.php +``` + +Set the panel origin to the exact HTTPS origin users visit, including a +non-default port if one is used. Set `imap_host` to the mail server's TLS +endpoint. Include the example from the end of Roundcube's +`/opt/roundcube/config/config.inc.php`. + +```php +require '/etc/coulobre-roundcube/webmail.inc.php'; +``` + +Configure the webmail site with `deploy/roundcube/apache.conf.example`. +Substitute the hostname and certificate paths, enable Apache's `ssl` and +`proxy_fcgi` modules, and enable the site. The example serves +`/opt/roundcube/public_html` through the dedicated FPM socket and blocks +the web installer. Run the Apache configuration test and reload Apache +and PHP-FPM. Keep certificate verification enabled between Roundcube and +both the panel and the IMAP server. + +On the panel, set the following values in `/etc/coulobre/panel.env`. Use the +same random key from `bridge.key` for Roundcube only. Set the webmail URL to +the canonical HTTPS base URL so a form POST can arrive without a redirect. + +```ini +COULOBRE_ROUNDCUBE_URL=https://webmail.example.org +COULOBRE_ROUNDCUBE_BRIDGE_KEY= +``` + +Run `panel_manage migrate --noinput`, collect static files, and restart +`coulobre-panel`. Use **Open webmail** beside a mailbox in the account +view. The browser carries a single-use ticket valid for 45 seconds. +Roundcube exchanges the ticket with Coulobre over HTTPS and uses its +ordinary IMAP login. The exchange appears in **Service credential +accesses**. Opening another mailbox in the same browser switches the +current Roundcube session. + ## Publish local DNS zones from the panel To publish to BIND9 on another host, use the