From ad3cbfae87b80e0f2e44ee6f7d07606d928e210e Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Tue, 10 Oct 2023 09:06:30 +0200 Subject: [PATCH] updated dovecot state --- states/dovecot/config.sls | 21 ++++++++++++++++++- states/dovecot/defaults.yaml | 9 ++++++++ states/dovecot/install.sls | 1 - states/dovecot/templates/globalsieverc.j2 | 8 +++++++ states/dovecot/templates/report-ham.sieve.j2 | 16 ++++++++++++++ states/dovecot/templates/report-spam.sieve.j2 | 8 +++++++ states/dovecot/templates/train-ham.sh.j2 | 2 ++ states/dovecot/templates/train-spam.sh.j2 | 2 ++ states/dovecot/templates/update_sieve.sh.j2 | 5 +++++ 9 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 states/dovecot/templates/globalsieverc.j2 create mode 100644 states/dovecot/templates/report-ham.sieve.j2 create mode 100644 states/dovecot/templates/report-spam.sieve.j2 create mode 100644 states/dovecot/templates/train-ham.sh.j2 create mode 100644 states/dovecot/templates/train-spam.sh.j2 create mode 100644 states/dovecot/templates/update_sieve.sh.j2 diff --git a/states/dovecot/config.sls b/states/dovecot/config.sls index 9c50e57..5fd1d8f 100644 --- a/states/dovecot/config.sls +++ b/states/dovecot/config.sls @@ -1,6 +1,5 @@ --- {%- from "dovecot/map.jinja" import dovecot with context %} - dovecot-imap-user: user.present: - name: {{ dovecot.imap_user }} @@ -33,3 +32,23 @@ dovecot-config-conf.d-{{ name }}: - context: config: {{ config }} {%- endfor %} + +dovecot-sieve-dir: + file.directory: + - name: {{ dovecot.sieve_dir }} + +{%- if dovecot.sieve_configfiles %} +{%- for configfile in dovecot.sieve_configfiles %} +dovecot-sieve-configfile-{{ configfile }}: + file.managed: + - name: {{ dovecot.sieve_dir }}/{{ configfile }} + - mode: '0755' + - source: salt://dovecot/templates/{{ configfile }}.j2 + - template: jinja +{%- endfor %} +{%- endif %} + +dovecot-sieve-refresh: + cmd.run: + - name: "{{ dovecot.sieve_dir }}/update_sieve.sh" + - cwd: {{ dovecot.sieve_dir }} diff --git a/states/dovecot/defaults.yaml b/states/dovecot/defaults.yaml index 3f33a47..3098b0a 100644 --- a/states/dovecot/defaults.yaml +++ b/states/dovecot/defaults.yaml @@ -5,6 +5,15 @@ dovecot: - dovecot-imapd - dovecot-lmtpd - dovecot-pgsql + root_storage: /var/lib/vmail + sieve_dir: /var/lib/vmail/sieve + sieve_configfiles: + - globalsieverc + - report-ham.sieve + - report-spam.sieve + - train-ham.sh + - train-spam.sh + - update_sieve.sh imap_user: vmail imap_user_id: 2020 config_dir: /etc/dovecot diff --git a/states/dovecot/install.sls b/states/dovecot/install.sls index 5696f48..540a8c4 100644 --- a/states/dovecot/install.sls +++ b/states/dovecot/install.sls @@ -1,6 +1,5 @@ --- {%- from "dovecot/map.jinja" import dovecot with context %} - dovecot-pkgs: pkg.installed: - pkgs: {{ dovecot.pkgs }} diff --git a/states/dovecot/templates/globalsieverc.j2 b/states/dovecot/templates/globalsieverc.j2 new file mode 100644 index 0000000..4957f22 --- /dev/null +++ b/states/dovecot/templates/globalsieverc.j2 @@ -0,0 +1,8 @@ +{%- from "dovecot/map.jinja" import dovecot with context -%} +require ["fileinto"]; + +# Move spam to spam folder +if anyof (header :contains "X-Spam-Flag" "YES", header :is "X-Spam" "Yes") { + fileinto "Junk"; + stop; +} diff --git a/states/dovecot/templates/report-ham.sieve.j2 b/states/dovecot/templates/report-ham.sieve.j2 new file mode 100644 index 0000000..78b1ea7 --- /dev/null +++ b/states/dovecot/templates/report-ham.sieve.j2 @@ -0,0 +1,16 @@ +{%- from "dovecot/map.jinja" import dovecot with context -%} +require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"]; + +if environment :matches "imap.mailbox" "*" { + set "mailbox" "${1}"; +} + +if string "${mailbox}" "Trash" { + stop; +} + +if environment :matches "imap.email" "*" { + set "email" "${1}"; +} + +pipe :copy "train-ham.sh" [ "${email}" ]; diff --git a/states/dovecot/templates/report-spam.sieve.j2 b/states/dovecot/templates/report-spam.sieve.j2 new file mode 100644 index 0000000..17d0d2c --- /dev/null +++ b/states/dovecot/templates/report-spam.sieve.j2 @@ -0,0 +1,8 @@ +{%- from "dovecot/map.jinja" import dovecot with context -%} +require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"]; + +if environment :matches "imap.email" "*" { + set "email" "${1}"; +} + +pipe :copy "train-spam.sh" [ "${email}" ]; diff --git a/states/dovecot/templates/train-ham.sh.j2 b/states/dovecot/templates/train-ham.sh.j2 new file mode 100644 index 0000000..39da8df --- /dev/null +++ b/states/dovecot/templates/train-ham.sh.j2 @@ -0,0 +1,2 @@ +{%- from "dovecot/map.jinja" import dovecot with context -%} +exec /usr/bin/rspamc -h 127.0.0.1:11334 learn_ham diff --git a/states/dovecot/templates/train-spam.sh.j2 b/states/dovecot/templates/train-spam.sh.j2 new file mode 100644 index 0000000..34c65bb --- /dev/null +++ b/states/dovecot/templates/train-spam.sh.j2 @@ -0,0 +1,2 @@ +{%- from "dovecot/map.jinja" import dovecot with context -%} +exec /usr/bin/rspamc -h 127.0.0.1:11334 learn_spam diff --git a/states/dovecot/templates/update_sieve.sh.j2 b/states/dovecot/templates/update_sieve.sh.j2 new file mode 100644 index 0000000..f60dda5 --- /dev/null +++ b/states/dovecot/templates/update_sieve.sh.j2 @@ -0,0 +1,5 @@ +{%- from "dovecot/map.jinja" import dovecot with context -%} +#!/usr/bin/env sh + +/usr/bin/find {{ dovecot.sieve_dir }} -name "*.sieve" -exec /usr/bin/sievec {} \; +/usr/bin/find {{ dovecot.root_storage }} -name "dovecot.sieve" -exec /usr/bin/sievec {} \;