Open the selected mailbox in Roundcube #73

Merged
parkel merged 2 commits from feature/roundcube-mailbox-signon into main 2026-11-21 20:52:27 +01:00
5 changed files with 271 additions and 0 deletions
Showing only changes of commit 653d365645 - Show all commits

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
<VirtualHost *:443>
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
<Directory "/opt/roundcube/public_html">
Options FollowSymLinks
AllowOverride None
DirectoryIndex index.php
Require all granted
<FilesMatch "^(index|static)\.php$">
SetHandler "proxy:unix:/run/php/coulobre-roundcube.sock|fcgi://localhost/"
</FilesMatch>
<FilesMatch "^(?!(index|static)\.php$).*\.ph(?:p|ar|tml)$">
Require all denied
</FilesMatch>
</Directory>
</VirtualHost>
+12
View File
@@ -0,0 +1,12 @@
<?php
$config['plugins'][] = 'coulobre_webmail';
$config['coulobre_panel_origin'] = 'https://panel.example.org';
$config['coulobre_bridge_key_file'] = '/etc/coulobre-roundcube/bridge.key';
$config['imap_host'] = 'ssl://mail.example.org:993';
$config['imap_conn_options'] = [
'ssl' => ['verify_peer' => true, 'verify_peer_name' => true],
];
$config['force_https'] = true;
$config['session_samesite'] = 'Lax';
+151
View File
@@ -0,0 +1,151 @@
<?php
declare(strict_types=1);
/**
* Accept a single-use Coulobre ticket and use Roundcube's normal IMAP login.
*/
class coulobre_webmail extends rcube_plugin
{
private ?array $login = null;
public function init(): void
{
$this->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.');
}
}
+15
View File
@@ -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
+70
View File
@@ -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