Roundcube alongside Nextcloud

We will run Roundcube 1.3.3 in a subfolder alongside and within Nextcloud on your NGINX, enhance security using TOTP (2FA) + fail2ban and finally add functionality using a carddav plugin for Nextcloud contacts.


Updated: Nov., 27th, 2017:
– perform “sudo -u www-data touch /var/www/emails/plugins/errors”  before starting fail2ban initially


To run Roundcube (https://your.dedyn.io/emails) alongside and within Nextcloud (as an “external site”) your NGINX configuration has to be modified and enhanced.  But first backup your old *.conf files inside the nginx-directories. Please substitute all the red values below properly to your environment.

sudo -s
apt install php-pear -y && service php7.1-fpm restart && service nginx stop
cp /etc/nginx/conf.d/nextcloud.conf /etc/nginx/conf.d/nextcloud.conf.bak
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

Modify the /etc/nginx/conf.d/nextcloud.conf:

######################
#     NEXTCLOUD      #
######################
fastcgi_cache_path /usr/local/tmp/cache levels=1:2 keys_zone=NEXTCLOUD:100m inactive=60m;
map $request_uri $skip_cache {
 default 1;
 ~*/thumbnail.php 0;
 ~*/apps/galleryplus/ 0;
 ~*/apps/gallery/ 0;
}
server {
 listen 80 default_server;
 server_name nc.c-rieger.de;
 location ^~ /.well-known/acme-challenge {
 proxy_pass http://127.0.0.1:81;
 }
 location / {
 return 301 https://$host$request_uri;
 }
}
server {
 listen 443 ssl http2 default_server;
 server_name nc.c-rieger.de;
 root /var/www/nextcloud/;
 access_log /var/log/nginx/access.log main;
 error_log /var/log/nginx/error.log warn;
 location = /robots.txt {
 allow all;
 log_not_found off;
 access_log off;
 }
 location = /.well-known/carddav {
 return 301 $scheme://$host/remote.php/dav;
 }
 location = /.well-known/caldav {
 return 301 $scheme://$host/remote.php/dav;
 }
 client_max_body_size 10240M;
 location / {
 rewrite ^ /index.php$uri;
 }
 location ^~ /emails/ {
 proxy_pass http://127.0.0.1:83;
 }
 location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
 deny all;
 }
 location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
 deny all;
 }
 location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
 fastcgi_split_path_info ^(.+\.php)(/.*)$;
 include fastcgi_params;
 include php_optimization.conf;
 fastcgi_pass php-handler;
 fastcgi_param HTTPS on;
 fastcgi_cache_bypass $skip_cache;
 fastcgi_no_cache $skip_cache;
 fastcgi_cache NEXTCLOUD;
 }
 location ~ ^/(?:updater|ocs-provider)(?:$|/) {
 try_files $uri/ =404;
 index index.php;
 }
 location ~ \.(?:css|js|woff|svg|gif)$ {
 try_files $uri /index.php$uri$is_args$args;
 add_header Cache-Control "public, max-age=15778463";
 log_not_found off;
 access_log off;
 expires 30d;
 }
 location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
 try_files $uri /index.php$uri$is_args$args;
 log_not_found off;
 access_log off;
 expires 30d;
 }
}

Now create the new /etc/nginx/conf.d/roundcube.conf:

######################
#     Roundcube      # 
######################
server {
 server_name 127.0.0.1 default_server;
 listen 127.0.0.1:83;
 root /var/www/;
 client_max_body_size 1024M;
 index index.php;
 access_log /var/log/nginx/email.access.log main;
 error_log /var/log/nginx/email.error.log warn;
 charset utf-8;
 location /emails/ {
 try_files $uri $uri/ index.php;
 }
 location ~ ^/emails/(README|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ {
 deny all;
 }
 location ~ ^/emails/(bin|SQL|config|temp|logs)/ {
 deny all;
 }
 location ~* \.php$ {
 fastcgi_split_path_info ^(.+\.php)(/.*)$;
 include fastcgi_params;
 include php_optimization.conf;
 fastcgi_pass php-handler;
 fastcgi_param HTTPS on;
 fastcgi_index index.php;
 try_files $uri =404;
 }
}

Validate your NGINX configuration

nginx -t

and if no errors will appear just restart NGINX.

service nginx restart

To get your Roundcube started (https://YOUR.DEDYN.IO/emails), you need to download and install Roundcube:

cd /usr/local/src
wget https://github.com/roundcube/roundcubemail/releases/download/1.3.3/roundcubemail-1.3.3-complete.tar.gz
tar xfz roundcubemail-1.3.3-complete.tar.gz &&  mv roundcubemail-1.3.3 emails
mv emails /var/www/ && chown -R www-data:www-data /var/www/

Call Roundcube in your browser (https://your.dedyn.io/emails/installer/) and walk through the configuration steps. Don’t forget to remove the folder /var/www/emails/installer afterwards.

rm -R /var/www/emails/installer && service nginx restart

Having Roundcube installed (/var/www/emails) we will start harden roundcube using TOTP (2FA). Please ensure having switched into sudo mode and change into roundcube’s plugin directory:

cd /var/www/emails/plugins

Then download the TOTP-app from github:

git clone https://github.com/alexandregz/twofactor_gauthenticator.git

Logout from roundcube and edit the main configuration to enable TOTP:

sudo -u www-data vi ../config/config.inc.php

Add ‘twofactor_gauthenticator’ to the plugins-section:

$config['plugins'] = array('enigma', 'fail2ban', 'twofactor_gauthenticator', 'markasjunk', 'newmail_notifier', 'zipdownload');

Save and quit the file (:wq!) and logon to your roundcube instance. Then activate twofactor_gauthenticator in the settings-panel:

If you paste your Nextcloud secret and apply these settings you may logon to Roundcube using the same 2FA as for Nextcloud. Logout and re-login to roundcube again. From now your account is even more secure using a second factor for authentication.

Logout from roundcube and go ahead witht the implementation of fail2ban to prevent bruteforce attacks. Change to the plugin-directory again:

cd /var/www/emails/plugins

Download and extract the fail2ban-plugin:

wget https://github.com/mattrude/rc-plugin-fail2ban/archive/1.3.zip
unzip 1.3 && rm 1.3.zip

Then rename the plugin to fail2ban:

mv rc-plugin-fail2ban-1.3 fail2ban && chown -R www-data:www-data /var/www/emails

Create the fail2ban configuration for roundcube:

vi /etc/fail2ban/jail.d/roundcube.local

Add the following rows:

[roundcube]
enabled = true
port = 80,443
protocol = tcp
maxretry = 3
bantime = 3600
logpath = /var/www/emails/logs/errors

Save and quit the file (:wq!) and create the roundcube filter expressions for fail2ban:

vi /etc/fail2ban/filter.d/roundcube.conf

Add the following rows:

[Definition]
failregex = Login failed for *.* from 127.0.0.1\(X-Real-IP\: <HOST>
ignoreregex =

Logout from roundcube and edit the main configuration to enable fail2ban:

sudo -u www-data vi ../config/config.inc.php

Add ‘fail2ban’ to the plugins-section:

$config['plugins'] = array('enigma', 'fail2ban', 'twofactor_gauthenticator', 'markasjunk', 'newmail_notifier', 'zipdownload');

Restart fail2ban:

sudo -u www-data touch /var/www/emails/logs/errors && service fail2ban restart

Logon to your roundcube instance. Then verify fail2ban is working as expected.

fail2ban-client status nextcloud && fail2ban-client status roundcube

Re-logon to Nextcloud and roundcube using wrong credentials once. Then open the fail2ban-status again:

fail2ban-client status nextcloud && fail2ban-client status roundcube

If you’ll find e.g. “currently failed: 1” twice your Nextcloud and roundcube was successfully hardened with TOTP and fail2ban. At least we will add Nextcloud contacts to our roundcube instance using the carddav plugin. First logout from roundcube and change to the plugin directory again:

cd /var/www/emails/plugins

Download the plugin and extract it:

wget https://github.com/blind-coder/rcmcarddav/archive/master.zip && unzip master.zip

Rename the directory to carddav and change into that directory:

mv rcmcarddav-master carddav && rm master.zip && cd carddav

Download the binaries using curl:

curl -sS https://getcomposer.org/installer | php

Then execute the following command:

php composer.phar install

Copy the default config:

cp config.inc.php.dist config.inc.php && chown -R www-data:www-data /var/www/emails

Then modify pwstore_scheme to ‘des_key’:

sudo -u www-data vi config.inc.php

Change the ‘pwstore_scheme’-value:

...
$prefs['_GLOBAL']['pwstore_scheme'] = 'des_key';
...

Save and quit the file (:wq!) and edit roundcube’s main configuration:

sudo -u www-data vi ../../config/config.inc.php

Add ‘carddav’ to the plugins-section:

$config['plugins'] = array('carddav', 'enigma', 'fail2ban', 'twofactor_gauthenticator', 'markasjunk', 'newmail_notifier', 'zipdownload');

Save and quit the file (:wq!) and logon to your roundcube instance again. You will find the carddav option in roundcube’s settings panel.

The carddav-url looks like:

https://YOUR.DEDYN.IO/remote.php/dav/addressbooks/users/YourUser/contacts/

Please fill in your url and your app-password from your Nextcloud. If configured properly the configuration will appear as:

Adjust the examplarily carddav settings properly and enjoy your Roundcube beside and as an external site within your Nextcloud.

That’s it …


Carsten Rieger