From fa66b767afa73516358da0f8a9df161c2bb6d74e Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Mon, 26 Jul 2021 10:04:35 +0200 Subject: [PATCH 001/129] add debugging info to email admin tool --- management/email_administrator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/management/email_administrator.py b/management/email_administrator.py index 37822622..5b877f1c 100755 --- a/management/email_administrator.py +++ b/management/email_administrator.py @@ -2,7 +2,7 @@ # Reads in STDIN. If the stream is not empty, mail it to the system administrator. -import sys +import sys, traceback import html import smtplib @@ -29,6 +29,7 @@ try: content = sys.stdin.read().strip() except: print("error occured while cleaning input text") + traceback.print_exc() sys.exit(1) # If there's nothing coming in, just exit. From aa360ee0c454f135c5f96d3bd92e18239b9afb49 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Mon, 26 Jul 2021 10:05:19 +0200 Subject: [PATCH 002/129] setup home for dovecot user outside ho mail dir --- setup/mail-users.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/mail-users.sh b/setup/mail-users.sh index 48b3ef32..2d051afb 100755 --- a/setup/mail-users.sh +++ b/setup/mail-users.sh @@ -50,7 +50,7 @@ driver = sqlite connect = $db_path default_pass_scheme = SHA512-CRYPT password_query = SELECT email as user, password FROM users WHERE email='%u'; -user_query = SELECT email AS user, "mail" as uid, "mail" as gid, "$STORAGE_ROOT/mail/mailboxes/%d/%n" as home FROM users WHERE email='%u'; +user_query = SELECT email AS user, "mail" as uid, "mail" as gid, "$STORAGE_ROOT/mail/homes/%d/%n" as home FROM users WHERE email='%u'; iterate_query = SELECT email AS user FROM users; EOF chmod 0600 /etc/dovecot/dovecot-sql.conf.ext # per Dovecot instructions From 21ad26e452efebf5cfcac951ff9c723f9da6966a Mon Sep 17 00:00:00 2001 From: NewbieOrange Date: Thu, 29 Jul 2021 04:39:40 +0800 Subject: [PATCH 003/129] Disable auto-complete for 2FA code in the control panel login form (#2013) --- management/templates/login.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/templates/login.html b/management/templates/login.html index 4c432aae..19b23d3a 100644 --- a/management/templates/login.html +++ b/management/templates/login.html @@ -64,7 +64,7 @@ sudo management/cli.py user make-admin me@{{hostname}}
- +
Enter the six-digit code generated by your two factor authentication app.
From 128541d506ecddec816dc16e664cef134a53a9ed Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sat, 31 Jul 2021 21:36:38 +0200 Subject: [PATCH 004/129] add alternative sshd port to ssh jail --- setup/system.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/setup/system.sh b/setup/system.sh index 90acf1df..40d17087 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -356,6 +356,20 @@ cat conf/fail2ban/jails.conf \ cp -f conf/fail2ban/filter.d/* /etc/fail2ban/filter.d/ cp -f conf/fail2ban/jail.d/* /etc/fail2ban/jail.d/ +# If SSH port is not default, add the not default to the ssh jail +if [ ! -z "$SSH_PORT" ]; then + # create backup copy + cp -f /etc/fail2ban/jail.conf jail.conf.miab_old + + if [ "$SSH_PORT" != "22" ]; then + # Add alternative SSH port + sed -i "s/port[ ]\+=[ ]\+ssh$/port = ssh,$SSH_PORT/g" /etc/fail2ban/jail.conf + else + # Set SSH port to default + sed -i "s/port[ ]\+=[ ]\+ssh/port = ssh/g" /etc/fail2ban/jail.conf + fi +fi + # fail2ban should be able to look back far enough because we increased findtime of recidive jail tools/editconf.py /etc/fail2ban/fail2ban.conf dbpurgeage=7d From 104d40e8191bbc39185253d445cb52fb460a605d Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sat, 31 Jul 2021 21:42:57 +0200 Subject: [PATCH 005/129] add alternative sshd port to ssh jail --- setup/system.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup/system.sh b/setup/system.sh index 40d17087..b7ed1317 100755 --- a/setup/system.sh +++ b/setup/system.sh @@ -364,9 +364,11 @@ if [ ! -z "$SSH_PORT" ]; then if [ "$SSH_PORT" != "22" ]; then # Add alternative SSH port sed -i "s/port[ ]\+=[ ]\+ssh$/port = ssh,$SSH_PORT/g" /etc/fail2ban/jail.conf + sed -i "s/port[ ]\+=[ ]\+ssh$/port = ssh,$SSH_PORT/g" /etc/fail2ban/jail.d/geoipblock.conf else # Set SSH port to default sed -i "s/port[ ]\+=[ ]\+ssh/port = ssh/g" /etc/fail2ban/jail.conf + sed -i "s/port[ ]\+=[ ]\+ssh/port = ssh/g" /etc/fail2ban/jail.d/geoipblock.conf fi fi From f6450c1cae4d54385b88d6d99b83fdf36bb34181 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sat, 31 Jul 2021 21:43:25 +0200 Subject: [PATCH 006/129] update obsolete settings --- setup/mail-dovecot.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/setup/mail-dovecot.sh b/setup/mail-dovecot.sh index b50e28d5..63810c50 100755 --- a/setup/mail-dovecot.sh +++ b/setup/mail-dovecot.sh @@ -77,6 +77,13 @@ tools/editconf.py /etc/dovecot/conf.d/10-auth.conf \ disable_plaintext_auth=yes \ "auth_mechanisms=plain login" +# Generate DH parameters. Will take a long time, so only if they don't exist +if [[ ! -f /etc/dovecot/dh.pem ]]; then + openssl dhparam -out /etc/dovecot/dh.pem 4096 +fi + +chown mail:dovecot /etc/dovecot/dh.pem + # Enable SSL, specify the location of the SSL certificate and private key files. # Use Mozilla's "Intermediate" recommendations at https://ssl-config.mozilla.org/#server=dovecot&server-version=2.2.33&config=intermediate&openssl-version=1.1.1, # except that the current version of Dovecot does not have a TLSv1.3 setting, so we only use TLSv1.2. @@ -84,10 +91,9 @@ tools/editconf.py /etc/dovecot/conf.d/10-ssl.conf \ ssl=required \ "ssl_cert=<$STORAGE_ROOT/ssl/ssl_certificate.pem" \ "ssl_key=<$STORAGE_ROOT/ssl/ssl_private_key.pem" \ - "ssl_protocols=TLSv1.2" \ - "ssl_cipher_list=ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384" \ - "ssl_prefer_server_ciphers=no" \ - "ssl_dh_parameters_length=2048" + "ssl_min_protocol=TLSv1.2" \ + "ssl_prefer_server_ciphers=yes" \ + "ssl_dh= Date: Sun, 1 Aug 2021 21:52:37 +0200 Subject: [PATCH 007/129] update DH security to 4096 --- conf/nginx-ssl.conf | 2 +- management/web_update.py | 4 ++-- setup/mail-dovecot.sh | 11 ++--------- setup/mail-postfix.sh | 2 +- setup/ssl.sh | 10 +++++----- setup/system.sh | 2 +- 6 files changed, 12 insertions(+), 19 deletions(-) diff --git a/conf/nginx-ssl.conf b/conf/nginx-ssl.conf index 621973df..3623c5f6 100644 --- a/conf/nginx-ssl.conf +++ b/conf/nginx-ssl.conf @@ -2,7 +2,7 @@ # Note that these settings are repeated in the SMTP and IMAP configuration. # ssl_protocols has moved to nginx.conf in bionic, check there for enabled protocols. ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; -ssl_dhparam STORAGE_ROOT/ssl/dh2048.pem; +ssl_dhparam STORAGE_ROOT/ssl/dh4096.pem; # as recommended by http://nginx.org/en/docs/http/configuring_https_servers.html ssl_session_cache shared:SSL:50m; diff --git a/management/web_update.py b/management/web_update.py index c846f6e0..73ec4157 100644 --- a/management/web_update.py +++ b/management/web_update.py @@ -211,9 +211,9 @@ def make_domain_config(domain, templates, ssl_certificates, env): # Add the HSTS header. if hsts == "yes": - nginx_conf_extra += "\tadd_header Strict-Transport-Security \"max-age=15768000\" always;\n" + nginx_conf_extra += "\tadd_header Strict-Transport-Security \"max-age=31536000\" always;\n" elif hsts == "preload": - nginx_conf_extra += "\tadd_header Strict-Transport-Security \"max-age=15768000; includeSubDomains; preload\" always;\n" + nginx_conf_extra += "\tadd_header Strict-Transport-Security \"max-age=31536000; includeSubDomains; preload\" always;\n" # Add in any user customizations in the includes/ folder. nginx_conf_custom_include = os.path.join(env["STORAGE_ROOT"], "www", safe_domain_name(domain) + ".conf") diff --git a/setup/mail-dovecot.sh b/setup/mail-dovecot.sh index 63810c50..5482d04f 100755 --- a/setup/mail-dovecot.sh +++ b/setup/mail-dovecot.sh @@ -77,23 +77,16 @@ tools/editconf.py /etc/dovecot/conf.d/10-auth.conf \ disable_plaintext_auth=yes \ "auth_mechanisms=plain login" -# Generate DH parameters. Will take a long time, so only if they don't exist -if [[ ! -f /etc/dovecot/dh.pem ]]; then - openssl dhparam -out /etc/dovecot/dh.pem 4096 -fi - -chown mail:dovecot /etc/dovecot/dh.pem - # Enable SSL, specify the location of the SSL certificate and private key files. # Use Mozilla's "Intermediate" recommendations at https://ssl-config.mozilla.org/#server=dovecot&server-version=2.2.33&config=intermediate&openssl-version=1.1.1, -# except that the current version of Dovecot does not have a TLSv1.3 setting, so we only use TLSv1.2. +# specify a minimum of TLSv1.2. tools/editconf.py /etc/dovecot/conf.d/10-ssl.conf \ ssl=required \ "ssl_cert=<$STORAGE_ROOT/ssl/ssl_certificate.pem" \ "ssl_key=<$STORAGE_ROOT/ssl/ssl_private_key.pem" \ "ssl_min_protocol=TLSv1.2" \ "ssl_prefer_server_ciphers=yes" \ - "ssl_dh= Date: Sun, 1 Aug 2021 22:49:25 +0200 Subject: [PATCH 008/129] remove old ciphers from postfix --- setup/mail-postfix.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/mail-postfix.sh b/setup/mail-postfix.sh index 618fce19..1c12f47c 100755 --- a/setup/mail-postfix.sh +++ b/setup/mail-postfix.sh @@ -131,7 +131,7 @@ tools/editconf.py /etc/postfix/main.cf \ smtpd_tls_dh1024_param_file=$STORAGE_ROOT/ssl/dh4096.pem \ smtpd_tls_protocols="!SSLv2,!SSLv3,!TLSv1,!TLSv1.1" \ smtpd_tls_ciphers=medium \ - tls_medium_cipherlist=ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA \ + tls_medium_cipherlist=ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256 \ smtpd_tls_exclude_ciphers="MD5, DES, ADH, RC4, PSD, SRP, 3DES, eNULL, aNULL" \ tls_preempt_cipherlist=yes \ smtpd_tls_received_header=yes From 1f3515821151d271d4af56efac33995a81c80991 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sun, 1 Aug 2021 23:09:59 +0200 Subject: [PATCH 009/129] use predefined DHE field groups --- conf/dh4096.pem | 13 +++++++++++++ setup/ssl.sh | 10 ++++------ 2 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 conf/dh4096.pem diff --git a/conf/dh4096.pem b/conf/dh4096.pem new file mode 100644 index 00000000..3cf0fcbc --- /dev/null +++ b/conf/dh4096.pem @@ -0,0 +1,13 @@ +-----BEGIN DH PARAMETERS----- +MIICCAKCAgEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz ++8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a +87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7 +YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi +7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD +ssbzSibBsu/6iGtCOGEfz9zeNVs7ZRkDW7w09N75nAI4YbRvydbmyQd62R0mkff3 +7lmMsPrBhtkcrv4TCYUTknC0EwyTvEN5RPT9RFLi103TZPLiHnH1S/9croKrnJ32 +nuhtK8UiNjoNq8Uhl5sN6todv5pC1cRITgq80Gv6U93vPBsg7j/VnXwl5B0rZp4e +8W5vUsMWTfT7eTDp5OWIV7asfV9C1p9tGHdjzx1VA0AEh/VbpX4xzHpxNciG77Qx +iu1qHgEtnmgyqQdgCpGBMMRtx3j5ca0AOAkpmaMzy4t6Gh25PXFAADwqTs6p+Y0K +zAqCkc3OyX3Pjsm1Wn+IpGtNtahR9EGC4caKAH5eZV9q//////////8CAQI= +-----END DH PARAMETERS----- diff --git a/setup/ssl.sh b/setup/ssl.sh index 5c36d789..d4ec6d7f 100755 --- a/setup/ssl.sh +++ b/setup/ssl.sh @@ -90,9 +90,7 @@ if [ ! -f $STORAGE_ROOT/ssl/ssl_certificate.pem ]; then ln -s $CERT $STORAGE_ROOT/ssl/ssl_certificate.pem fi -# Generate some Diffie-Hellman cipher bits. -# openssl's default bit length for this is 1024 bits, but we'll create -# 4096 bits of bits per the latest recommendations. -if [ ! -f $STORAGE_ROOT/ssl/dh4096.pem ]; then - openssl dhparam -out $STORAGE_ROOT/ssl/dh4096.pem 4096 -fi +# We no longer generate Diffie-Hellman cipher bits. Following rfc7919 we use +# a predefined finite field group, in this case ffdhe4096 from +# https://raw.githubusercontent.com/internetstandards/dhe_groups/master/ffdhe4096.pem +cp -f conf/dh4096.pem $STORAGE_ROOT/ssl/ From cf6eac0d0c33ba6933d48189de4ac1a78ac3bf01 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Mon, 2 Aug 2021 00:05:12 +0200 Subject: [PATCH 010/129] add nginx security headers --- conf/nginx/security.conf | 5 +++++ setup/web.sh | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 conf/nginx/security.conf diff --git a/conf/nginx/security.conf b/conf/nginx/security.conf new file mode 100644 index 00000000..fb871805 --- /dev/null +++ b/conf/nginx/security.conf @@ -0,0 +1,5 @@ +add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload'; +add_header X-Frame-Options "SAMEORIGIN"; +add_header X-Content-Type-Options nosniff; +add_header Content-Security-Policy-Report-Only "default-src 'self'; font-src *;img-src * data:; script-src *; style-src *;frame-ancestors 'self'"; +add_header Referrer-Policy "strict-origin"; diff --git a/setup/web.sh b/setup/web.sh index 3a8fc81b..12133fe2 100755 --- a/setup/web.sh +++ b/setup/web.sh @@ -156,6 +156,8 @@ if [ ! -f /etc/nginx/conf.d/10-geoblock.conf ]; then cp -f conf/nginx/conf.d/10-geoblock.conf /etc/nginx/conf.d/ fi +cp -f conf/nginx/security.conf /etc/nginx/sites-enabled/ + # touch logfiles that might not exist touch /var/log/nginx/geoipblock.log chown www-data /var/log/nginx/geoipblock.log From bd9952704aa5658a53220bff2bfc23e607e8b67c Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Mon, 2 Aug 2021 00:27:45 +0200 Subject: [PATCH 011/129] mute re indexing, could be lots of noise on existing installs --- setup/dovecot-fts-xapian.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/dovecot-fts-xapian.sh b/setup/dovecot-fts-xapian.sh index e985bf8d..47afda8d 100755 --- a/setup/dovecot-fts-xapian.sh +++ b/setup/dovecot-fts-xapian.sh @@ -76,7 +76,7 @@ restart_service dovecot # and compare those to what actually exist in mailboxes. # This removes mails from the index that have already been expunged and makes # sure that the next doveadm index will index all the missing mails (if any). -doveadm fts rescan -A +hide_output doveadm fts rescan -A # Adds unindexed files to the fts database # * `-q`: Queues the indexing to be run by indexer process. (will background the indexing) From a3b7878ef4013247373ad118cdc827e23bd1f1e4 Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Mon, 2 Aug 2021 00:44:47 +0200 Subject: [PATCH 012/129] add contextmenu plugin --- setup/webmail.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/setup/webmail.sh b/setup/webmail.sh index 5d645789..f103e2a9 100755 --- a/setup/webmail.sh +++ b/setup/webmail.sh @@ -33,11 +33,12 @@ VERSION=1.4.11 HASH=3877f0e70f29e7d0612155632e48c3db1e626be3 PERSISTENT_LOGIN_VERSION=6b3fc450cae23ccb2f393d0ef67aa319e877e435 # version 5.2.0 HTML5_NOTIFIER_VERSION=68d9ca194212e15b3c7225eb6085dbcf02fd13d7 # version 0.6.4+ +CONTEXT_MENU_VERSION=602a3812922fb8f71814eb3b8d91e9b7859aab7e # version 3.2.1 CARDDAV_VERSION=4.1.1 CARDDAV_HASH=87b73661b7799b2079c28324311eddb4241242bb -UPDATE_KEY=$VERSION:$PERSISTENT_LOGIN_VERSION:$HTML5_NOTIFIER_VERSION:$CARDDAV_VERSION +UPDATE_KEY=$VERSION:$PERSISTENT_LOGIN_VERSION:$HTML5_NOTIFIER_VERSION:$CARDDAV_VERSION:$CONTEXT_MENU_VERSION # paths that are often reused. RCM_DIR=/usr/local/lib/roundcubemail @@ -86,6 +87,9 @@ if [ $needs_update == 1 ]; then tar -C ${RCM_PLUGIN_DIR} --no-same-owner -zxf /tmp/carddav.tar.gz rm -f /tmp/carddav.tar.gz + # install context menu plugin + git_clone https://github.com/johndoh/roundcube-contextmenu.git $CONTEXT_MENU_VERSION '' ${RCM_PLUGIN_DIR}/contextmenu + # record the version we've installed echo $UPDATE_KEY > ${RCM_DIR}/version fi @@ -130,7 +134,7 @@ cat > $RCM_CONFIG < ~256 bits for AES-256, see above -\$config['plugins'] = array('html5_notifier', 'archive', 'zipdownload', 'password', 'managesieve', 'jqueryui', 'persistent_login', 'carddav'); +\$config['plugins'] = array('html5_notifier', 'archive', 'zipdownload', 'password', 'managesieve', 'jqueryui', 'persistent_login', 'carddav', 'contextmenu'); \$config['skin'] = 'elastic'; \$config['login_autocomplete'] = 2; \$config['password_charset'] = 'UTF-8'; From 75f14a07358fe7a60a64120410c56056370201b8 Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Mon, 2 Aug 2021 22:09:04 +0200 Subject: [PATCH 013/129] make plugin installation of carddav like other git based installs --- setup/webmail.sh | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/setup/webmail.sh b/setup/webmail.sh index f103e2a9..b2651312 100755 --- a/setup/webmail.sh +++ b/setup/webmail.sh @@ -34,9 +34,7 @@ HASH=3877f0e70f29e7d0612155632e48c3db1e626be3 PERSISTENT_LOGIN_VERSION=6b3fc450cae23ccb2f393d0ef67aa319e877e435 # version 5.2.0 HTML5_NOTIFIER_VERSION=68d9ca194212e15b3c7225eb6085dbcf02fd13d7 # version 0.6.4+ CONTEXT_MENU_VERSION=602a3812922fb8f71814eb3b8d91e9b7859aab7e # version 3.2.1 - -CARDDAV_VERSION=4.1.1 -CARDDAV_HASH=87b73661b7799b2079c28324311eddb4241242bb +CARDDAV_VERSION=b6d1eb31c559ea3ddd8986d287eb82d657477c6e # version 4.1.2 UPDATE_KEY=$VERSION:$PERSISTENT_LOGIN_VERSION:$HTML5_NOTIFIER_VERSION:$CARDDAV_VERSION:$CONTEXT_MENU_VERSION @@ -77,17 +75,10 @@ if [ $needs_update == 1 ]; then # install roundcube html5_notifier plugin git_clone https://github.com/kitist/html5_notifier.git $HTML5_NOTIFIER_VERSION '' ${RCM_PLUGIN_DIR}/html5_notifier - # download and verify the full release of the carddav plugin - wget_verify \ - https://github.com/mstilkerich/rcmcarddav/releases/download/v${CARDDAV_VERSION}/carddav-v${CARDDAV_VERSION}.tar.gz \ - $CARDDAV_HASH \ - /tmp/carddav.tar.gz - - # unzip and cleanup - tar -C ${RCM_PLUGIN_DIR} --no-same-owner -zxf /tmp/carddav.tar.gz - rm -f /tmp/carddav.tar.gz - - # install context menu plugin + # install roundcube carddav plugin + git_clone https://github.com/mstilkerich/rcmcarddav.git $CARDDAV_VERSION '' ${RCM_PLUGIN_DIR}/carddav + + # install roundcube context menu plugin git_clone https://github.com/johndoh/roundcube-contextmenu.git $CONTEXT_MENU_VERSION '' ${RCM_PLUGIN_DIR}/contextmenu # record the version we've installed From 4b260354c25b29487f5d78d0bab6b760418dc5a9 Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Mon, 2 Aug 2021 22:47:42 +0200 Subject: [PATCH 014/129] revert carddav plugin install --- setup/webmail.sh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/setup/webmail.sh b/setup/webmail.sh index b2651312..38b800dd 100755 --- a/setup/webmail.sh +++ b/setup/webmail.sh @@ -34,7 +34,9 @@ HASH=3877f0e70f29e7d0612155632e48c3db1e626be3 PERSISTENT_LOGIN_VERSION=6b3fc450cae23ccb2f393d0ef67aa319e877e435 # version 5.2.0 HTML5_NOTIFIER_VERSION=68d9ca194212e15b3c7225eb6085dbcf02fd13d7 # version 0.6.4+ CONTEXT_MENU_VERSION=602a3812922fb8f71814eb3b8d91e9b7859aab7e # version 3.2.1 -CARDDAV_VERSION=b6d1eb31c559ea3ddd8986d287eb82d657477c6e # version 4.1.2 + +CARDDAV_VERSION=4.1.1 +CARDDAV_HASH=87b73661b7799b2079c28324311eddb4241242bb UPDATE_KEY=$VERSION:$PERSISTENT_LOGIN_VERSION:$HTML5_NOTIFIER_VERSION:$CARDDAV_VERSION:$CONTEXT_MENU_VERSION @@ -75,9 +77,16 @@ if [ $needs_update == 1 ]; then # install roundcube html5_notifier plugin git_clone https://github.com/kitist/html5_notifier.git $HTML5_NOTIFIER_VERSION '' ${RCM_PLUGIN_DIR}/html5_notifier - # install roundcube carddav plugin - git_clone https://github.com/mstilkerich/rcmcarddav.git $CARDDAV_VERSION '' ${RCM_PLUGIN_DIR}/carddav - + # download and verify the full release of the carddav plugin. Can't use github because does not include all dependencies + wget_verify \ + https://github.com/mstilkerich/rcmcarddav/releases/download/v${CARDDAV_VERSION}/carddav-v${CARDDAV_VERSION}.tar.gz \ + $CARDDAV_HASH \ + /tmp/carddav.tar.gz + + # unzip and cleanup + tar -C ${RCM_PLUGIN_DIR} --no-same-owner -zxf /tmp/carddav.tar.gz + rm -f /tmp/carddav.tar.gz + # install roundcube context menu plugin git_clone https://github.com/johndoh/roundcube-contextmenu.git $CONTEXT_MENU_VERSION '' ${RCM_PLUGIN_DIR}/contextmenu From 19799fd5e652d4ffc22408e15185e3db480b1750 Mon Sep 17 00:00:00 2001 From: kiekerjan Date: Mon, 16 Aug 2021 11:53:31 +0200 Subject: [PATCH 015/129] Create codeql-analysis.yml --- .github/workflows/codeql-analysis.yml | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 00000000..f05421ad --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,71 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '43 20 * * 0' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'python' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 From b036b09eb1a6ffe1795014c0e7c80d9fa823adf2 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Mon, 16 Aug 2021 12:12:20 +0200 Subject: [PATCH 016/129] update readme --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f3df0629..6c5b5199 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Functionality changes and additions This applies geoip filtering on acces to the admin panel of the box. Order of filtering: block continents that are not allowed, block countries that are not allowed, allow countries that are allowed (overriding continent filtering). Edit /etc/nginx/conf.d/10-geoblock.conf to configure. * Add geoipblocking for ssh access This applies geoip filtering for access to the ssh server. Edit /etc/geoiplookup.conf. All countries defined in this file are allowed. Works for alternate ssh ports. -* Make fail2ban a more strict +* Make fail2ban more strict enable postfix filters, lengthen bantime and findtime * Add fail2ban jails for both above mentioned geoipblocking filters * Add fail2ban filters for web scanners and badbots @@ -25,6 +25,8 @@ Functionality changes and additions To be used before for example when changing IP addresses. Shortening TTL values will propagate changes faster. For reference, default TTL is 1 day, short TTL is 5 minutes. To use, edit file /etc/forceshortdnsttl and add a line for each domain for which shorter TTLs should be used. To use short TTLs for all known domains, add "forceshortdnsttl" * Use the box as a Hidden Master in the DNS system Thus only the secondary DNS servers are used as public DNS servers. When using a hidden master, no glue records are necessary at your domain hoster. To use, first setup secondary DNS servers via the Custom DNS administration page. At least two secondary servers should be set. When that functions, edit file /etc/usehiddenmasterdns and add a line for each domain for which Hidden Master should be used. To use Hidden Master for all known domains, add "usehiddenmasterdns". +* Daily ip blacklist check + Using check-dnsbl.py from https://github.com/gsauthof/utilit Bug fixes * Munin routes are ignored for Multi Factor Authentication [see github issue](https://github.com/mail-in-a-box/mailinabox/issues/1865) @@ -40,8 +42,10 @@ Maintenance (personal) * Remove nextcloud skeleton to save disk space Fun -* Add option to define ADMIN_IP_ADDRESS (currently only used to ignore fail2ban jails) -* Add dynamic dns tools in the tools directory. +* Add option to define ADMIN_IP_ADDRESS + Currently only used to ignore fail2ban jails +* Add dynamic dns tools in the tools directory + Can be used to control DNS entries on the mail-in-a-box to point to a machine with a non-fixed (e.g. residential) ip address Original mailinabox content starts here: From f20a992bcb71c1ab740090847d5a9b660d2410f4 Mon Sep 17 00:00:00 2001 From: kiekerjan Date: Mon, 16 Aug 2021 12:22:39 +0200 Subject: [PATCH 017/129] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6c5b5199..3861c199 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,15 @@ Functionality changes and additions * Add some munin plugins * Update nextcloud to 20.0.8 * Update roundcube carddav plugin to 4.1.1 +* Add roundcube context menu plugin * Use shorter TTL values in the DNS server. To be used before for example when changing IP addresses. Shortening TTL values will propagate changes faster. For reference, default TTL is 1 day, short TTL is 5 minutes. To use, edit file /etc/forceshortdnsttl and add a line for each domain for which shorter TTLs should be used. To use short TTLs for all known domains, add "forceshortdnsttl" * Use the box as a Hidden Master in the DNS system Thus only the secondary DNS servers are used as public DNS servers. When using a hidden master, no glue records are necessary at your domain hoster. To use, first setup secondary DNS servers via the Custom DNS administration page. At least two secondary servers should be set. When that functions, edit file /etc/usehiddenmasterdns and add a line for each domain for which Hidden Master should be used. To use Hidden Master for all known domains, add "usehiddenmasterdns". * Daily ip blacklist check Using check-dnsbl.py from https://github.com/gsauthof/utilit +* Updated ssl security for web and email + Removed older cryptos following internet.nl recommendations Bug fixes * Munin routes are ignored for Multi Factor Authentication [see github issue](https://github.com/mail-in-a-box/mailinabox/issues/1865) From 60adba79a1287a779187f8ba21abbb0b77bae82a Mon Sep 17 00:00:00 2001 From: kiekerjan Date: Mon, 16 Aug 2021 13:15:33 +0200 Subject: [PATCH 018/129] Update README.md --- README.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3861c199..cb00a812 100644 --- a/README.md +++ b/README.md @@ -6,29 +6,30 @@ I made a number of modifications to the original Mail-in-a-Box, some to fix bugs Functionality changes and additions * Change installation target to Ubuntu 20.04. Thanks to [Power Mail-in-a-Box](https://github.com/ddavness/power-mailinabox) -* Add geoipblocking on the admin web console +* Add geoipblocking on the admin web console This applies geoip filtering on acces to the admin panel of the box. Order of filtering: block continents that are not allowed, block countries that are not allowed, allow countries that are allowed (overriding continent filtering). Edit /etc/nginx/conf.d/10-geoblock.conf to configure. -* Add geoipblocking for ssh access +* Add geoipblocking for ssh access This applies geoip filtering for access to the ssh server. Edit /etc/geoiplookup.conf. All countries defined in this file are allowed. Works for alternate ssh ports. -* Make fail2ban more strict +* Make fail2ban more strict enable postfix filters, lengthen bantime and findtime * Add fail2ban jails for both above mentioned geoipblocking filters * Add fail2ban filters for web scanners and badbots * Add xapian full text searching to dovecot (from https://github.com/grosjo/fts-xapian) * Add rkhunter -* Configure domain names for which only www will be hosted. +* Configure domain names for which only www will be hosted Edit /etc/miabwwwdomains.conf to configure. The box will handle incoming traffic asking for these domain names. The DNS entries are entered in an external DNS provider! If you want this box to handle the DNS entries, simply add a mail alias. (existing functionality of the vanilla Mail-in-a-Box) * Add some munin plugins * Update nextcloud to 20.0.8 +* Add nextcloud notes app * Update roundcube carddav plugin to 4.1.1 * Add roundcube context menu plugin -* Use shorter TTL values in the DNS server. +* Use shorter TTL values in the DNS server To be used before for example when changing IP addresses. Shortening TTL values will propagate changes faster. For reference, default TTL is 1 day, short TTL is 5 minutes. To use, edit file /etc/forceshortdnsttl and add a line for each domain for which shorter TTLs should be used. To use short TTLs for all known domains, add "forceshortdnsttl" -* Use the box as a Hidden Master in the DNS system +* Use the box as a Hidden Master in the DNS system Thus only the secondary DNS servers are used as public DNS servers. When using a hidden master, no glue records are necessary at your domain hoster. To use, first setup secondary DNS servers via the Custom DNS administration page. At least two secondary servers should be set. When that functions, edit file /etc/usehiddenmasterdns and add a line for each domain for which Hidden Master should be used. To use Hidden Master for all known domains, add "usehiddenmasterdns". -* Daily ip blacklist check - Using check-dnsbl.py from https://github.com/gsauthof/utilit -* Updated ssl security for web and email +* Daily ip blacklist check + Using check-dnsbl.py from https://github.com/gsauthof/utility +* Updated ssl security for web and email Removed older cryptos following internet.nl recommendations Bug fixes @@ -45,9 +46,9 @@ Maintenance (personal) * Remove nextcloud skeleton to save disk space Fun -* Add option to define ADMIN_IP_ADDRESS +* Add option to define ADMIN_IP_ADDRESS Currently only used to ignore fail2ban jails -* Add dynamic dns tools in the tools directory +* Add dynamic dns tools in the tools directory Can be used to control DNS entries on the mail-in-a-box to point to a machine with a non-fixed (e.g. residential) ip address Original mailinabox content starts here: From daad122236f7eca841e9a46fa19dd57f44bcb5ac Mon Sep 17 00:00:00 2001 From: lamkin <88649589+lamkin@users.noreply.github.com> Date: Mon, 16 Aug 2021 16:46:32 +0100 Subject: [PATCH 019/129] Ignore bad encoding in email addresses when parsing maillog files (#2017) local/domain parts of email address should be standard ASCII or UTF-8. Some email addresses contain extended ASCII, leading to decode failure by the UTF-8 codec (and thus failure of the Usage-Report script) This change allows maillog parsing to continue over lines containing such addresses --- management/mail_log.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/mail_log.py b/management/mail_log.py index 1626f820..59c32c6e 100755 --- a/management/mail_log.py +++ b/management/mail_log.py @@ -586,7 +586,7 @@ def scan_postfix_submission_line(date, log, collector): def readline(filename): """ A generator that returns the lines of a file """ - with open(filename) as file: + with open(filename, errors='replace') as file: while True: line = file.readline() if not line: From 0ba841c7b66979e00e0d790b53555f05f72e9063 Mon Sep 17 00:00:00 2001 From: NewbieOrange Date: Mon, 23 Aug 2021 02:13:58 +0800 Subject: [PATCH 020/129] fail2ban now supports ipv6 (#2015) Since fail2ban 0.10.0, ipv6 support has been added. The current Ubuntu 18.04 repository has fail2ban 0.10.2, which does have ipv6 protection. --- security.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/security.md b/security.md index 8c39437e..5de8c612 100644 --- a/security.md +++ b/security.md @@ -69,8 +69,6 @@ The following services are protected: SSH, IMAP (dovecot), SMTP submission (post Some other services running on the box may be missing fail2ban filters. -`fail2ban` only blocks IPv4 addresses, however. If the box has a public IPv6 address, it is not protected from these attacks. - Outbound Mail ------------- From 20ccda8710411552d69978aeff41a9281fbb2012 Mon Sep 17 00:00:00 2001 From: myfirstnameispaul Date: Mon, 28 Jun 2021 05:51:05 -0700 Subject: [PATCH 021/129] Re-order DS record algorithms by digest type and revise warning message. Note that 7, 4 is printed last in the status checks page but does not appear in the file, and I couldn't figure out why. --- management/status_checks.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/management/status_checks.py b/management/status_checks.py index 7e766d0f..e509ff41 100755 --- a/management/status_checks.py +++ b/management/status_checks.py @@ -619,7 +619,9 @@ def check_dnssec(domain, env, output, dns_zonefiles, is_checking_primary=False): output.print_ok("DNSSEC 'DS' record is set correctly at registrar. (Records using algorithm other than ECDSAP256SHA256 should be removed.)") return else: # no record uses alg 13 - output.print_warning("DNSSEC 'DS' record set at registrar is valid but should be updated to ECDSAP256SHA256 (see below).") + output.print_warning("""DNSSEC 'DS' record set at registrar is valid but should be updated to ECDSAP256SHA256 (see below). + IMPORTANT: Do not delete existing DNSSEC 'DS' records for this domain until confirmation that the new DNSSEC 'DS' record + for this domain is valid.""") else: if is_checking_primary: output.print_error("""The DNSSEC 'DS' record for %s is incorrect. See further details below.""" % domain) @@ -630,7 +632,8 @@ def check_dnssec(domain, env, output, dns_zonefiles, is_checking_primary=False): output.print_line("""Follow the instructions provided by your domain name registrar to set a DS record. Registrars support different sorts of DS records. Use the first option that works:""") - preferred_ds_order = [(7, 1), (7, 2), (8, 4), (13, 4), (8, 1), (8, 2), (13, 1), (13, 2)] # low to high + preferred_ds_order = [(7, 1), (8, 1), (13, 1), (7, 2), (8, 4), (13, 4), (8, 2), (13, 2)] # low to high + def preferred_ds_order_func(ds_suggestion): k = (int(ds_suggestion['alg']), int(ds_suggestion['digalg'])) if k in preferred_ds_order: From 67b5711c683df8f05acd48ce13c4afcb2c5d3008 Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sun, 22 Aug 2021 14:43:33 -0400 Subject: [PATCH 022/129] Recommend that DS records be updated to not use SHA1 and exclude MUST NOT methods (SHA1) and the unlikely option RSASHA1-NSEC3-SHA1 (7) + SHA-384 (4) from the DS record suggestions --- management/status_checks.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/management/status_checks.py b/management/status_checks.py index e509ff41..e2d4b1a7 100755 --- a/management/status_checks.py +++ b/management/status_checks.py @@ -612,14 +612,14 @@ def check_dnssec(domain, env, output, dns_zonefiles, is_checking_primary=False): # # But it may not be preferred. Only algorithm 13 is preferred. Warn if any of the # matched zones uses a different algorithm. - if set(r[1] for r in matched_ds) == { '13' }: # all are alg 13 + if set(r[1] for r in matched_ds) == { '13' } and set(r[2] for r in matched_ds) <= { '2', '4' }: # all are alg 13 and digest type 2 or 4 output.print_ok("DNSSEC 'DS' record is set correctly at registrar.") return - elif '13' in set(r[1] for r in matched_ds): # some but not all are alg 13 - output.print_ok("DNSSEC 'DS' record is set correctly at registrar. (Records using algorithm other than ECDSAP256SHA256 should be removed.)") + elif len([r for r in matched_ds if r[1] == '13' and r[2] in ( '2', '4' )]) > 0: # some but not all are alg 13 + output.print_ok("DNSSEC 'DS' record is set correctly at registrar. (Records using algorithm other than ECDSAP256SHA256 and digest types other than SHA-256/384 should be removed.)") return else: # no record uses alg 13 - output.print_warning("""DNSSEC 'DS' record set at registrar is valid but should be updated to ECDSAP256SHA256 (see below). + output.print_warning("""DNSSEC 'DS' record set at registrar is valid but should be updated to ECDSAP256SHA256 and SHA-256 (see below). IMPORTANT: Do not delete existing DNSSEC 'DS' records for this domain until confirmation that the new DNSSEC 'DS' record for this domain is valid.""") else: @@ -632,7 +632,7 @@ def check_dnssec(domain, env, output, dns_zonefiles, is_checking_primary=False): output.print_line("""Follow the instructions provided by your domain name registrar to set a DS record. Registrars support different sorts of DS records. Use the first option that works:""") - preferred_ds_order = [(7, 1), (8, 1), (13, 1), (7, 2), (8, 4), (13, 4), (8, 2), (13, 2)] # low to high + preferred_ds_order = [(7, 2), (8, 4), (13, 4), (8, 2), (13, 2)] # low to high, see https://github.com/mail-in-a-box/mailinabox/issues/1998 def preferred_ds_order_func(ds_suggestion): k = (int(ds_suggestion['alg']), int(ds_suggestion['digalg'])) @@ -641,6 +641,7 @@ def check_dnssec(domain, env, output, dns_zonefiles, is_checking_primary=False): return -1 # index before first item output.print_line("") for i, ds_suggestion in enumerate(sorted(expected_ds_records.values(), key=preferred_ds_order_func, reverse=True)): + if preferred_ds_order_func(ds_suggestion) == -1: continue # don't offer record types that the RFC says we must not offer output.print_line("") output.print_line("Option " + str(i+1) + ":") output.print_line("----------") From ba80d9e72dd984af0ee733d55291a00e7f8685e6 Mon Sep 17 00:00:00 2001 From: David Duque Date: Mon, 23 Aug 2021 11:25:41 +0100 Subject: [PATCH 023/129] Show backup retention period form when configuring B2 backups (#2024) --- management/templates/system-backup.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/templates/system-backup.html b/management/templates/system-backup.html index a63b38e6..6cbcc4fa 100644 --- a/management/templates/system-backup.html +++ b/management/templates/system-backup.html @@ -138,7 +138,7 @@ -
+
From 63255d321a562dea326e0218426501d3d649ac9b Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sat, 28 Aug 2021 13:34:37 +0200 Subject: [PATCH 024/129] tuning fail2ban --- .../filter.d/miab-postfix-rdnsfail.conf | 12 +++++++ .../filter.d/miab-postfix-scanner.conf | 7 +++++ conf/fail2ban/filter.d/nginx-badrequests.conf | 8 +++++ .../filter.d/nginx-missingresource.conf | 6 ++++ conf/fail2ban/filter.d/webexploits.conf | 5 +-- conf/fail2ban/jail.d/badrequests.conf | 14 +++++++++ conf/fail2ban/jail.d/postfix-extra.conf | 31 +++++++++++++++++++ conf/fail2ban/jail.d/webexploits.conf | 8 +++-- conf/fail2ban/jails.conf | 4 +-- 9 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 conf/fail2ban/filter.d/miab-postfix-rdnsfail.conf create mode 100644 conf/fail2ban/filter.d/miab-postfix-scanner.conf create mode 100644 conf/fail2ban/filter.d/nginx-badrequests.conf create mode 100644 conf/fail2ban/filter.d/nginx-missingresource.conf create mode 100644 conf/fail2ban/jail.d/badrequests.conf create mode 100644 conf/fail2ban/jail.d/postfix-extra.conf diff --git a/conf/fail2ban/filter.d/miab-postfix-rdnsfail.conf b/conf/fail2ban/filter.d/miab-postfix-rdnsfail.conf new file mode 100644 index 00000000..c2eb3634 --- /dev/null +++ b/conf/fail2ban/filter.d/miab-postfix-rdnsfail.conf @@ -0,0 +1,12 @@ +[INCLUDES] + +before = common.conf + +[Definition] +miab-errors=postfix/(submission/)?smtpd.*warning: hostname .* does not resolve to address :.+ +miab-normal=postfix/(submission/)?smtpd.*warning: hostname .* does not resolve to address $ +ignoreregex = + +failregex = > + +mode = normal \ No newline at end of file diff --git a/conf/fail2ban/filter.d/miab-postfix-scanner.conf b/conf/fail2ban/filter.d/miab-postfix-scanner.conf new file mode 100644 index 00000000..191dacd6 --- /dev/null +++ b/conf/fail2ban/filter.d/miab-postfix-scanner.conf @@ -0,0 +1,7 @@ +[INCLUDES] + +before = common.conf + +[Definition] +failregex=postfix/submission/smtpd.*warning: non-SMTP command from.*\[\].*HTTP.*$ +ignoreregex = diff --git a/conf/fail2ban/filter.d/nginx-badrequests.conf b/conf/fail2ban/filter.d/nginx-badrequests.conf new file mode 100644 index 00000000..5ba83405 --- /dev/null +++ b/conf/fail2ban/filter.d/nginx-badrequests.conf @@ -0,0 +1,8 @@ +# Ban requests for non-existing or not-allowed resources +# Ban requests for 40x results + +[Definition] +# first regex for error.log, second for access.log +failregex = ^.* \[error\] .*2: No such file or directory.*client: .*$ + ^ - .*"(GET|POST|HEAD).*HTTP.*" (400|401|403|404) .*$ +ignoreregex = ^.*(robots.txt|favicon.ico).*$ \ No newline at end of file diff --git a/conf/fail2ban/filter.d/nginx-missingresource.conf b/conf/fail2ban/filter.d/nginx-missingresource.conf new file mode 100644 index 00000000..22a26e7b --- /dev/null +++ b/conf/fail2ban/filter.d/nginx-missingresource.conf @@ -0,0 +1,6 @@ +# Ban requests for non-existing or not-allowed resources + +[Definition] +failregex = ^.* \[error\] .*2: No such file or directory.*client: .*$ +ignoreregex = ^.*(robots.txt|favicon.ico).*$ + diff --git a/conf/fail2ban/filter.d/webexploits.conf b/conf/fail2ban/filter.d/webexploits.conf index ff5a101a..dbb297ae 100644 --- a/conf/fail2ban/filter.d/webexploits.conf +++ b/conf/fail2ban/filter.d/webexploits.conf @@ -97,7 +97,8 @@ failregex = ^ -.*(GET|POST|HEAD).*(/\.git/config) ^ -.*(GET|POST|HEAD).*(/examples/file-manager\.html) ^ -.*(GET|POST|HEAD).*(/getcfg\.php) ^ -.*(GET|POST|HEAD).*(/get_password\.php) - ^ -.*(GET|POST|HEAD).*(/\.git/info/) + ^ -.*(GET|POST|HEAD).*(/\.git/info) + ^ -.*(GET|POST|HEAD).*(/\.git/HEAD) ^ -.*(GET|POST|HEAD).*(/Hello\.World) ^ -.*(GET|POST|HEAD).*(/hndUnblock\.cgi) ^ -.*(GET|POST|HEAD).*(/images/login9/login_33\.jpg) @@ -231,7 +232,7 @@ failregex = ^ -.*(GET|POST|HEAD).*(/\.git/config) ^ -.*(GET|POST|HEAD).*(\x22sanitize) ^ -.*(GET|POST|HEAD).*(\x22SimplePie) ^ -.*(GET|POST|HEAD).*(\x5C0disconnectHandlers) - ^ -.*(GET).*(\.\./wp-config.php) + ^ -.*(GET|POST|HEAD).*(\.\./wp-config.php) ignoreregex = diff --git a/conf/fail2ban/jail.d/badrequests.conf b/conf/fail2ban/jail.d/badrequests.conf new file mode 100644 index 00000000..d80e527c --- /dev/null +++ b/conf/fail2ban/jail.d/badrequests.conf @@ -0,0 +1,14 @@ +# Block clients that generate too many non existing resources +# Do not deploy of you host many websites on your box +# any bad html link will trigger a false positive. +# This jail is meant to catch scanners that try many +# sites. +[badrequests] +enabled = true +port = http,https +filter = nginx-badrequests +logpath = /var/log/nginx/error.log + /var/log/nginx/access.log +maxretry = 8 +findtime = 15m +bantime = 15m diff --git a/conf/fail2ban/jail.d/postfix-extra.conf b/conf/fail2ban/jail.d/postfix-extra.conf new file mode 100644 index 00000000..2e62e70d --- /dev/null +++ b/conf/fail2ban/jail.d/postfix-extra.conf @@ -0,0 +1,31 @@ +# typically non smtp commands. Block fast for access to postfix +[miab-postfix-scanner] +enabled = true +port = smtp,465,587 +filter = miab-postfix-scanner +logpath = /var/log/mail.log +maxretry = 2 +findtime = 1d +bantime = 1h + +# ip lookup of hostname does not match. Go easy on block +[miab-pf-rdnsfail] +enabled = true +port = smtp,465,587 +mode = normal +filter = miab-postfix-rdnsfail +logpath = /var/log/mail.log +maxretry = 8 +findtime = 12h +bantime = 30m + +# ip lookup of hostname does not match with failure. More strict block +[miab-pf-rdnsfail-e] +enabled = true +port = smtp,465,587 +mode = errors +filter = miab-postfix-rdnsfail[mode=errors] +logpath = /var/log/mail.log +maxretry = 4 +findtime = 1d +bantime = 1h diff --git a/conf/fail2ban/jail.d/webexploits.conf b/conf/fail2ban/jail.d/webexploits.conf index 30baaceb..f5edda95 100644 --- a/conf/fail2ban/jail.d/webexploits.conf +++ b/conf/fail2ban/jail.d/webexploits.conf @@ -1,8 +1,12 @@ +# Block clients based on a list of specific requests +# The list contains applications that are not installed +# only scanners and bad parties will try too often +# so blocking can be fast and long [webexploits] enabled = true port = http,https filter = webexploits logpath = /var/log/nginx/access.log maxretry = 2 -findtime = 240m -bantime = 60m +findtime = 4h +bantime = 4h diff --git a/conf/fail2ban/jails.conf b/conf/fail2ban/jails.conf index b360aa77..006a5c7e 100644 --- a/conf/fail2ban/jails.conf +++ b/conf/fail2ban/jails.conf @@ -5,7 +5,7 @@ # Whitelist our own IP addresses. 127.0.0.1/8 is the default. But our status checks # ping services over the public interface so we should whitelist that address of # ours too. The string is substituted during installation. -ignoreip = 127.0.0.1/8 ::1/128 PUBLIC_IP PUBLIC_IPV6 ADMIN_HOME_IP ADMIN_HOME_IPV6 +ignoreip = 127.0.0.1/8 ::1/128 PUBLIC_IP PUBLIC_IPV6/64 ADMIN_HOME_IP ADMIN_HOME_IPV6/64 bantime = 15m findtime = 120m maxretry = 4 @@ -69,7 +69,7 @@ findtime = 15m enabled = true maxretry = 10 bantime = 2w -findtime = 3d +findtime = 6d action = iptables-allports[name=recidive] # In the recidive section of jail.conf the action contains: # From c4fa84b966baa577b3195d43cd886243fa367d04 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sun, 29 Aug 2021 22:47:29 +0200 Subject: [PATCH 025/129] tuning fail2ban --- conf/fail2ban/filter.d/nginx-badrequests.conf | 4 +--- conf/fail2ban/jail.d/badrequests.conf | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/conf/fail2ban/filter.d/nginx-badrequests.conf b/conf/fail2ban/filter.d/nginx-badrequests.conf index 5ba83405..0265699d 100644 --- a/conf/fail2ban/filter.d/nginx-badrequests.conf +++ b/conf/fail2ban/filter.d/nginx-badrequests.conf @@ -1,8 +1,6 @@ # Ban requests for non-existing or not-allowed resources -# Ban requests for 40x results [Definition] -# first regex for error.log, second for access.log +# regex for nginx error.log failregex = ^.* \[error\] .*2: No such file or directory.*client: .*$ - ^ - .*"(GET|POST|HEAD).*HTTP.*" (400|401|403|404) .*$ ignoreregex = ^.*(robots.txt|favicon.ico).*$ \ No newline at end of file diff --git a/conf/fail2ban/jail.d/badrequests.conf b/conf/fail2ban/jail.d/badrequests.conf index d80e527c..7fd87e72 100644 --- a/conf/fail2ban/jail.d/badrequests.conf +++ b/conf/fail2ban/jail.d/badrequests.conf @@ -8,7 +8,6 @@ enabled = true port = http,https filter = nginx-badrequests logpath = /var/log/nginx/error.log - /var/log/nginx/access.log maxretry = 8 findtime = 15m bantime = 15m From 36897b35ab628876086c183cb1f8600897b28476 Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Fri, 3 Sep 2021 20:18:30 +0200 Subject: [PATCH 026/129] Update dns blacklist checker --- tools/check-dnsbl.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/check-dnsbl.py b/tools/check-dnsbl.py index 402c7f86..e0bd9654 100755 --- a/tools/check-dnsbl.py +++ b/tools/check-dnsbl.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 - +# From https://github.com/gsauthof/utility Thanks! # 2016, Georg Sauthoff , GPLv3+ import argparse @@ -189,7 +189,7 @@ def get_addrs(dest, mx=True): domains = [ dest ] if mx: try: - r = dns.resolver.resolve(dest, 'mx') + r = dns.resolver.resolve(dest, 'mx', search=True) domains = [ answer.exchange for answer in r ] log.debug('destinatin {} has MXs: {}' .format(dest, ', '.join([str(d) for d in domains]))) @@ -199,7 +199,7 @@ def get_addrs(dest, mx=True): for domain in domains: for t in ['a', 'aaaa']: try: - r = dns.resolver.resolve(domain, t) + r = dns.resolver.resolve(domain, t, search=True) except dns.resolver.NoAnswer: continue xs = [ ( answer.address, domain ) for answer in r ] @@ -216,12 +216,12 @@ def check_dnsbl(addr, bl): rev = dns.reversename.from_address(addr) domain = str(rev.split(3)[0]) + '.' + bl try: - r = dns.resolver.resolve(domain, 'a') + r = dns.resolver.resolve(domain, 'a', search=True) except (dns.resolver.NXDOMAIN, dns.resolver.NoNameservers, dns.resolver.NoAnswer): return 0 address = list(r)[0].address try: - r = dns.resolver.resolve(domain, 'txt') + r = dns.resolver.resolve(domain, 'txt', search=True) txt = list(r)[0].to_text() except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN): txt = '' @@ -237,7 +237,7 @@ def check_rdns(addrs): log.debug('Check if there is a reverse DNS record that maps address {} to {}' .format(addr, domain)) try: - r = dns.resolver.resolve(dns.reversename.from_address(addr), 'ptr') + r = dns.resolver.resolve(dns.reversename.from_address(addr), 'ptr', search=True) a = list(r)[0] target = str(a.target).lower() source = str(domain).lower() @@ -316,7 +316,7 @@ if __name__ == '__main__': # ## In[ ]: # -#r = dns.resolver.resolve(dns.reversename.from_address('89.238.75.224'), 'ptr') +#r = dns.resolver.resolve(dns.reversename.from_address('89.238.75.224'), 'ptr', search=True) #a = list(r)[0] #a.target.to_text() # @@ -360,7 +360,7 @@ if __name__ == '__main__': ## In[ ]: # ## as of 2016-11, listed -#r = dns.resolver.resolve('39.227.103.116.zen.spamhaus.org', 'txt') +#r = dns.resolver.resolve('39.227.103.116.zen.spamhaus.org', 'txt', search=True) #answer = list(r)[0] #answer.to_text() # @@ -388,7 +388,7 @@ if __name__ == '__main__': # ## In[ ]: # -#a = dns.resolver.resolve('georg.so', 'MX') +#a = dns.resolver.resolve('georg.so', 'MX', search=True) # # ## In[ ]: @@ -404,7 +404,7 @@ if __name__ == '__main__': ## In[ ]: # #[ x.exchange for x in a] -#dns.resolver.resolve(list(a)[0].exchange, 'a') +#dns.resolver.resolve(list(a)[0].exchange, 'a', search=True) # # ## In[ ]: @@ -416,14 +416,14 @@ if __name__ == '__main__': ## In[ ]: # ## should throw NoAnswer -#a = dns.resolver.resolve('escher.lru.li', 'mx') +#a = dns.resolver.resolve('escher.lru.li', 'mx', search=True) ##b = list(a) #a # # ## In[ ]: # -#a = dns.resolver.resolve('georg.so', 'a') +#a = dns.resolver.resolve('georg.so', 'a', search=True) #b = list(a)[0] #b.address #dns.reversename.from_address(b.address) @@ -433,7 +433,7 @@ if __name__ == '__main__': # ## should throw NXDOMAIN #rs = str(r.split(3)[0]) -#dns.resolver.resolve(rs + '.zen.spamhaus.org', 'A' ) +#dns.resolver.resolve(rs + '.zen.spamhaus.org', 'A' , search=True) # # ## In[ ]: From 42e9a5ae69e1bed6e55c29737ab8a6e403ac1aca Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Fri, 3 Sep 2021 20:40:53 +0200 Subject: [PATCH 027/129] update readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cb00a812..deebab71 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ Functionality changes and additions * Add geoipblocking on the admin web console This applies geoip filtering on acces to the admin panel of the box. Order of filtering: block continents that are not allowed, block countries that are not allowed, allow countries that are allowed (overriding continent filtering). Edit /etc/nginx/conf.d/10-geoblock.conf to configure. * Add geoipblocking for ssh access - This applies geoip filtering for access to the ssh server. Edit /etc/geoiplookup.conf. All countries defined in this file are allowed. Works for alternate ssh ports. + This applies geoip filtering for access to the ssh server. Edit /etc/geoiplookup.conf. All countries defined in this file are allowed. Works for alternate ssh ports. + This uses goiplookup from https://github.com/axllent/goiplookup * Make fail2ban more strict enable postfix filters, lengthen bantime and findtime * Add fail2ban jails for both above mentioned geoipblocking filters From 9b39251469042e3e9b0f99a1482307763ddd5ac9 Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Fri, 3 Sep 2021 22:23:00 +0200 Subject: [PATCH 028/129] active roundcube markasjunk plugin --- setup/webmail.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/webmail.sh b/setup/webmail.sh index 38b800dd..88cdd950 100755 --- a/setup/webmail.sh +++ b/setup/webmail.sh @@ -134,7 +134,7 @@ cat > $RCM_CONFIG < ~256 bits for AES-256, see above -\$config['plugins'] = array('html5_notifier', 'archive', 'zipdownload', 'password', 'managesieve', 'jqueryui', 'persistent_login', 'carddav', 'contextmenu'); +\$config['plugins'] = array('html5_notifier', 'archive', 'zipdownload', 'password', 'managesieve', 'jqueryui', 'persistent_login', 'carddav', 'markasjunk', 'contextmenu'); \$config['skin'] = 'elastic'; \$config['login_autocomplete'] = 2; \$config['password_charset'] = 'UTF-8'; From 700188c44392aaa3a1e5cd5feaa59767db38cb53 Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sun, 22 Aug 2021 14:29:33 -0400 Subject: [PATCH 029/129] Roundcube 1.5 RC --- setup/webmail.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/setup/webmail.sh b/setup/webmail.sh index 55fea631..d3652975 100755 --- a/setup/webmail.sh +++ b/setup/webmail.sh @@ -29,8 +29,8 @@ apt_install \ # Combine the Roundcube version number with the commit hash of plugins to track # whether we have the latest version of everything. -VERSION=1.4.11 -HASH=3877f0e70f29e7d0612155632e48c3db1e626be3 +VERSION=1.5-rc +HASH=a7cb2a39702536d769c7ff93f716e27f0b93f9d9 PERSISTENT_LOGIN_VERSION=6b3fc450cae23ccb2f393d0ef67aa319e877e435 # version 5.2.0 HTML5_NOTIFIER_VERSION=68d9ca194212e15b3c7225eb6085dbcf02fd13d7 # version 0.6.4+ CARDDAV_VERSION=3.0.3 @@ -132,6 +132,7 @@ cat > $RCM_CONFIG < From 53ec0f39cb074dc43a2f8b245aa8d4d12c74914e Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sun, 22 Aug 2021 15:02:38 -0400 Subject: [PATCH 030/129] Use 'secrets' to generate the system API key and remove some debugging-related code * Rename the 'master' API key to be called the 'system' API key * Generate the key using the Python secrets module which is meant for this * Remove some debugging helper code which will be obsoleted by the upcoming changes for session keys --- management/auth.py | 29 ++++++++++++----------------- management/daemon.py | 27 +++++---------------------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/management/auth.py b/management/auth.py index fd143c76..de2b61b5 100644 --- a/management/auth.py +++ b/management/auth.py @@ -1,6 +1,5 @@ -import base64, os, os.path, hmac, json +import base64, os, os.path, hmac, json, secrets -from flask import make_response import utils from mailconfig import get_mail_password, get_mail_user_privileges @@ -9,7 +8,7 @@ from mfa import get_hash_mfa_state, validate_auth_mfa DEFAULT_KEY_PATH = '/var/lib/mailinabox/api.key' DEFAULT_AUTH_REALM = 'Mail-in-a-Box Management Server' -class KeyAuthService: +class AuthService: """Generate an API key for authenticating clients Clients must read the key from the key file and send the key with all HTTP @@ -18,16 +17,12 @@ class KeyAuthService: """ def __init__(self): self.auth_realm = DEFAULT_AUTH_REALM - self.key = self._generate_key() self.key_path = DEFAULT_KEY_PATH + self.init_system_api_key() - def write_key(self): - """Write key to file so authorized clients can get the key + def init_system_api_key(self): + """Write an API key to a local file so local processes can use the API""" - The key file is created with mode 0640 so that additional users can be - authorized to access the API by granting group/ACL read permissions on - the key file. - """ def create_file_with_mode(path, mode): # Based on answer by A-B-B: http://stackoverflow.com/a/15015748 old_umask = os.umask(0) @@ -36,6 +31,8 @@ class KeyAuthService: finally: os.umask(old_umask) + self.key = secrets.token_hex(24) + os.makedirs(os.path.dirname(self.key_path), exist_ok=True) with create_file_with_mode(self.key_path, 0o640) as key_file: @@ -72,8 +69,9 @@ class KeyAuthService: if username in (None, ""): raise ValueError("Authorization header invalid.") - elif username == self.key: - # The user passed the master API key which grants administrative privs. + + if username == self.key: + # The user passed the system API key which grants administrative privs. return (None, ["admin"]) else: # The user is trying to log in with a username and either a password @@ -136,8 +134,8 @@ class KeyAuthService: # email address, current hashed password, and current MFA state, so that the # key becomes invalid if any of that information changes. # - # Use an HMAC to generate the API key using our master API key as a key, - # which also means that the API key becomes invalid when our master API key + # Use an HMAC to generate the API key using our system API key as a key, + # which also means that the API key becomes invalid when our system API key # changes --- i.e. when this process is restarted. # # Raises ValueError via get_mail_password if the user doesn't exist. @@ -153,6 +151,3 @@ class KeyAuthService: hash_key = self.key.encode('ascii') return hmac.new(hash_key, msg, digestmod="sha256").hexdigest() - def _generate_key(self): - raw_key = os.urandom(32) - return base64.b64encode(raw_key).decode('ascii') diff --git a/management/daemon.py b/management/daemon.py index 8490ee44..bb723ea6 100755 --- a/management/daemon.py +++ b/management/daemon.py @@ -1,5 +1,8 @@ #!/usr/local/lib/mailinabox/env/bin/python3 # +# The API can be accessed on the command line, e.g. use `curl` like so: +# curl --user $( Date: Sun, 22 Aug 2021 16:07:16 -0400 Subject: [PATCH 031/129] Replace HMAC-based session API keys with tokens stored in memory in the daemon process Since the session cache clears keys after a period of time, this fixes #1821. Based on https://github.com/mail-in-a-box/mailinabox/pull/2012, and so: Co-Authored-By: NewbieOrange Also fixes #2029 by not revealing through the login failure error message whether a user exists or not. --- api/mailinabox.yml | 34 +++++-- management/auth.py | 173 +++++++++++++++++--------------- management/daemon.py | 29 ++++-- management/templates/index.html | 6 ++ management/templates/login.html | 4 +- setup/management.sh | 4 +- tests/fail2ban.py | 2 +- 7 files changed, 149 insertions(+), 103 deletions(-) diff --git a/api/mailinabox.yml b/api/mailinabox.yml index 14cf54de..bd4b203b 100644 --- a/api/mailinabox.yml +++ b/api/mailinabox.yml @@ -54,24 +54,24 @@ tags: System operations, which include system status checks, new version checks and reboot status. paths: - /me: - get: + /login: + post: tags: - User - summary: Get user information + summary: Exchange a username and password for a session API key. description: | - Returns user information. Used for user authentication. + Returns user information and a session API key. Authenticate a user by supplying the auth token as a base64 encoded string in format `email:password` using basic authentication headers. If successful, a long-lived `api_key` is returned which can be used for subsequent - requests to the API. - operationId: getMe + requests to the API in place of the password. + operationId: login x-codeSamples: - lang: curl source: | - curl -X GET "https://{host}/admin/me" \ + curl -X GET "https://{host}/admin/login" \ -u ":" responses: 200: @@ -92,6 +92,24 @@ paths: privileges: - admin status: ok + /logout: + post: + tags: + - User + summary: Invalidates a session API key. + description: | + Invalidates a session API key so that it cannot be used after this API call. + operationId: logout + x-codeSamples: + - lang: curl + source: | + curl -X GET "https://{host}/admin/logout" \ + -u ":" + responses: + 200: + description: Successful operation + content: + application/json: /system/status: post: tags: @@ -1803,7 +1821,7 @@ components: The `access-token` is comprised of the Base64 encoding of `username:password`. The `username` is the mail user's email address, and `password` can either be the mail user's - password, or the `api_key` returned from the `getMe` operation. + password, or the `api_key` returned from the `login` operation. When using `curl`, you can supply user credentials using the `-u` or `--user` parameter. requestBodies: diff --git a/management/auth.py b/management/auth.py index de2b61b5..38c15e91 100644 --- a/management/auth.py +++ b/management/auth.py @@ -1,5 +1,7 @@ import base64, os, os.path, hmac, json, secrets +from datetime import timedelta +from expiringdict import ExpiringDict import utils from mailconfig import get_mail_password, get_mail_user_privileges @@ -9,16 +11,13 @@ DEFAULT_KEY_PATH = '/var/lib/mailinabox/api.key' DEFAULT_AUTH_REALM = 'Mail-in-a-Box Management Server' class AuthService: - """Generate an API key for authenticating clients - - Clients must read the key from the key file and send the key with all HTTP - requests. The key is passed as the username field in the standard HTTP - Basic Auth header. - """ def __init__(self): self.auth_realm = DEFAULT_AUTH_REALM self.key_path = DEFAULT_KEY_PATH + self.max_session_duration = timedelta(days=2) + self.init_system_api_key() + self.sessions = ExpiringDict(max_len=64, max_age_seconds=self.max_session_duration.total_seconds()) def init_system_api_key(self): """Write an API key to a local file so local processes can use the API""" @@ -31,123 +30,133 @@ class AuthService: finally: os.umask(old_umask) - self.key = secrets.token_hex(24) + self.key = secrets.token_hex(32) os.makedirs(os.path.dirname(self.key_path), exist_ok=True) with create_file_with_mode(self.key_path, 0o640) as key_file: key_file.write(self.key + '\n') - def authenticate(self, request, env): - """Test if the client key passed in HTTP Authorization header matches the service key - or if the or username/password passed in the header matches an administrator user. + def authenticate(self, request, env, login_only=False, logout=False): + """Test if the HTTP Authorization header's username matches the system key, a session key, + or if the username/password passed in the header matches a local user. Returns a tuple of the user's email address and list of user privileges (e.g. ('my@email', []) or ('my@email', ['admin']); raises a ValueError on login failure. - If the user used an API key, the user's email is returned as None.""" + If the user used the system API key, the user's email is returned as None since + this key is not associated with a user.""" - def decode(s): - return base64.b64decode(s.encode('ascii')).decode('ascii') - - def parse_basic_auth(header): + def parse_http_authorization_basic(header): + def decode(s): + return base64.b64decode(s.encode('ascii')).decode('ascii') if " " not in header: return None, None scheme, credentials = header.split(maxsplit=1) if scheme != 'Basic': return None, None - credentials = decode(credentials) if ":" not in credentials: return None, None username, password = credentials.split(':', maxsplit=1) return username, password - header = request.headers.get('Authorization') - if not header: - raise ValueError("No authorization header provided.") - - username, password = parse_basic_auth(header) - + username, password = parse_http_authorization_basic(request.headers.get('Authorization', '')) if username in (None, ""): raise ValueError("Authorization header invalid.") - if username == self.key: - # The user passed the system API key which grants administrative privs. + if username.strip() == "" and password.strip() == "": + raise ValueError("No email address, password, session key, or API key provided.") + + # If user passed the system API key, grant administrative privs. This key + # is not associated with a user. + if username == self.key and not login_only: return (None, ["admin"]) + + # If the password corresponds with a session token for the user, grant access for that user. + if password in self.sessions and self.sessions[password]["email"] == username and not login_only: + sessionid = password + session = self.sessions[sessionid] + if session["password_token"] != self.create_user_password_state_token(username, env): + # This session is invalid because the user's password/MFA state changed + # after the session was created. + del self.sessions[sessionid] + raise ValueError("Session expired.") + if logout: + # Clear the session. + del self.sessions[sessionid] + else: + # Re-up the session so that it does not expire. + self.sessions[sessionid] = session + + # If no password was given, but a username was given, we're missing some information. + elif password.strip() == "": + raise ValueError("Enter a password.") + else: - # The user is trying to log in with a username and either a password - # (and possibly a MFA token) or a user-specific API key. - return (username, self.check_user_auth(username, password, request, env)) + # The user is trying to log in with a username and a password + # (and possibly a MFA token). On failure, an exception is raised. + self.check_user_auth(username, password, request, env) + + # Get privileges for authorization. This call should never fail because by this + # point we know the email address is a valid user --- unless the user has been + # deleted after the session was granted. On error the call will return a tuple + # of an error message and an HTTP status code. + privs = get_mail_user_privileges(username, env) + if isinstance(privs, tuple): raise ValueError(privs[0]) + + # Return the authorization information. + return (username, privs) def check_user_auth(self, email, pw, request, env): # Validate a user's login email address and password. If MFA is enabled, # check the MFA token in the X-Auth-Token header. # - # On success returns a list of privileges (e.g. [] or ['admin']). On login - # failure, raises a ValueError with a login error message. + # On login failure, raises a ValueError with a login error message. On + # success, nothing is returned. - # Sanity check. - if email == "" or pw == "": - raise ValueError("Enter an email address and password.") - - # The password might be a user-specific API key. create_user_key raises - # a ValueError if the user does not exist. - if hmac.compare_digest(self.create_user_key(email, env), pw): - # OK. - pass - else: + # Authenticate. + try: # Get the hashed password of the user. Raise a ValueError if the - # email address does not correspond to a user. + # email address does not correspond to a user. But wrap it in the + # same exception as if a password fails so we don't easily reveal + # if an email address is valid. pw_hash = get_mail_password(email, env) - # Authenticate. - try: - # Use 'doveadm pw' to check credentials. doveadm will return - # a non-zero exit status if the credentials are no good, - # and check_call will raise an exception in that case. - utils.shell('check_call', [ - "/usr/bin/doveadm", "pw", - "-p", pw, - "-t", pw_hash, - ]) - except: - # Login failed. - raise ValueError("Invalid password.") + # Use 'doveadm pw' to check credentials. doveadm will return + # a non-zero exit status if the credentials are no good, + # and check_call will raise an exception in that case. + utils.shell('check_call', [ + "/usr/bin/doveadm", "pw", + "-p", pw, + "-t", pw_hash, + ]) + except: + # Login failed. + raise ValueError("Incorrect email address or password.") - # If MFA is enabled, check that MFA passes. - status, hints = validate_auth_mfa(email, request, env) - if not status: - # Login valid. Hints may have more info. - raise ValueError(",".join(hints)) + # If MFA is enabled, check that MFA passes. + status, hints = validate_auth_mfa(email, request, env) + if not status: + # Login valid. Hints may have more info. + raise ValueError(",".join(hints)) - # Get privileges for authorization. This call should never fail because by this - # point we know the email address is a valid user. But on error the call will - # return a tuple of an error message and an HTTP status code. - privs = get_mail_user_privileges(email, env) - if isinstance(privs, tuple): raise ValueError(privs[0]) - - # Return a list of privileges. - return privs - - def create_user_key(self, email, env): - # Create a user API key, which is a shared secret that we can re-generate from - # static information in our database. The shared secret contains the user's - # email address, current hashed password, and current MFA state, so that the - # key becomes invalid if any of that information changes. - # - # Use an HMAC to generate the API key using our system API key as a key, - # which also means that the API key becomes invalid when our system API key - # changes --- i.e. when this process is restarted. - # - # Raises ValueError via get_mail_password if the user doesn't exist. - - # Construct the HMAC message from the user's email address and current password. - msg = b"AUTH:" + email.encode("utf8") + b" " + get_mail_password(email, env).encode("utf8") + def create_user_password_state_token(self, email, env): + # Create a token that changes if the user's password or MFA options change + # so that sessions become invalid if any of that information changes. + msg = get_mail_password(email, env).encode("utf8") # Add to the message the current MFA state, which is a list of MFA information. # Turn it into a string stably. msg += b" " + json.dumps(get_hash_mfa_state(email, env), sort_keys=True).encode("utf8") - # Make the HMAC. + # Make a HMAC using the system API key as a hash key. hash_key = self.key.encode('ascii') return hmac.new(hash_key, msg, digestmod="sha256").hexdigest() + def create_session_key(self, username, env, type=None): + # Create a new session. + token = secrets.token_hex(32) + self.sessions[token] = { + "email": username, + "password_token": self.create_user_password_state_token(username, env), + } + return token diff --git a/management/daemon.py b/management/daemon.py index bb723ea6..ca891772 100755 --- a/management/daemon.py +++ b/management/daemon.py @@ -56,8 +56,10 @@ def authorized_personnel_only(viewfunc): try: email, privs = auth_service.authenticate(request, env) except ValueError as e: - # Write a line in the log recording the failed login - log_failed_login(request) + # Write a line in the log recording the failed login, unless no authorization header + # was given which can happen on an initial request before a 403 response. + if "Authorization" in request.headers: + log_failed_login(request) # Authentication failed. error = str(e) @@ -134,11 +136,12 @@ def index(): csr_country_codes=csr_country_codes, ) -@app.route('/me') -def me(): +# Create a session key by checking the username/password in the Authorization header. +@app.route('/login', methods=["POST"]) +def login(): # Is the caller authorized? try: - email, privs = auth_service.authenticate(request, env) + email, privs = auth_service.authenticate(request, env, login_only=True) except ValueError as e: if "missing-totp-token" in str(e): return json_response({ @@ -153,19 +156,29 @@ def me(): "reason": str(e), }) + # Return a new session for the user. resp = { "status": "ok", "email": email, "privileges": privs, + "api_key": auth_service.create_session_key(email, env, type='login'), } - # Is authorized as admin? Return an API key for future use. - if "admin" in privs: - resp["api_key"] = auth_service.create_user_key(email, env) + app.logger.info("New login session created for {}".format(email)) # Return. return json_response(resp) +@app.route('/logout', methods=["POST"]) +def logout(): + try: + email, _ = auth_service.authenticate(request, env, logout=True) + app.logger.info("{} logged out".format(email)) + except ValueError as e: + pass + finally: + return json_response({ "status": "ok" }) + # MAIL @app.route('/mail/users') diff --git a/management/templates/index.html b/management/templates/index.html index 12f6ad8e..267f5dd6 100644 --- a/management/templates/index.html +++ b/management/templates/index.html @@ -367,11 +367,17 @@ var current_panel = null; var switch_back_to_panel = null; function do_logout() { + // Clear the session from the backend. + api("/logout", "POST"); + + // Forget the token. api_credentials = ["", ""]; if (typeof localStorage != 'undefined') localStorage.removeItem("miab-cp-credentials"); if (typeof sessionStorage != 'undefined') sessionStorage.removeItem("miab-cp-credentials"); + + // Return to the start. show_panel('login'); } diff --git a/management/templates/login.html b/management/templates/login.html index 19b23d3a..3447d794 100644 --- a/management/templates/login.html +++ b/management/templates/login.html @@ -105,8 +105,8 @@ function do_login() { api_credentials = [$('#loginEmail').val(), $('#loginPassword').val()] api( - "/me", - "GET", + "/login", + "POST", {}, function(response) { // This API call always succeeds. It returns a JSON object indicating diff --git a/setup/management.sh b/setup/management.sh index 1c57bb2e..7e31fe00 100755 --- a/setup/management.sh +++ b/setup/management.sh @@ -49,8 +49,8 @@ hide_output $venv/bin/pip install --upgrade pip # NOTE: email_validator is repeated in setup/questions.sh, so please keep the versions synced. hide_output $venv/bin/pip install --upgrade \ rtyaml "email_validator>=1.0.0" "exclusiveprocess" \ - flask dnspython python-dateutil \ - qrcode[pil] pyotp \ + flask dnspython python-dateutil expiringdict \ + qrcode[pil] pyotp \ "idna>=2.0.0" "cryptography==2.2.2" boto psutil postfix-mta-sts-resolver b2sdk # CONFIGURATION diff --git a/tests/fail2ban.py b/tests/fail2ban.py index 1cb55eba..cb55c51f 100644 --- a/tests/fail2ban.py +++ b/tests/fail2ban.py @@ -232,7 +232,7 @@ if __name__ == "__main__": run_test(managesieve_test, [], 20, 30, 4) # Mail-in-a-Box control panel - run_test(http_test, ["/admin/me", 200], 20, 30, 1) + run_test(http_test, ["/admin/login", 200], 20, 30, 1) # Munin via the Mail-in-a-Box control panel run_test(http_test, ["/admin/munin/", 401], 20, 30, 1) From 26932ecb103b326069f3653e4420d770189c1460 Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sun, 22 Aug 2021 16:38:49 -0400 Subject: [PATCH 032/129] Add a 'welcome' panel to the control panel and make it the default page instead of the status checks which take too long to load Fixes #2014 --- management/templates/index.html | 6 ++++++ management/templates/login.html | 2 +- management/templates/welcome.html | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 management/templates/welcome.html diff --git a/management/templates/index.html b/management/templates/index.html index 267f5dd6..492a953b 100644 --- a/management/templates/index.html +++ b/management/templates/index.html @@ -118,6 +118,10 @@
+
+ {% include "welcome.html" %} +
+
{% include "system-status.html" %}
@@ -409,6 +413,8 @@ $(function() { // Recall what the user was last looking at. if (typeof localStorage != 'undefined' && localStorage.getItem("miab-cp-lastpanel")) { show_panel(localStorage.getItem("miab-cp-lastpanel")); + } else if (api_credentials[0] != "") { + show_panel('welcome'); } else { show_panel('login'); } diff --git a/management/templates/login.html b/management/templates/login.html index 3447d794..8ae79857 100644 --- a/management/templates/login.html +++ b/management/templates/login.html @@ -163,7 +163,7 @@ function do_login() { // Open the next panel the user wants to go to. Do this after the XHR response // is over so that we don't start a new XHR request while this one is finishing, // which confuses the loading indicator. - setTimeout(function() { show_panel(!switch_back_to_panel || switch_back_to_panel == "login" ? 'system_status' : switch_back_to_panel) }, 300); + setTimeout(function() { show_panel(!switch_back_to_panel || switch_back_to_panel == "login" ? 'welcome' : switch_back_to_panel) }, 300); } }, undefined, diff --git a/management/templates/welcome.html b/management/templates/welcome.html new file mode 100644 index 00000000..124d2d28 --- /dev/null +++ b/management/templates/welcome.html @@ -0,0 +1,16 @@ + + +

{{hostname}}

+ +

Welcome to your Mail-in-a-Box control panel.

+ From e5909a62870fc3a9d39a7ffe63a5264f9666ea79 Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sun, 22 Aug 2021 16:40:07 -0400 Subject: [PATCH 033/129] Allow non-admin login to the control panel and show/hide menu items depending on the login state * When logged out, no menu items are shown. * When logged in, Log Out is shown. * When logged in as an admin, the remaining menu items are also shown. * When logged in as a non-admin, the mail and contacts/calendar instruction pages are shown. Fixes #1987 --- management/templates/index.html | 46 +++++++++++++++++++++------------ management/templates/login.html | 28 +++++++++++++++++--- management/templates/users.html | 6 ++--- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/management/templates/index.html b/management/templates/index.html index 492a953b..081d527f 100644 --- a/management/templates/index.html +++ b/management/templates/index.html @@ -62,6 +62,9 @@ ol li { margin-bottom: 1em; } + + .if-logged-in { display: none; } + .if-logged-in-admin { display: none; } @@ -83,7 +86,7 @@
@@ -302,7 +306,7 @@ function ajax_with_indicator(options) { return false; // handy when called from onclick } -var api_credentials = ["", ""]; +var api_credentials = null; function api(url, method, data, callback, callback_error, headers) { // from http://www.webtoolkit.info/javascript-base64.html function base64encode(input) { @@ -350,9 +354,10 @@ function api(url, method, data, callback, callback_error, headers) { // We don't store user credentials in a cookie to avoid the hassle of CSRF // attacks. The Authorization header only gets set in our AJAX calls triggered // by user actions. - xhr.setRequestHeader( - 'Authorization', - 'Basic ' + base64encode(api_credentials[0] + ':' + api_credentials[1])); + if (api_credentials) + xhr.setRequestHeader( + 'Authorization', + 'Basic ' + base64encode(api_credentials.username + ':' + api_credentials.session_key)); }, success: callback, error: callback_error || default_error, @@ -375,7 +380,7 @@ function do_logout() { api("/logout", "POST"); // Forget the token. - api_credentials = ["", ""]; + api_credentials = null; if (typeof localStorage != 'undefined') localStorage.removeItem("miab-cp-credentials"); if (typeof sessionStorage != 'undefined') @@ -383,6 +388,9 @@ function do_logout() { // Return to the start. show_panel('login'); + + // Reset menus. + show_hide_menus(); } function show_panel(panelid) { @@ -405,15 +413,21 @@ function show_panel(panelid) { $(function() { // Recall saved user credentials. - if (typeof sessionStorage != 'undefined' && sessionStorage.getItem("miab-cp-credentials")) - api_credentials = sessionStorage.getItem("miab-cp-credentials").split(":"); - else if (typeof localStorage != 'undefined' && localStorage.getItem("miab-cp-credentials")) - api_credentials = localStorage.getItem("miab-cp-credentials").split(":"); + try { + if (typeof sessionStorage != 'undefined' && sessionStorage.getItem("miab-cp-credentials")) + api_credentials = JSON.parse(sessionStorage.getItem("miab-cp-credentials")); + else if (typeof localStorage != 'undefined' && localStorage.getItem("miab-cp-credentials")) + api_credentials = JSON.parse(localStorage.getItem("miab-cp-credentials")); + } catch (_) { + } + + // Toggle menu state. + show_hide_menus(); // Recall what the user was last looking at. - if (typeof localStorage != 'undefined' && localStorage.getItem("miab-cp-lastpanel")) { + if (api_credentials != null && typeof localStorage != 'undefined' && localStorage.getItem("miab-cp-lastpanel")) { show_panel(localStorage.getItem("miab-cp-lastpanel")); - } else if (api_credentials[0] != "") { + } else if (api_credentials != null) { show_panel('welcome'); } else { show_panel('login'); diff --git a/management/templates/login.html b/management/templates/login.html index 8ae79857..421c8845 100644 --- a/management/templates/login.html +++ b/management/templates/login.html @@ -102,7 +102,7 @@ function do_login() { } // Exchange the email address & password for an API key. - api_credentials = [$('#loginEmail').val(), $('#loginPassword').val()] + api_credentials = { username: $('#loginEmail').val(), session_key: $('#loginPassword').val() } api( "/login", @@ -141,7 +141,9 @@ function do_login() { // Login succeeded. // Save the new credentials. - api_credentials = [response.email, response.api_key]; + api_credentials = { username: response.email, + session_key: response.api_key, + privileges: response.privileges }; // Try to wipe the username/password information. $('#loginEmail').val(''); @@ -152,14 +154,17 @@ function do_login() { // Remember the credentials. if (typeof localStorage != 'undefined' && typeof sessionStorage != 'undefined') { if ($('#loginRemember').val()) { - localStorage.setItem("miab-cp-credentials", api_credentials.join(":")); + localStorage.setItem("miab-cp-credentials", JSON.stringify(api_credentials)); sessionStorage.removeItem("miab-cp-credentials"); } else { localStorage.removeItem("miab-cp-credentials"); - sessionStorage.setItem("miab-cp-credentials", api_credentials.join(":")); + sessionStorage.setItem("miab-cp-credentials", JSON.stringify(api_credentials)); } } + // Toggle menus. + show_hide_menus(); + // Open the next panel the user wants to go to. Do this after the XHR response // is over so that we don't start a new XHR request while this one is finishing, // which confuses the loading indicator. @@ -183,4 +188,19 @@ function show_login() { } }); } + +function show_hide_menus() { + var is_logged_in = (api_credentials != null); + var privs = api_credentials ? api_credentials.privileges : []; + $('.if-logged-in').toggle(is_logged_in); + $('.if-logged-in-admin, .if-logged-in-not-admin').toggle(false); + if (is_logged_in) { + $('.if-logged-in-not-admin').toggle(true); + privs.forEach(function(priv) { + $('.if-logged-in-' + priv).toggle(true); + $('.if-logged-in-not-' + priv).toggle(false); + }); + } + $('.if-not-logged-in').toggle(!is_logged_in); +} diff --git a/management/templates/users.html b/management/templates/users.html index 24adf4a1..2ad5ebdb 100644 --- a/management/templates/users.html +++ b/management/templates/users.html @@ -203,7 +203,7 @@ function users_set_password(elem) { var email = $(elem).parents('tr').attr('data-email'); var yourpw = ""; - if (api_credentials != null && email == api_credentials[0]) + if (api_credentials != null && email == api_credentials.username) yourpw = "

If you change your own password, you will be logged out of this control panel and will need to log in again.

"; show_modal_confirm( @@ -232,7 +232,7 @@ function users_remove(elem) { var email = $(elem).parents('tr').attr('data-email'); // can't remove yourself - if (api_credentials != null && email == api_credentials[0]) { + if (api_credentials != null && email == api_credentials.username) { show_modal_error("Archive User", "You cannot archive your own account."); return; } @@ -264,7 +264,7 @@ function mod_priv(elem, add_remove) { var priv = $(elem).parents('td').find('.name').text(); // can't remove your own admin access - if (priv == "admin" && add_remove == "remove" && api_credentials != null && email == api_credentials[0]) { + if (priv == "admin" && add_remove == "remove" && api_credentials != null && email == api_credentials.username) { show_modal_error("Modify Privileges", "You cannot remove the admin privilege from yourself."); return; } From 52a5100265121860ec44ece60784f57a2b859388 Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Thu, 9 Sep 2021 22:52:30 +0200 Subject: [PATCH 034/129] align recidive search time to a week --- conf/fail2ban/jails.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/fail2ban/jails.conf b/conf/fail2ban/jails.conf index 006a5c7e..b434fc99 100644 --- a/conf/fail2ban/jails.conf +++ b/conf/fail2ban/jails.conf @@ -69,7 +69,7 @@ findtime = 15m enabled = true maxretry = 10 bantime = 2w -findtime = 6d +findtime = 7d action = iptables-allports[name=recidive] # In the recidive section of jail.conf the action contains: # From 91079ab9347b7326a11c4011ce7c6cf8cf8b1491 Mon Sep 17 00:00:00 2001 From: mailinabox-contributor <90476861+mailinabox-contributor@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:12:41 -0500 Subject: [PATCH 035/129] add numeric flag value to DNSSEC DS status message (#2033) Some registrars (e.g. Porkbun) accept Key Data when creating a DS RR, but accept only a numeric flags value to indicate the key type (256 for KSK, 257 for ZSK). https://datatracker.ietf.org/doc/html/rfc5910#section-4.3 --- management/status_checks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/status_checks.py b/management/status_checks.py index e2d4b1a7..1e7223a5 100755 --- a/management/status_checks.py +++ b/management/status_checks.py @@ -646,7 +646,7 @@ def check_dnssec(domain, env, output, dns_zonefiles, is_checking_primary=False): output.print_line("Option " + str(i+1) + ":") output.print_line("----------") output.print_line("Key Tag: " + ds_suggestion['keytag']) - output.print_line("Key Flags: KSK") + output.print_line("Key Flags: KSK (256)") output.print_line("Algorithm: %s / %s" % (ds_suggestion['alg'], ds_suggestion['alg_name'])) output.print_line("Digest Type: %s / %s" % (ds_suggestion['digalg'], ds_suggestion['digalg_name'])) output.print_line("Digest: " + ds_suggestion['digest']) From 353084ce6726e7aaee9b9df1c87dd03df29c5f9e Mon Sep 17 00:00:00 2001 From: Elsie Hupp <9206310+elsiehupp@users.noreply.github.com> Date: Sun, 19 Sep 2021 09:53:03 -0400 Subject: [PATCH 036/129] Use "smart invert" for dark mode (#2038) * Use "smart invert" for dark mode Signed-off-by: Elsie Hupp <9206310+elsiehupp@users.noreply.github.com> * Add more contrast to form controls Co-authored-by: Joshua Tauberer --- management/templates/index.html | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/management/templates/index.html b/management/templates/index.html index 081d527f..115693aa 100644 --- a/management/templates/index.html +++ b/management/templates/index.html @@ -62,9 +62,37 @@ ol li { margin-bottom: 1em; } - + .if-logged-in { display: none; } .if-logged-in-admin { display: none; } + + /* The below only gets used if it is supported */ + @media (prefers-color-scheme: dark) { + /* Invert invert lightness but not hue */ + html { + filter: invert(100%) hue-rotate(180deg); + } + + /* Set explicit background color (necessary for Firefox) */ + html { + background-color: #111; + } + + /* Override Boostrap theme here to give more contrast. The black turns to white by the filter. */ + .form-control { + color: black !important; + } + + /* Revert the invert for the navbar */ + button, div.navbar { + filter: invert(100%) hue-rotate(180deg); + } + + /* Revert the revert for the dropdowns */ + ul.dropdown-menu { + filter: invert(100%) hue-rotate(180deg); + } + } From e54dc198542c7d2c3cefda3ff40bb09a51da40bc Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Tue, 21 Sep 2021 22:17:10 +0200 Subject: [PATCH 037/129] slightly change dns resolver call --- management/status_checks.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/management/status_checks.py b/management/status_checks.py index 531a63d9..33641ed2 100755 --- a/management/status_checks.py +++ b/management/status_checks.py @@ -73,6 +73,9 @@ def get_ssh_port(): except FileNotFoundError: # sshd is not installed. That's ok. return None + except subprocess.CalledProcessError: + # error while calling shell command + return None returnNext = False for e in output.split(): @@ -799,7 +802,7 @@ def query_dns(qname, rtype, nxdomain='[Not Set]', at=None, as_list=False): # Do the query. try: - response = resolver.resolve(qname, rtype) + response = resolver.resolve(qname, rtype, search=True) except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): # Host did not have an answer for this query; not sure what the # difference is between the two exceptions. From df46e1311b05d4f01e1908dbfaa4102c18aa9094 Mon Sep 17 00:00:00 2001 From: drpixie Date: Fri, 24 Sep 2021 22:07:40 +1000 Subject: [PATCH 038/129] Include NSD config files from /etc/nsd/nsd.conf.d/*.conf (#2035) And write MIAB dns zone config into /etc/nsd/nsd.conf.d/zones.conf. Delete lingering old zones.conf file. Co-authored-by: Joshua Tauberer --- management/dns_update.py | 2 +- setup/dns.sh | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/management/dns_update.py b/management/dns_update.py index c000f345..b79e266a 100755 --- a/management/dns_update.py +++ b/management/dns_update.py @@ -604,7 +604,7 @@ def get_dns_zonefile(zone, env): def write_nsd_conf(zonefiles, additional_records, env): # Write the list of zones to a configuration file. - nsd_conf_file = "/etc/nsd/zones.conf" + nsd_conf_file = "/etc/nsd/nsd.conf.d/zones.conf" nsdconf = "" # Append the zones. diff --git a/setup/dns.sh b/setup/dns.sh index b64a6580..c8a73a73 100755 --- a/setup/dns.sh +++ b/setup/dns.sh @@ -62,7 +62,13 @@ for ip in $PRIVATE_IP $PRIVATE_IPV6; do echo " ip-address: $ip" >> /etc/nsd/nsd.conf; done -echo "include: /etc/nsd/zones.conf" >> /etc/nsd/nsd.conf; +# Create a directory for additional configuration directives, including +# the zones.conf file written out by our management daemon. +echo "include: /etc/nsd/nsd.conf.d/*.conf" >> /etc/nsd/nsd.conf; + +# Remove the old location of zones.conf that we generate. It will +# now be stored in /etc/nsd/nsd.conf.d. +rm -f /etc/nsd/zones.conf # Create DNSSEC signing keys. From 66b15d42a505feecd1013bcb41ee2aa73ca850ee Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sun, 19 Sep 2021 09:03:57 -0400 Subject: [PATCH 039/129] CHANGELOG entries --- CHANGELOG.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9338b052..e22592fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,33 @@ CHANGELOG ========= +In Development +-------------- + +Mail: + +* Upgraded to Roundcube 1.5 Release Candidate. + +Firewall: + +* Fail2ban's IPv6 support is enabled. + +Control panel: + +* The control panel menus are now hidden before login, but now non-admins can log in to access the mail and contacts/calendar instruction pages. +* The login form now disables browser autocomplete in the two-factor authentication code field. +* After logging in, the default page is now a fast-loading welcome page rather than the slow-loading system status checks page. +* The backup retention period option now displays for B2 backup targets. +* The DNSSEC DS record recommendations are cleaned up and now recommend changing records that use SHA1. +* Control panel logins are now tied to a session backend that allows true logouts (rather than an encrypted cookie). +* Failed logins no longer directly reveal whether the email address corresponds to a user account. +* Browser dark mode now inverts the color scheme. + +Other: + +* The mail log tool now doesn't crash if there are email addresess in log messages with invalid UTF-8 characters. +* Additional nsd.conf files can be placed in /etc/nsd.conf.d. + v0.54 (June 20, 2021) --------------------- From 79966e36e3f74a50e923c74e83faf76e16c6ef13 Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Sun, 12 Sep 2021 19:51:16 -0400 Subject: [PATCH 040/129] Set a cookie for /admin/munin pages to grant access to Munin reports The /admin/munin routes used the same Authorization: header logic as the other API routes, but they are browsed directly in the browser because they are handled as static pages or as a proxy to a CGI script. This required users to enter their email username/password for HTTP basic authentication in the standard browser auth prompt, which wasn't ideal (and may leak the password in browser storage). It also stopped working when MFA was enabled for user accounts. A token is now set in a cookie when visiting /admin/munin which is then checked in the routes that proxy the Munin pages. The cookie's lifetime is kept limited to limit the opportunity for any unknown CSRF attacks via the Munin CGI script. --- CHANGELOG.md | 1 + management/auth.py | 16 +++++++++------ management/daemon.py | 36 ++++++++++++++++++++++++++++----- management/templates/index.html | 6 +++++- management/templates/munin.html | 20 ++++++++++++++++++ 5 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 management/templates/munin.html diff --git a/CHANGELOG.md b/CHANGELOG.md index e22592fe..7dd0b9ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Control panel: * After logging in, the default page is now a fast-loading welcome page rather than the slow-loading system status checks page. * The backup retention period option now displays for B2 backup targets. * The DNSSEC DS record recommendations are cleaned up and now recommend changing records that use SHA1. +* The Munin monitoring pages no longer require a separate HTTP basic authentication login and can be used if two-factor authentication is turned on. * Control panel logins are now tied to a session backend that allows true logouts (rather than an encrypted cookie). * Failed logins no longer directly reveal whether the email address corresponds to a user account. * Browser dark mode now inverts the color scheme. diff --git a/management/auth.py b/management/auth.py index 38c15e91..0a88c457 100644 --- a/management/auth.py +++ b/management/auth.py @@ -72,14 +72,9 @@ class AuthService: return (None, ["admin"]) # If the password corresponds with a session token for the user, grant access for that user. - if password in self.sessions and self.sessions[password]["email"] == username and not login_only: + if self.get_session(username, password, "login", env) and not login_only: sessionid = password session = self.sessions[sessionid] - if session["password_token"] != self.create_user_password_state_token(username, env): - # This session is invalid because the user's password/MFA state changed - # after the session was created. - del self.sessions[sessionid] - raise ValueError("Session expired.") if logout: # Clear the session. del self.sessions[sessionid] @@ -158,5 +153,14 @@ class AuthService: self.sessions[token] = { "email": username, "password_token": self.create_user_password_state_token(username, env), + "type": type, } return token + + def get_session(self, user_email, session_key, session_type, env): + if session_key not in self.sessions: return None + session = self.sessions[session_key] + if session_type == "login" and session["email"] != user_email: return None + if session["type"] != session_type: return None + if session["password_token"] != self.create_user_password_state_token(session["email"], env): return None + return session diff --git a/management/daemon.py b/management/daemon.py index ca891772..280efec0 100755 --- a/management/daemon.py +++ b/management/daemon.py @@ -654,16 +654,42 @@ def privacy_status_set(): # MUNIN @app.route('/munin/') -@app.route('/munin/') @authorized_personnel_only -def munin(filename=""): - # Checks administrative access (@authorized_personnel_only) and then just proxies - # the request to static files. +def munin_start(): + # Munin pages, static images, and dynamically generated images are served + # outside of the AJAX API. We'll start with a 'start' API that sets a cookie + # that subsequent requests will read for authorization. (We don't use cookies + # for the API to avoid CSRF vulnerabilities.) + response = make_response("OK") + response.set_cookie("session", auth_service.create_session_key(request.user_email, env, type='cookie'), + max_age=60*30, secure=True, httponly=True, samesite="Strict") # 30 minute duration + return response + +def check_request_cookie_for_admin_access(): + session = auth_service.get_session(None, request.cookies.get("session", ""), "cookie", env) + if not session: return False + privs = get_mail_user_privileges(session["email"], env) + if not isinstance(privs, list): return False + if "admin" not in privs: return False + return True + +def authorized_personnel_only_via_cookie(f): + @wraps(f) + def g(*args, **kwargs): + if not check_request_cookie_for_admin_access(): + return Response("Unauthorized", status=403, mimetype='text/plain', headers={}) + return f(*args, **kwargs) + return g + +@app.route('/munin/') +@authorized_personnel_only_via_cookie +def munin_static_file(filename=""): + # Proxy the request to static files. if filename == "": filename = "index.html" return send_from_directory("/var/cache/munin/www", filename) @app.route('/munin/cgi-graph/') -@authorized_personnel_only +@authorized_personnel_only_via_cookie def munin_cgi(filename): """ Relay munin cgi dynazoom requests /usr/lib/munin/cgi/munin-cgi-graph is a perl cgi script in the munin package diff --git a/management/templates/index.html b/management/templates/index.html index 115693aa..f9c87f2c 100644 --- a/management/templates/index.html +++ b/management/templates/index.html @@ -124,7 +124,7 @@
  • Custom DNS
  • External DNS
  • -
  • Munin Monitoring
  • +
  • Munin Monitoring
  • Mail
  • @@ -202,6 +202,10 @@ {% include "ssl.html" %}
    +
    + {% include "munin.html" %} +
    +